Reaction Time Program Help
hello. i've been trying use code below reaction timer. led supposed flash randomly, , when user presses button, time took them press button supposed printed onto serial monitor. however, elapsed time seems printing serial monitor without button having been pressed.
i wondering needed change in order fix problem. advice appreciated! thank you.
code: [select]
int switchpin = 2;
int ledpin = 13 ;
boolean lastbutton = low;
boolean currentbutton = low;
boolean started = false;
boolean timer = false;
long starttime;
long endtime;
long randomtime;
float elapsedtime;
void setup()
{
pinmode(switchpin, input);
pinmode(ledpin, output);
serial.begin(9600);
}
boolean debounce(boolean last)
{
boolean current = digitalread(switchpin);
if(last != current)
{
delay(5);
current = digitalread(switchpin);
}
return current;
}
void loop()
{
currentbutton = debounce(lastbutton);
if(lastbutton == low && currentbutton == high)
{
started = !started;
lastbutton = high;
}
lastbutton = currentbutton;
if(started == true && timer == false)
{
random();
timer = true;
}
if(started == false && timer == true)
{
stop();
timer = false;
}
}
void random()
{
randomtime = random(4,10);
randomtime = randomtime*1000;
digitalwrite(ledpin, high);
delay(100);
digitalwrite(ledpin, low);
delay(randomtime);
start();
}
void start(){
starttime = millis();
digitalwrite(ledpin, high);
}
void stop(){
endtime = millis();
elapsedtime = (endtime - starttime)+5;
elapsedtime = elapsedtime/1000;
serial.print("time seconds: ");
serial.println(elapsedtime);
digitalwrite(ledpin, low);
}
}
i wondering needed change in order fix problem. advice appreciated! thank you.
i haven't spent time going through it. see pinmode switch being set input. should input_pullup. pin states high , low , int type, not boolean. though won't cause problem in experience.
Arduino Forum > Using Arduino > Programming Questions > Reaction Time Program Help
arduino
Comments
Post a Comment