debounce + millis - ***SOLVED***

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

"but it doesn't work." doesn't give us anything to offer help with.

I don't see a question here.
If you follow the forum guidelines, post your code using code tags and then ask your question using complete sentences, you are likely to get more guidance.

We are likely going to want a schematic. Not a pretty Fritzing Lego picture. We are used to thinking of a circuit according to its function, not its physical layout, so the schematic diagram is the language in which we understand the circuit.

You are trying so hard to wrap your head around it to try to put everything together. Please don't do that.
It is possible to make your sketch work, but it will be hard to understand.
Pull everything away from each other, and make separate blocks according to the what it does (its functionality).
The sketch might become bigger, but I don't care :wink: The structure of the code should be visible with a glance at the code.

If you make a debounce, then make a seperate part that only does the debouncing.

If you want to detect the moment a button is pressed. Then use the State Change Detection example: https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection

If you want a millis-timer, then make a separate part for that.

I suggest to combine the debounce with the State Change Detection and use the Bounce2 library for that: GitHub - thomasfredericks/Bounce2: Debouncing library for Arduino and Wiring.

If you only want to turn on a led for some time, then you can use millis() for a single shot timer: Fun_with_millis/millis_single_delay.ino at master · Koepel/Fun_with_millis · GitHub. If you want more than a led, for example a sequence of multiple things at multiple times, then there are a few good ways to do that (a table with command, a finite state machine and a few more).

I don't understand what kind of sequence you want to do. Can you explain it better or make a drawing or time table ?

Your code can be simplified by using the button library and output library:

#include <ezButton.h> // ezButton library
#include <ezOutput.h> // ezOutput library

ezButton button(7); // create Button object that attach to pin 7;
ezOutput led(9);    // create ezOutput object that attach to pin 9;

void setup() {
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isPressed())
    led.toggle();
}

Sorry with the late response, I'm new in the forum and didn't find the post :smiley:

Thank you everyone for the help.
The goal of the code is to operate different loads with power mosfet npn logic level like water pump, solenoids (total 5 loads). At the begging I tried with two leds.

Now the code is more clear, separate blocks - two state machine - dealing with loads, and one function dealing with the push button.

You would simplify your code if you just used a capacitor for the debounce.

Also you can find more info on bounce 2 there

@johnerrington I did but no change

Now the code is more clear, separate blocks - two state machine - dealing with loads, and one function dealing with the push button.

We can not help you if you do not post your code.

I strongly recommend the use of the Bounce2 library as well.

I would guess, you have a 4 pin "tactile" button and have it connected wrong, post a drawing.

Now the code is working.

@cattledog, you can see the first code I posted, that the code I had problem with.

@JCA34F Yes for the tests I'm using tactile button and the connection was ok that I know by the response I got from the circuit.

The problem was the bounce - the code didn't solve it because it was wrong.
I tried to use 2 different libraries of bounce, I tried to use libraries of buttons, operating many loads with one button etc.

Thank you all :slight_smile:

Well done - although youre making life very complicated with debounce code when a simple cap will do it!

Can you edit the first post and add "SOLVED" to the title please?