Setting up a reset timer

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;
  }
}

Welcome to the forum

Which board are you using ?
Why do you want to reset the board ?

Resetting the board is not a great solution in virtually every case and doesn’t look to be required in your case .
What are you trying to do ??

You can use an analog output to pull the reset down

Am running it on an uno right now for set up and testing then moving the atmega328 chip to home made circuit.

Needing to reset the user will not be near the buttons so if it resets after set amount of time then players can get ready and just wait for reset to play

1 Like

Trying to get it to restart program after 10 seconds of no input from user so that it will be ready for next user with out having to walk to button put to reset/stop timer then walk back to were they need to be for program to start

You do not need to reset the board in order to do that.

How would you recommend doing it? Right now it just keeps running till buttons are pushed. Was wanting it to restart the delay and timer after 10 seconds of no activity.

Here is a virtual running of my program

https://wokwi.com/projects/363678646264965121

Write a function that sets any required variables, including the state, to starting values and call that from setup(). The function can be called whenever you want to set the variables back to the start

I was looking at this bit of code is this what you are talking about?

unsigned long myTime; // Millis() function time value 
int timeExpected = 5000; // I want to reset it in every 5 secs for testing.
const int RESET_PIN = 2; // Reset pin's connection

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN,OUTPUT);
}
void loop() {
  digitalWrite(LED_BUILTIN,HIGH);
    myTime = millis();
    Serial.println(myTime); // prints time since program started
     delay(1000);

   if(myTime % timeExpected == 0) {
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
     Serial.println("RESET");
    //digitalWrite(RESET_PIN, LOW);(I just test with LED_BUILTIN Low function)
    }

  }

I was referring to any variable values that need to be changed back to their initial values when you restart the system without a reset. Typically this would be any variables holding the state of the system, timing variables that need to be given a starting value and variables holding the state of inputs

It would help if you described what your sketch is intended to do and how users interact with it

Need to figure out best way for section marked in red to last 10 seconds before it starts over at start

flow.jpg

Save the value of millis() as the light turns on. Then every time through loop() test whether the button has become pressed. If so then take the action you choose. If not then test whether 10 seconds has elapsed by subtracting the start time from millis(). If so then take the action required

Sometimes setting a boolean to true to flag that timing is taking place can help as it means that the code can test it to determine the state of the sketch. Using switch/case eliminates the need for the boolean by executing the code block for the timing state

I had a similar project, and I used the Watchdog Timer. Just include #include <avr/wdt.h> in your code, enable it in setup() with wdt_enable(WDTO_8S); , and reset it in loop() using wdt_reset(); . Change WDTO_8S for different timeout durations. Hope this helps! :blush:

Problem ran into with watchdog was it limits it to 8 second from start of program i have a 1 to 10 second delay at start so it can throw it out of range before it starts

presumably you need a reset() that

  • turns the LED off
  • captures the current timestamp, and
  • determines a random amount of time to turn it on

loop()

  • monitors the time until the LED needs to turn on and captures timestamp to time-out, call reset(), if the button is not pressed

  • turns it on and captures a 2nd timestamp and

  • checks when the button is pressed and prints the time since the LED turned on

  • call reset if the LED is on, the button hasn't been pressed and the timeout has expired

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.