User:Lieven Van Speybroeck/Prototyping/5-Playback Pattern

From XPUB & Lens-Based wiki

Recording music with a pushbutton on Arduino

This program lets you make a record of tones by pushing a pushbutton on Arduino.
Waiting 5 seconds ends the 'record-session'.
By pushing 1 more time, the pattern you recorded will be played back.

int btnPin=2;
int spkrPin=13;
int btnActive=0;
int toneArray[100];
int delayTime=0;
int i=0;
int g=1;
int q=0;
 
void setup(){
  pinMode(spkrPin, OUTPUT);
  pinMode(btnPin, INPUT);
  Serial.begin(9600);
  tone(spkrPin, 400, 10);
 
}
 
void loop(){
  
  Serial.println(" ");
  Serial.println("RECORD STARTED");
  Serial.println("--------------");
  Serial.println(" ");
  
  while (delayTime < 5000) { // Keep 'recording' until the buttons is untouched for a minimum of 5 seconds
 
    btnActive=digitalRead(btnPin); // Read the buttonstate
 
    while (btnActive==LOW){ // Go through this loop as long as the button is left unpressed
        delayTime=(millis()-toneArray[i-1]); // Measure the time the button is left unpressed
        btnActive=digitalRead(btnPin);
    }
 
// ok, pressed now...
 
    toneArray[i]=millis(); // Store 'time of press' into an array
    Serial.print("WAITED: ");Serial.print(delayTime, DEC);Serial.println(" milliseconds"); // Print how long you've waited to push the button
    i++; // Move up the pointer in the array 1 position
 
    btnActive=digitalRead(btnPin); // Read the buttonstate
 
    while (btnActive==HIGH){ // Go through this loop as long as the button is pressed
        btnActive=digitalRead(btnPin);
        tone(spkrPin, 400, 10); // Play tone while pushing the button
    }
 
// ok, released now...
 
    toneArray[i]=millis(); // Store 'time of release' into the array
    Serial.print ("PUSHED: ");Serial.print((toneArray[i]-toneArray[i-1]), DEC);Serial.println(" milliseconds"); // Print how long you've pushed the button
    i++; // Move up the pointer in the array 1 position
  }
  
// record ended
  
  Serial.println(" ");
  Serial.println("RECORD ENDED");
  Serial.println("------------");
  Serial.println(" ");
  
// print the array so we see which values are in there...

  Serial.println("These values are in the array:");
  Serial.println(" ");

  for (q=0; q<i; q++){
     Serial.print("toneArray[");Serial.print(q);Serial.print("]: ");Serial.println(toneArray[q]);
  }
 
// playback time

  Serial.println(" ");
  Serial.println("PLAYBACK");
  Serial.println("--------");
  Serial.println(" ");
 
  // This gets triggered when the 'record' is ended, so the button was left untouched for 5 seconds and then got pushed 1 more time
  for (g=0; g<(i-2); g=g+2){
     tone(spkrPin, 400, (toneArray[g+1]-toneArray[g])); // Playback the tones based on the values in the array
     delay(toneArray[g+2]-toneArray[g+1]); // Define the delay between tones based on the values in the array
     Serial.print("TONE: " );Serial.print((toneArray[g+1]-toneArray[g]), DEC);Serial.println(" milliseconds"); // Print out the length of the tone that was played
     Serial.print("DELAY: ");Serial.print((toneArray[g+2]-toneArray[g+1]), DEC);Serial.println(" milliseconds"); // Print out the length of the delay
  }
 
  while(1){}; // 'Stop the program: go into an endless empty loop
}

An example of a record in the serial monitor:

 

RECORD STARTED

--------------

 

WAITED: 563 milliseconds

PUSHED: 979 milliseconds

WAITED: 665 milliseconds

PUSHED: 1815 milliseconds

WAITED: 1534 milliseconds

PUSHED: 223 milliseconds

WAITED: 362 milliseconds

PUSHED: 188 milliseconds

WAITED: 568 milliseconds

PUSHED: 3535 milliseconds

WAITED: 3852 milliseconds

PUSHED: 1064 milliseconds

WAITED: 10342 milliseconds

PUSHED: 83 milliseconds

 

RECORD ENDED

------------

 

These values are in the array:

 

toneArray[0]: 563

toneArray[1]: 1542

toneArray[2]: 2207

toneArray[3]: 4022

toneArray[4]: 5556

toneArray[5]: 5779

toneArray[6]: 6141

toneArray[7]: 6329

toneArray[8]: 6897

toneArray[9]: 10432

toneArray[10]: 14284

toneArray[11]: 15348

toneArray[12]: 25690

toneArray[13]: 25773

 

PLAYBACK

--------

 

TONE: 979 milliseconds

DELAY: 665 milliseconds

TONE: 1815 milliseconds

DELAY: 1534 milliseconds

TONE: 223 milliseconds

DELAY: 362 milliseconds

TONE: 188 milliseconds

DELAY: 568 milliseconds

TONE: 3535 milliseconds

DELAY: 3852 milliseconds

TONE: 1064 milliseconds

DELAY: 10342 milliseconds