Hey guys, so I have been trying to create a working reaction tester using this board as I want to upload the times recorded however I started developing using tinkercad's emulator. I now have a working version of this idea but when recreating it using the hardware I have a few issues I can't seem to resolve.
- When pressing the button it will light up the onboard LED instead of the LED on the breadboard
- It records its own time and outputs this into the serial monitor instead of the user tracked time
Here is what I have designed in Tinkercad but instead of the UNO its the MRK 1010.
int buttonPin = 2; // the number of the push button pin
int ledPin = 3; // the number of the LED pin
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean Started = false;
boolean timer = false;
long startTime;
long endTime;
long randomTime;
float elapsedTime;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(buttonPin);
if(last != current)
{
delay(5);
current = digitalRead(buttonPin);
}
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 Taken: ");
Serial.println(elapsedTime);
digitalWrite(ledPin, LOW);
}
-Thanks.
