[SOLVED] Counter Time No Working

How to solve the time counter that doesn't run when I put the value inside the loop instead of outside the loop?
COUNTER TIMER WORK

unsigned long previousMillis1 = 0;
const long interval1 = 1000;

int cd = 50;
int ready = 1;

void setup () {
Serial.begin (9600);
}
void loop () {
  if(millis() - previousMillis1 >= interval1) {
    previousMillis1 = millis();
    if(ready == 1){
      Serial.println(cd);
      cd--;
      if(cd == 0){
        ready = 0;
        Serial.println("gone");
      }
    }
  }
}

COUNTER TIMER NO WORK

unsigned long previousMillis1 = 0;
const long interval1 = 1000;

int cd;
int ready;

void setup () {
Serial.begin (9600);
}
void loop () {
cd = 50;
ready = 1;
  if(millis() - previousMillis1 >= interval1) {
    previousMillis1 = millis();
    if(ready == 1){
      Serial.println(cd);
      cd--;
      if(cd == 0){
        ready = 0;
        Serial.println("gone");
      }
    }
  }
}

icd?

First line after loop

If you need values to be kept while loop() is running you must initialize them once, either when you define the variables or inside the setup() function.
If you put "cd=50; ready=1;" those values are set to initial value each time, so "cd" won't ever go to zero as it'll be always equal to 49 after "cd--".

I can't run if the counter is of type like this

void loop () {
cd = 50;
ready = 1;
  if(millis() - previousMillis1 >= interval1) {
    previousMillis1 = millis();
    if(ready == 1){
      Serial.println(cd);
      cd--;
      if(cd == 0){
        ready = 0;
        Serial.println("gone");
      }
    }
  }
}

Static
See post 3

Is there a way to make it run, i make the time counter the data is fetched from the eeprom?

I found it this way, is it the right way?

unsigned long previousMillis1 = 0;
const long interval1 = 1000;

void setup () {
  Serial.begin (9600);
}

void loop () {
  int cd = 50;
  int ready = 1;
  while( true ) {
    if(millis() - previousMillis1 >= interval1) {
      previousMillis1 = millis();
      if(ready == 1){
        Serial.println(cd);
        cd--;
        if(cd == 0){
          ready = 0;
          Serial.println("mati");
        }
      }
    }
    delay(2);
  }
}

No, you have just created a second loop to match the original working code. This is blocking and not encouraged.

Try static int cd equals 50 in your original non working example

Not sure if will work as on phone so can’t test

What is wrong with post3

Is there another way to create counter timers with ready data fetched from the eeprom when something changes?

how about

const unsigned long Interval1 = 1000;
      unsigned long msecLst;

int cd;

void loop () {
    unsigned long msec = millis ();

    if (0 < cd && msec - msecLst >= Interval1) {
        previousMillis1 = msec;

        cd--;
        Serial.println (cd);
        if (cd == 0)
            Serial.println ("mati");
    }
}

void setup () {
    Serial.begin (9600);
    cd = 10;
}

Is that why the setup is below?

Yes, it's possible, have a look at the EEPROM library, but what is your intended usage? You want to stop a count and resume it after powering down Arduino? Or you need just to know if on previous count reached zero (and stop working again if rebooted)? In both cases, are you sure you understood what EEPROM is for?

PS: and remember EEPROM has a limited "working life", proportional to the number of writes...

It worked for me but if I change the eeprom does the cd code in setup change without having to restart the device?

not clear what you want to do.

i set the value of "cd" in setup to suggest that it can be re-initialized at any time.

not sure what you want the EEPROM for? would the EEPROM contain the value for "cd"? if so, you might read and set the value of "cd" from the EEPROM read in setup()

This is what I mean, when you finish pressing the button, the data is stored in the eeprom and then the data from the eeprom is displayed to bring up the counter timers. If we put vltg = a; in void setup can eeprom display it there without having to restart the device. void setup doesn't it only run once since the supply is matched?

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "OneButton.h"
#include "EEPROM.h"

OneButton b1(33, true);
OneButton b2(14, true);
OneButton b3(26, true);

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32

#define OLED_RESET     -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define NUMFLAKES     10

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16

byte a;

int vlt = 1;

void setup() {
  EEPROM.begin(64);
  Serial.begin(9600);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }

  b1.attachClick(click1);
  b1.attachDoubleClick(doubleclick1);
  b1.attachLongPressStart(longPressStart1);
  b1.attachLongPressStop(longPressStop1);
  b1.attachDuringLongPress(longPress1);

  // link the b 2 functions.
  b2.attachClick(click2);
  b2.attachDoubleClick(doubleclick2);
  b2.attachLongPressStart(longPressStart2);
  b2.attachLongPressStop(longPressStop2);
  b2.attachDuringLongPress(longPress2);

  b3.attachClick(click3);
  b3.attachDoubleClick(doubleclick3);
  b3.attachLongPressStart(longPressStart3);
  b3.attachLongPressStop(longPressStop3);
  b3.attachDuringLongPress(longPress3);

  a = EEPROM.read(0);
  vlt = a;
}

void loop() {
  b1.tick();
  b2.tick();
  b3.tick();
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(1, 1);
  display.print(msg);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(50, 15);
  display.print(a);
  display.display();
  unsigned long msec = millis ();
  
  if(msec - previousMillis1 >= interval1) {
    previousMillis1 = msec;
      if(vlt == 1){
        a--;
        if(a == 0){
          vlt = 0;
          if(msec - previousMillis1 >= interval1) {
            previousMillis1 = msec;
            Serial.print("123");
          }
        }
      }
  }
  
  EEPROM.commit();
}

void click1() {
  a--;
}

void doubleclick1() {
  a--;
}

void longPressStart1() {
  a--;
}

void longPress1() {
  a--;
}

void longPressStop1() {
}

void click2() {
  EEPROM.write(0, a);
}

void doubleclick2() {
}

void longPressStart2() {
}
void longPress2() {
}

void longPressStop2() {
}

void click3() {
  a++;
}

void doubleclick3() {
  a++;
}

void longPressStart3() {
  a++;
}
void longPress3() {
  a++;
}

void longPressStop3() {
  a++;
}

capture this in a sub-function that can be called from setup() on startup as well as after a button press

I don't know the workflow

what does that mean?

show me the code to do the above -- that reads a displays the EEPROM values

display.print(a); or void setup () { cd = a } ?

The code works but I haven't been able to make if (cd == 0) Serial.println("mati"); blink