Millis() +EEPROM

Hi guys.

I need little help from you.
Can someone please help me sorting the "SEC" in the code below?
Simply, this is a countdown for the button to prevent accidental write to EEPROM. Its counting down from 3-0 in 1 second intervals. When Reaching 0 it stores value to EEPROM (in this case, just for testing purposes it adds +1 just to see if its not writing on every loop)
So everything is cool except for the fact that the value "SEC" once on 0 i goes back to 3 (because i write 3 when button is HIGH) where i want it to stay on 0 once the while loop stops executing and reset to 3 only if next button press occur if that make sense :slight_smile:

#include "EEPROM.h"
int sec = 0;
int timer0Button = 0;
int interval = 1000;
int timer0StartMillis;
int timer0CurrentMillis;
bool inputPin = 0;
int memory = EEPROM.read(10);
bool finished = false;
void setup() {
  Serial.begin(9600);
  pinMode(8, INPUT);
  timer0StartMillis = millis();
}

void loop() {

  Serial.print("timer0Button = ");
  Serial.print(timer0Button );
  Serial.print("SEC = ");
  Serial.print(sec );
  Serial.print("EEPROM = ");
  Serial.println(memory);
  if (digitalRead(8) == HIGH) {
    timer0Button = 1;
    sec = 3;
    while (!finished) {
      if (digitalRead(8) == LOW){
        break;
      }
      Serial.print("timer0Button = ");
      Serial.print(timer0Button );
      Serial.print("SEC = ");
      Serial.print(sec );
      Serial.print("EEPROM = ");
      Serial.println(memory);
      if (timer0Button == 1) {
        timer0CurrentMillis = millis();
        if (timer0CurrentMillis - timer0StartMillis > interval) {
          sec -= 1;
          timer0StartMillis = timer0CurrentMillis;
        }
        if (sec == 0) {
          EEPROM.write(10, (memory += 1));
          timer0Button = 0;
          finished = true;

        }
      }
    }
  }
  else {
    timer0Button = 0;
    sec = 0;
    finished = false;
  }

}

Thanks for your help

  if (digitalRead(8) == HIGH) {
    timer0Button = 1;
    sec = 3;

If you change it to this:

  if (digitalRead(8) == HIGH) {
    if (timer0Button == 0) {
       timer0button = 1;
       sec = 3;
    }

Then 'sec' will only be set to 3 when you go from not-pressed to pressed.

johnwasser:

  if (digitalRead(8) == HIGH) {

timer0Button = 1;
    sec = 3;




If you change it to this:


if (digitalRead(8) == HIGH) {
    if (timer0Button == 0) {
      timer0button = 1;
      sec = 3;
    }



Then 'sec' will only be set to 3 when you go from not-pressed to pressed.

Thanks,their is no difference im afraid. Either my code and your extra section dosnt change anything, still returns to 3 after the cycle rather than staying on 0 unless unless the input is reseted.
Thanks

Because the button is still pressed when countdown is finished, "sec" is immediatly set to 3.
You should reset the timing variables int the first iteration of the while loop. (When sec == 0).

Try change

  if (digitalRead(8) == HIGH) {
    timer0Button = 1;
    sec = 3;
    while (!finished) {
      if (digitalRead(8) == LOW){
        break;
      }

to (compiles but not tested...)

  if (digitalRead(8) == HIGH) {
    while (!finished) {
      if (digitalRead(8) == LOW){
        break;
      }
      if (sec == 0) {                    //Restart timer
        timer0Button = 1;
        sec = 3;
        timer0StartMillis = millis();     
      }