Hello all,
I used bounce example and read a lot on the internet and now I would apricate your help:
I have NO push button in pull down connection, I'm trying to use debounce and timer for turning on the led (after that it will be 5 (3+2) different 12V loads, x time for 3 of them and then y time for 2+2 of them and then turn off the system) but it doesn't work.
here is the code:
const int button = 2;
const int led = 4;
int button_state = LOW; // the current reading from the button
int led_state = LOW; // the current state of the led
unsigned long currentMillis = 0; //currentMillis = current time
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
unsigned long outputsOnTime = 5*1000UL; //5 seconds
unsigned long ledMillis; //led on timing period start
void setup() {
pinMode(button, INPUT); // set the button as input
pinMode(led, OUTPUT); // set the led as output
Serial.begin(9600);
// set initial OUTPUT state
digitalWrite(led, led_state);
}
void loop() {
//save the current time:
currentMillis = millis();
int reading = digitalRead(button);
// If the switch changed, due to noise or pressing:
if (reading != button_state) {
lastDebounceTime = millis();
}
if ((currentMillis - lastDebounceTime) >= debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != button_state) {
button_state = reading;
// only toggle the OUTPUT if the new button state is HIGH
if (button_state == HIGH) {
led_state = !led_state;
digitalWrite(led, led_state);
ledMillis = millis();
}
if((currentMillis - ledMillis) >= outputsOnTime){
led_state = !led_state;
digitalWrite(led, led_state);
ledMillis = currentMillis;
button_state = LOW;
}
}
}
}
In that code - when I press the button -> led turn on, stop pressing -> led turn off.
Kudos to @UKHeliBob
https://forum.arduino.cc/index.php?topic=503368.0
Thanks in advance