User:Lieven Van Speybroeck/Prototyping/5-Playback Pattern: Difference between revisions
mNo edit summary |
|||
Line 9: | Line 9: | ||
int spkrPin=13; | int spkrPin=13; | ||
int btnActive=0; | int btnActive=0; | ||
int toneArray[100]; | int toneArray[100]; | ||
int delayTime=0; | int delayTime=0; | ||
int g=0; | int g=0; | ||
int i=0; | |||
void setup(){ | void setup(){ |
Revision as of 17:21, 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 g=0;
int i=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
}
for (g=0; g<(i-2); g=g+2) { // this function gets triggered when the button has been left unpressed for 5 seconds and then pressed 1 more time.
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]); // olay the delay based on the time values stored in the array
}
while(1){}; // 'stop' the program
}