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