User:Lieven Van Speybroeck/Prototyping/5-Playback Pattern: Difference between revisions

From XPUB & Lens-Based wiki
Line 47: Line 47:
// It goes through the whole array with the time values (except for the last 2, these represent the 'play' push)
// It goes through the whole array with the time values (except for the last 2, these represent the 'play' push)
// and plays the tones and delays based on the array. After it has played 1 tone and the following delay, it
// and plays the tones and delays based on the array. After it has played 1 tone and the following delay, it
// goes op 2 positions in the array, because for playing one note, you always need 2 time values.
// goes uyp 2 positions in the array, because for playing one note, you always need 2 time values.
// So the notes form 'couples' of time, where the delay between them is the last value of one note subtracted from the first value of the next note  
// So the notes form 'couples' of time, where the delay between them is the last value of one note subtracted from the first value of the next note  



Revision as of 19:36, 14 November 2010

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=0;

void setup(){
  pinMode(spkrPin, OUTPUT);
  pinMode(btnPin, INPUT);
  Serial.begin(9600);
  tone(spkrPin, 400, 10);
  
}

void loop(){
  while (delayTime < 5000) { // While there's no delay of 5 seconds, keep making a 'record'. 
    Serial.println("Waiting for press...");  
    while (btnActive==LOW){ // While the button is not pressed
        delayTime=(millis()-toneArray[i-1]); // how long is the button left unpressed?
        btnActive=digitalRead(btnPin);
    }
    // ok, pressed now...
    toneArray[i]=millis(); // Put the time of pressing in an array that stores the times
    i++; // add 1 to the array pointer
    
    Serial.println("Waiting for release...");  
    btnActive=digitalRead(btnPin);
    while (btnActive==HIGH){
        btnActive=digitalRead(btnPin);
        tone(spkrPin, 400, 10);
    }
    //ok, released now...
    toneArray[i]=millis(); // Put the time of releasing in the array that stores the times
    i++; // add one to the array pointer
  }
 
// the next loop gets triggered when the button has been left unpressed for 5 seconds and then pressed 1 more time.
// It goes through the whole array with the time values (except for the last 2, these represent the 'play' push)
// and plays the tones and delays based on the array. After it has played 1 tone and the following delay, it
// goes uyp 2 positions in the array, because for playing one note, you always need 2 time values.
// So the notes form 'couples' of time, where the delay between them is the last value of one note subtracted from the first value of the next note 

  for (g=0; g<(i-2); g=g+2) {
     tone(spkrPin, 400, (toneArray[g+1]-toneArray[g])); // play the tones based on the time values stored in the array
     delay(toneArray[g+2]-toneArray[g+1]); // define the delay based on the time values stored in the array
  }
  
  while(1){}; // 'stop' the program
}