ArduinoProjects / Experimental / MusicMachine / MusicMachine.ino

// Arduino 7 segment display example software
// http://www.hacktronics.com/Tutorials/arduino-and-7-segment-led.html
// License: http://www.opensource.org/licenses/mit-license.php (Go crazy)
// Created by: Jody McAdams
// twitter: @MegaJiXiang
/* 
Pins starts from top left

1. Anode F          LED Pin 1        Arduino Pin 8
2. Anode G          LED Pin 2        Arduino Pin 9
3. No Pin
4. Common Cathode
5. No Pin
6. Anode E          LED Pin 6        Arduino Pin 7
7. Anode D          LED Pin 7        Arduino Pin 6
8. Anode C          LED Pin 8        Arduino Pin 5
9. Anode RHDP       LED Pin 9        Arduino Pin 10
10. No Pin
11. No Pin
12. Common Cathode  
13. Anode B         LED Pin 13       Arduino Pin 4
14. Anode A         LED Pin 14       Arduino Pin 3
*/

// Define the LED digit patters, from 0 - 9
// Note that these patterns are for common cathode displays
// For common anode displays, change the 1's to 0's and 0's to 1's
// 1 = LED on, 0 = LED off, in this order:
//                                    Arduino pin: 3,4,5,6,7,8,9,

const char g_numDigits = 10;
byte seven_seg_digits[g_numDigits][7] = {
                                 { 1,1,1,1,1,1,0 },  // = 0
                                 { 0,1,1,0,0,0,0 },  // = 1
                                 { 1,1,0,1,1,0,1 },  // = 2
                                 { 1,1,1,1,0,0,1 },  // = 3
                                 { 0,1,1,0,0,1,1 },  // = 4
                                 { 1,0,1,1,0,1,1 },  // = 5
                                 { 1,0,1,1,1,1,1 },  // = 6
                                 { 1,1,1,0,0,0,0 },  // = 7
                                 { 1,1,1,1,1,1,1 },  // = 8
                                 { 1,1,1,0,0,1,1 }   // = 9
                                                           };

const char g_numNotes = 12;                                                          
byte seven_seg_notes[g_numNotes][8] = {                                 
                                 { 1,0,0,1,1,1,0,0 },  // = C
                                 { 1,0,0,1,1,1,0,1 },  // = C#
                                 { 0,1,1,1,1,0,1,0 },  // = D
                                 { 0,1,1,1,1,0,1,1 },  // = D#
                                 { 1,0,0,1,1,1,1,0 },  // = E
                                 { 1,0,0,0,1,1,1,0 },  // = F
                                 { 1,0,0,0,1,1,1,1 },  // = F#
                                 { 1,1,1,1,0,1,1,0 },  // = G
                                 { 1,1,1,1,0,1,1,1 },  // = G#
                                 { 1,1,1,0,1,1,1,0 },  // = A
                                 { 1,1,1,0,1,1,1,1 },  // = A#
                                 { 0,0,1,1,1,1,1,0 },  // = B
                                                           };

int g_numNotesInSet = 12;
int g_noteOffset = g_numNotesInSet*2;
int g_notes[] =
{

//2
65,
69,
73,
78,
82,
87,
93,
98,
104,
110,
117,
123,

//3
131,
139,
147,
156,
165,
175,
185,
196,
208,
220,
233,
247,

//4
262,
277,
294,
311,
330,
349,
370,
392,
415,
440,
466,
494,

//5
523,
554,
587,
622,
659,
698,
740,
784,
831,
880,
932,
988,

//6
1047,
1109,
1175,
1245,
1319,
1397,
1480,
1568,
1661,
1760,
1865,
1976,

//7
2093,
2217,
2349,
2489,
2637,
2794,
2960,
3136,
3322, 
3520,
3729,
3951,

};
                          
int g_numNotesRecorded = 0;

const int g_numRecordableNotes = 8;
int g_recordedNotes[g_numRecordableNotes];

int g_potPin = 0;    // select the input pin for the potentiometer
int g_buzzerPin = 11;
int g_buttonPin1 = 12;
int g_buttonPin2 = 13;

