User:Lieven Van Speybroeck/Prototyping/5-Playback Pattern: Difference between revisions
Line 12: | Line 12: | ||
int delayTime=0; | int delayTime=0; | ||
int i=0; | int i=0; | ||
int g=0; | int g=1; | ||
int q=0; | |||
void setup(){ | void setup(){ | ||
pinMode(spkrPin, OUTPUT); | pinMode(spkrPin, OUTPUT); | ||
Line 19: | Line 20: | ||
Serial.begin(9600); | Serial.begin(9600); | ||
tone(spkrPin, 400, 10); | tone(spkrPin, 400, 10); | ||
} | } | ||
void loop(){ | 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 | while (delayTime < 5000) { // Keep 'recording' until the buttons is untouched for a minimum of 5 seconds | ||
btnActive=digitalRead(btnPin); // Read the buttonstate | btnActive=digitalRead(btnPin); // Read the buttonstate | ||
while (btnActive==LOW){ // Go through this loop as long as the button is left unpressed | 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 | delayTime=(millis()-toneArray[i-1]); // Measure the time the button is left unpressed | ||
btnActive=digitalRead(btnPin); | btnActive=digitalRead(btnPin); | ||
} | } | ||
// ok, pressed now... | // ok, pressed now... | ||
toneArray[i]=millis(); // Store 'time of press' into an array | toneArray[i]=millis(); // Store 'time of press' into an array | ||
Serial.print(" | 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 | i++; // Move up the pointer in the array 1 position | ||
btnActive=digitalRead(btnPin); // Read the buttonstate | btnActive=digitalRead(btnPin); // Read the buttonstate | ||
while (btnActive==HIGH){ // Go through this loop as long as the button is pressed | while (btnActive==HIGH){ // Go through this loop as long as the button is pressed | ||
btnActive=digitalRead(btnPin); | btnActive=digitalRead(btnPin); | ||
tone(spkrPin, 400, 10); // Play tone while pushing the button | tone(spkrPin, 400, 10); // Play tone while pushing the button | ||
} | } | ||
// ok, released now... | // ok, released now... | ||
toneArray[i]=millis(); // Store 'time of release' into the array | toneArray[i]=millis(); // Store 'time of release' into the array | ||
Serial.print (" | 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 | i++; // Move up the pointer in the array 1 position | ||
} | } | ||
// record ended... | // 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(" "); | |||
Serial.println(" | for (q=0; q<i; q++){ | ||
for (g= | 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=1; g<(i-2); g=g+2){ | |||
tone(spkrPin, 400, (toneArray[g+1]-toneArray[g])); // Playback the tones based on the values in the array | 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 | delay(toneArray[g+2]-toneArray[g+1]); // Define the delay between tones based on the values in the array | ||
Serial.print(" | 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(" | 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 | while(1){}; // 'Stop the program: go into an endless empty loop | ||
} | } | ||
</source> | </source> |
Revision as of 11:50, 16 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=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=1; 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
}