was trying to set up the program so it will rest set the board after a set amount of time examples are
rest set after x+10seconds were x is a random number generated else were or were when (ledPin1, HIGH) then have it reset after 10 seconds time.
i have tried the watch dog and for the most part it worked except when the x start time was too high for it.
i don't have the pins free to use a hardware solution unless the is a way to use the analog pins
#include <TM1637Display.h>
#include <avr/wdt.h>
// Set up the display with 4 bits - R/W tied to GND
byte CLK1 = 6;
byte DIO1 = 7;
byte CLK2 = 8;
byte DIO2 = 9;
byte CLK3 = 10;
byte DIO3 = 11;
byte ledPin1 = 2; // red stop LED
byte ledPin2 = 3; // red stop LED
byte buttonPin1 = 4; // reaction timer button1
byte buttonPin2 = 5; // reaction timer button1
TM1637Display display1 = TM1637Display(CLK1, DIO1); // LED display
TM1637Display display2 = TM1637Display(CLK2, DIO2); // LED display
TM1637Display display3 = TM1637Display(CLK3, DIO3); // LED display
// States of execution
long randomDelayTime; // holds the random time amount
unsigned long timerStartMillis; // the time when the timer started
unsigned long timerEndMillis1; // the time when the timer1 finished
unsigned long timerEndMillis2; // the time when the timer2 finished
unsigned long difference1;
unsigned long difference2;
enum State {prepareState, waitState, timingState, resultState};
State myProgrammState = prepareState;
// Setup function - called only once
ISR(WDT_vect)
{
wdt_disable(); // disable watchdog
}
void setup() {
display1.setBrightness(5);
display2.setBrightness(5);
display3.setBrightness(5);
pinMode(ledPin1, OUTPUT); // red LED is an output
pinMode(ledPin2, OUTPUT); // red LED is an output
pinMode(buttonPin1, INPUT); // button is an input
pinMode(buttonPin2, INPUT); // button is an input
}
void loop() {
switch (myProgrammState) {
case prepareState:
randomDelayTime = random(10000); // this is the random amount to be used 0-10 seconds
while ((digitalRead(buttonPin1) == true) ||
(digitalRead(buttonPin2) == true)) {} // wait until the buttons are released
myProgrammState = waitState; // finished prepare state - lets move on
display1.clear();
display2.clear();
display3.clear();
display2.showNumberDecEx(randomDelayTime, false, 2, 4 );
timerEndMillis1 = 0;
timerEndMillis2 = 0;
break;
case waitState:
delay(randomDelayTime); // delay for the random amount
digitalWrite(ledPin1, HIGH); // when finished - set red LED high
digitalWrite(ledPin2, HIGH); // when finished - set red LED high
myProgrammState = timingState; // now we are ready to start timing reactions
timerStartMillis = millis(); // get the current time;
break;
case timingState:
if ((digitalRead(buttonPin1) == true) && (timerEndMillis1 == 0)) { // when they press the button1
timerEndMillis1 = millis(); // get the current time
digitalWrite(ledPin1, LOW); // turn off the red led
difference1 = timerEndMillis1 - timerStartMillis; // time taken is difference between times
}
if ((digitalRead(buttonPin2) == true) && (timerEndMillis2 == 0)) { // when they press the button2
timerEndMillis2 = millis(); // get the current time
digitalWrite(ledPin2, LOW); // turn off the red led
difference2 = timerEndMillis2 - timerStartMillis; // time taken is difference between times
}
if ((timerEndMillis1 != 0) && (timerEndMillis2 != 0)) { //both buttons are pressed
myProgrammState = resultState;
}
break;
case resultState:
display1.showNumberDecEx(difference1, false, 2, 4 );
display3.showNumberDecEx(difference2, false, 2, 4 );
delay(5000); // leave the message on the screen for 5 seconds
myProgrammState = prepareState; // ready to start all over
break;
}
}