void setup() {                
  pinMode(3, OUTPUT);//Anode A
  pinMode(4, OUTPUT);//Anode B  
  pinMode(5, OUTPUT);//Anode C
  pinMode(6, OUTPUT);//Anode D
  pinMode(7, OUTPUT);//Anode E
  pinMode(8, OUTPUT);//Anode F
  pinMode(9, OUTPUT);//Anode G
  pinMode(10, OUTPUT);//Anode RHDP (right hand decimal place)
  
  writeDot(0);  // start with the "dot" off
  
  pinMode(g_potPin, INPUT);  // declare g_potPin as an INPUT
  
  pinMode(g_buttonPin1, INPUT);  // declare g_buttonPin1 as an INPUT
  pinMode(g_buttonPin2, INPUT);  // declare g_buttonPin2 as an INPUT
}

void writeDot(byte dot) {
  digitalWrite(10, dot);
}
    
void sevenSegWriteDigit(byte digit) {
  byte pin = 3;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pin, seven_seg_digits[digit][segCount]);
    ++pin;
  }
}

void sevenSegWriteNote(byte noteIndex) {
  byte pin = 3;
  for (byte segCount = 0; segCount < 8; ++segCount) {
    digitalWrite(pin, seven_seg_notes[noteIndex][segCount]);
    ++pin;
  }
}

int g_allowRecord = false;
int g_allowModeSwitch = true;

int g_songFinished = false;

int g_playBackIndex = 0;

int g_recordBleepTime = 100;

void loop() {
  
  int potVal = analogRead(g_potPin);    // read the value from the sensor
  float potFrac = 1.0f-(potVal/1023.0f);
   
  // read the state of the pushbutton value:
  int buttonState1 = digitalRead(g_buttonPin1);
  
  // read the state of the pushbutton value:
  int buttonState2 = digitalRead(g_buttonPin2);

  int potBasedNote = potFrac*(g_numNotes-1) + 0.5f;
  
  //If you have released either of these buttons, you
  //can switch modes again
  if(buttonState1 == LOW && buttonState2 == LOW)
  {
    g_allowModeSwitch = true;
  }
  
  //Play mode
  if(g_songFinished)
   {
     //Re-enter record mode
     if( g_allowModeSwitch
         && ((buttonState1 == HIGH && buttonState2 == HIGH)))
      {
        g_allowModeSwitch = false;
        
        g_songFinished = false;
        g_numNotesRecorded = 0;
      }
      
      
     //for(int i=0; i<g_numNotesRecorded; ++i)
     //{
        //Display current note
        //sevenSegWriteNote(potBasedNote);
        tone(g_buzzerPin,g_notes[g_recordedNotes[g_playBackIndex]+g_noteOffset],200);
        
        delay(500*(1.0f-potFrac));
        noTone(g_buzzerPin);
     //}
     
     ++g_playBackIndex;
     
     if(g_playBackIndex == g_numNotesRecorded)
      {
        g_playBackIndex = 0;
      }
   }
   //Record mode
   else
   {
       //End song early
       if( g_allowModeSwitch
         && ((buttonState1 == HIGH && buttonState2 == HIGH)
      || (g_numNotesRecorded == g_numRecordableNotes) ))
      {
        g_allowModeSwitch = false;
        g_songFinished = true;
      }
  
       sevenSegWriteNote(potBasedNote);
       
        //Test the note before recording
       if(buttonState1 == HIGH)
        {
         tone(g_buzzerPin,g_notes[potBasedNote+g_noteOffset],100);
        }
        else
         {
           noTone(g_buzzerPin);
         }
         
      //Record the note on button press
      if(g_allowRecord && (buttonState2 == HIGH))
       {
         tone(g_buzzerPin,g_notes[potBasedNote+g_noteOffset],g_recordBleepTime);
         delay(g_recordBleepTime);
         noTone(g_buzzerPin);
         
         g_recordedNotes[g_numNotesRecorded] = potBasedNote;
         ++g_numNotesRecorded;
         
         g_allowRecord = false;
       }
       else if(buttonState2 == LOW)
       {
         g_allowRecord = true;
       }
   }
  
  //delay(2000);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.