Commits

Jody McAdams committed ce104f5

pretty good music device

  • Participants
  • Parent commits 9fe1d9d

Comments (0)

Files changed (1)

File 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
 
                                  { 0,0,1,1,1,1,1,0 },  // = B
                                                            };
 
-int g_numNotesInSet = 12;                             
+int g_numNotesInSet = 12;
+int g_noteOffset = g_numNotesInSet*2;
 int g_notes[] =
 {
-  
-//1
-33,
-35,
-37,
-39,
-41,
-44,
-46,
-49,
-52,
-55,
-58,
-62,
 
 //2
 65,
                           
 int g_numNotesRecorded = 0;
 
-const int g_numRecordableNotes = 3;
+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(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) {
   }
 }
 
+int g_allowRecord = false;
+int g_allowModeSwitch = true;
+
+int g_songFinished = false;
+
+int g_playBackIndex = 0;
+
+int g_recordBleepTime = 100;
+
 void loop() {
-  for (byte count = 0; count < g_numNotes; ++count) {
-   delay(500);
-   sevenSegWriteNote(count); 
-   tone(11,g_notes[count+g_numNotesInSet*3],1000);
+  
+  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);
 }