Hi, I've written a program to test how quickly one can react to something, with the press of a button. This will be looped, and I keep track of the amount of times the user has tried.
But my problem is storing each try (double) and then comparing these values, becuse I need to display the minimum, maximum, and average result each time. Can anyone help me?
Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
void setup()
{
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
pinMode(8, INPUT); // button
digitalWrite(8, HIGH); //pull-up
}
int i = 1;
double startTime;
double endTime;
double reactionTime;
double minimum;
double maximum;
double average;
void loop()
{
start:
delay(300);
while(digitalRead(8) == HIGH)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Reaction #");
lcd.print(i);
lcd.setCursor(0,1);
lcd.print("Push to start");
delay(150);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Concentrate!");
int randTime = random(3000, 10000);
delay(randTime);
lcd.clear();
lcd.print("################");
lcd.setCursor(0,1);
lcd.print("################");
double startTime = millis();
while(digitalRead(8) == HIGH)
{
}
double reactionTime = (millis()-startTime) / 1000;
if(minimum=<reactionTime){
minimum = reactionTime;
}
if(maximum>=reactionTime){
maximum = reactionTime;
}
average = i /
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time:");
lcd.print(reactionTime, 3);
lcd.print(" ant:");
lcd.print(i);
lcd.setCursor(0,1);
lcd.print("Min: ");
lcd.print(minimum, 3);
delay(1500);
lcd.print("Max: ");
lcd.print(maximum, 3);
delay(1500);
lcd.print("Avg: ");
lcd.print(average, 3);
delay(1500);
if (digitalRead(8) == LOW)
{
i++;
goto start;
}
else
{
i++;
}
}
double startTime = millis();
Why is startTime a double when millis() returns an unsigned long ?
Why have you got two variables named startTime, each with their own scope ?
Rookie mistakes, thanks for pointing it out!
Is it bad to use start and goto?
I've worked on my code, but somehow the reactionTime wont print the time now. any ideas why?
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
void setup()
{
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
pinMode(8, INPUT); // button
digitalWrite(8, HIGH); //pull-up
}
int i = 0;
unsigned long startTime;
double reactionTime[20]; // this should not have an upper limit, but if i leave it open i get error
double minimum = 0;
double maximum = 0;
double average;
void loop()
{
Serial.println(reactionTime[i]);
delay(300);
while(digitalRead(8) == HIGH)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Reaction #");
lcd.print(i+1);
lcd.setCursor(0,1);
lcd.print("Push to start");
delay(150);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Consentrate!!");
int randTime = random(3000, 10000);
delay(randTime);
lcd.clear();
lcd.print("################");
lcd.setCursor(0,1);
lcd.print("################");
startTime = millis();
while(digitalRead(8) == HIGH)
{
}
reactionTime[i] = (millis()-startTime) / 1000;
if (reactionTime[i] <= reactionTime[i-1]){
minimum = reactionTime[i];
}
if reactionTime[i] >= reactionTime[i-1]){
maximum = reactionTime[i];
}
int s = 0;
for (int k=0; k < 10; k++)
{
s += reactionTime[k];
}
average = s / (i+1);
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time:");
lcd.print(reactionTime[i], 3);
lcd.print(" try:");
lcd.print(i+1);
lcd.setCursor(0,1);
lcd.print("Min: ");
lcd.print(minimum, 3);
delay(2000);
lcd.setCursor(0,1);
lcd.print("Max: ");
lcd.print(maximum, 3);
delay(2000);
lcd.setCursor(0,1);
lcd.print("Avg: ");
lcd.print(average, 3);
delay(2000);
while(digitalRead(8) != LOW)
{
}
if (digitalRead(8) == LOW)
{
i++;
}
}
Why are you using double to store the times?
millis returns unsigned long.
And then you sum the times in an "int".
TheMemberFormerlyKnownAsAWOL:
Why are you using double to store the times?
millis returns unsigned long.
And then you sum the times in an "int".
I initially used unsigned long, but just played around to see if it would affect the result. But it didn't.
Just some low hanging fruit:
while (digitalRead(8) != LOW)
{
}
if (digitalRead(8) == LOW)
{
i++;
}
If you waited for pin 8 to go low, you don't need to test it again after the while exit. You know it must be.
Hmm, like this?
while(digitalRead(8) != LOW)
{
}
i++;
}
edit: seems to be working .