The countdown doesn't work on the buttons

I made an action button to make a countdown, when the button is pressed it counts down from 10 seconds, when it is pressed again it counts down from 20 seconds, the problem is when the done1 and done2 ​​buttons can be enabled, what should I change?

unsigned long prevMillis = 0;

// Counter (seconds)
int counter;
int counter2;

const int ledPin =  LED_BUILTIN;
unsigned long previousMillis = 0;
int current_Volts = 1;
int current_Volts_low;
int current_Volts_high;

bool counter1start = false;
bool counter2start = false;

 const int buttonPin = 4;
 int buttonState = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  counter = 10;
  counter2 = 20;
  prevMillis = millis();
  Serial.println("Please enter your name:");
}

void loop () {
  buttonState = digitalRead(buttonPin);

  if (millis() - previousMillis >= 1000) {
    previousMillis = millis();
    digitalWrite(ledPin, !digitalRead(ledPin));
    if ((buttonState == HIGH) && counter1start == false) {
      counter2 = 20;
      counter1start = true;
      counter2start = false; 
    }
    if (counter1start) Serial.println(counter--);
    if (counter <= 0) {
      counter1start = false;
      Serial.println("Done1");
      //current_Volts = 50;
      counter = 10;
    }
    if ((buttonState == LOW) && counter2start == false) {
      counter = 10;
      counter1start = false;
      counter2start = true;
    }
    if (counter2start) Serial.println(counter2--);
    if (counter2 <= 0) {
      counter2start = false;
      Serial.println("Done2");
      //current_Volts = 50;
      counter2 = 20;
    }
  }
}

This code structure is similar to whack a mole coding. :woozy_face:

Study how to use the State Machine coding technique in you sketches.

Edit

Thanks for using code tags.


Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

YMMD :+1:

Try this:

#include <Arduino.h>
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/archive/refs/heads/master.zip

Bounce2::Button button = Bounce2::Button();

const int buttonPin = 4;
const int ledPin =  LED_BUILTIN;

byte step = 0;
int counter;
int counterLimit = 10;
bool start = false;

unsigned long previousMillis = 0;

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

  Serial.println("Please enter your name:");

  button.attach(buttonPin, INPUT_PULLUP);
  button.interval(5); // interval im ms
  button.setPressedState(LOW);
}

void loop()
{
  button.update();

  if (button.pressed())
  {
    if(step == 1)
    {
      counter = 0;
      step = 2;
      counterLimit = 20;
      start = true;
      previousMillis = millis();
      Serial.println("Starting countdown 2");
      Serial.println(counterLimit - counter);
    }
    else
    {
      counter = 0;
      step = 1;
      counterLimit = 10;
      start = true;
      previousMillis = millis();
      Serial.println("Starting countdown 1");
      Serial.println(counterLimit - counter);
    }
  }

  switch (step)
  {
    case 0:
      break;

    case 1:
      if ((millis() - previousMillis >= 1000) && (start == true))
      {
        counter++;
        previousMillis = millis();
        Serial.println(counterLimit - counter);
      }
      if((counter == counterLimit) && (start == true))
      {
        start = false;
        Serial.println("Done 1!");
      }
      break;
    case 2:
      if ((millis() - previousMillis >= 1000) && (start == true))
      {
        counter++;
        previousMillis = millis();
        Serial.println(counterLimit - counter);
      }
      if((counter == counterLimit) && (start == true))
      {
        start = false;
        Serial.println("Done 2!");
      }
      break;
    default:
      break;
  }
}

Hello bimosora

Try, check and study this small C++ example for your count down task:

/* BLOCK COMMENT
  - https://forum.arduino.cc/t/the-countdown-doesnt-work-on-the-buttons/1088319
  - The countdown doesn’t work on the buttons
  - This sketch may contain traces of C++.
  - In case of indisposition:
  - https://www.learncpp.com/
  - Hardware:
  - Thanks to LarryD
  - https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
  - https://forum.arduino.cc/t/beginners-software-needs-hardware/1066703
  - Tested @ Arduino: Mega[X] - UNO [ ] - Nano [ ]
*/
#define ProjectName "The countdown doesn’t work on the buttons"
// make structures
struct TIMER
{
  const unsigned long Interval;
  unsigned long stamp;
};
struct COUNTDOWN
{
  const int CountDowns[2];
  int countDownValue;
  const int ButtonPin;
  int stateOld;
  TIMER debounce;
  TIMER count;
} countDown {{10, 20}, 10, A0, false, 20, 0,  1000, 0};
// tools
void heartBeat(int LedPin)
{
  static bool setUp = false;
  if (!setUp) pinMode (LedPin, OUTPUT), setUp = !setUp;
  digitalWrite(LedPin, (millis() / 1000) % 2);
}
void setup()
{
  Serial.begin(115200);
  Serial.print(ProjectName), Serial.print(" in "), Serial.println(__FILE__);
  pinMode (countDown.ButtonPin, INPUT_PULLUP);
}
void loop()
{
  heartBeat(LED_BUILTIN);
  unsigned long currentMillis = millis();
  if (currentMillis - countDown.debounce.stamp >= countDown.debounce.Interval && countDown.countDownValue == 0 ? HIGH : LOW) {
    countDown.debounce.stamp = currentMillis;
    int stateNew = digitalRead(countDown.ButtonPin) ? LOW : HIGH;
    if (countDown.stateOld != stateNew)
    {
      countDown.stateOld = stateNew;
      if (stateNew == HIGH)
      {
        static int selector = LOW;
        countDown.countDownValue = countDown.CountDowns[selector];
        Serial.print("Starting countdown "), Serial.println(selector + 1);
        selector = selector == HIGH ? LOW : HIGH;
      }
    }
  }
  if (currentMillis - countDown.count.stamp >= countDown.count.Interval && countDown.countDownValue != 0 ? HIGH : LOW)
  {
    countDown.count.stamp = currentMillis;
    Serial.print("count down "), Serial.println(countDown.countDownValue--);
    if (countDown.countDownValue == 0) Serial.println("done");
  }
}

Have a nice day and enjoy coding in C++.

Thanks everyone I'll try to clean it up and try what you came up with

When the initial button is HIGH will it run automatically?

Show me your circuit.

For example I have a rain sensor, when there is no rain it will automatically read the digital pin is low, what about your code will it read low the first time, I only see the code working when the button is pressed?

:point_up_2:

I tried to change the button in your code using a slide switch to experiment whether it will run automatically in LOW status or not

What about show me what you doing in reality?

Pay attention to the button here, it should when I slide it to the right HIGH the way but it only works when there is a ground cable only

Is there anything I have to change, it looks like the countdown works when the button is released not when it is pressed or released?

You can't connect in this way.

Check it:

It doesn't work for switches

Have you ever seen the light turn on when the button is pressed and the light turns off when the button is released. That's what I meant to turn on the counter

It's the same thing.

You can't short arduino pin with 5V or GND directly.

Using internal pull up the state will be always HIGH on right side and LOW in left side,

switch

Your code actually works but I want to replace it with a switch so when the switch I point to the right of the counter 2 way, then I point to the left of the counter 1 way, so the button is not released then the new run

You know this tool, when it's high, for example, the lights on the Arduino are on, then when it's low, the lights on the Arduino are off, but I want to change it to 2 counters for the lights. The counter lights on first and the counter lights off first
Arduino-IR-Collision-Detection-Module-Pin-Outs