User:Laurier Rochon/prototyping/arduino recorder
< User:Laurier Rochon
Revision as of 19:16, 7 November 2010 by Laurier Rochon (talk | contribs) (Created page with "== Rough arduino pattern recorder/playback #1 == Simple code to emulate a pattern recording device. It has a few little glitches, but the main idea works. *Program starts *User...")
Rough arduino pattern recorder/playback #1
Simple code to emulate a pattern recording device. It has a few little glitches, but the main idea works.
- Program starts
- User starts using pushbutton to determine a pattern (up to 100 clicks before array goes out of bounds)
- After 5 seconds of silence, after user has started pushing (there can be 1min at the beginning though, that's fine)
- Play the pattern back in a loop, with 2 sec intervals
(there is no way to restart it yet, I guess that will be part II)
VIDEO
{{#ev:vimeo|16590052|640}}
HARD
- Piezo in ground/pin 13
- Pushbutton in 10K resistor => pin 2 / 5V power
SOFT
//globals
const int buttonPin = 2;
int values[100];
int c=0;
unsigned long time;
boolean startrec = false;
int buttonState = 1;
int newstate = 1;
void setup() {
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop(){
//time counter
time = millis();
buttonState = digitalRead(buttonPin);
//this prevents the button from recording 2-3 times in the same push...
delay(10);
//if state has changed
if(buttonState != newstate){
startrec = true;
//store in the array, print to screen and increment array index
newstate = buttonState;
values[c] = time/100;
Serial.println(values[c]);
c++;
}
if(startrec){
//if it's been more than 5 seconds doing nothing, after you've started pushing
int deadtime = (time/100)- values[c-1];
if(deadtime > 50){
int cc = 0;
//loop through values, and interpret the time difference as delays
for(int a=0;a<c-1;a++){
tone(13, 600,10);
int dtime = (values[a+1]-values[a])*100;
delay(dtime);
}
//gimme something to look at
Serial.println("waiting 2 secs...replaying");
delay(2000);
}
}
}