EZ button with 4 buttons (1X4 pad) debounce function and multiple if statements don’t work

I drive LED string with Nano and control 3 parameters: frequency (4 values), power (4values), runtime (2 values). I’m using 1X4 keypad. Three buttons are used to enter 3 parameters, one button for each parameter. For example Button1 defines the frequency (period) and the parameter value changes with the number of presses. Button2 sets LED On -time and button 3 OFF-time. Once all parameters are entered Button4 is pressed to complete the entry of ALL parameters. I’m using EZ button program and multiple if statements.

Problems:

  1. Debounce function does not work. One button press periodically sets button’s counter to two instead of one for all buttons. The result seems to be the same for different debounce time. I don't think the problem is the keypad as I tried 4 different pads with the same result.
  2. If/else does not work properly. No matter how many times I press the button and what button count I get it always return the value of the parameter corresponding to the single press of the button.
#include <ezButton.h>
const int ledPin = 11;
ezButton button1(3);  // create Button object that attach to pin 3;
ezButton button2(2);  // create Button object that attach to pin 2;
ezButton button3(5);
ezButton button4(4);
int period = 0;
float power = 0;
float runtime = 0;
int ledon = 0;
int ledoff = 0;
int countt = 0;
void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  button1.setDebounceTime(65); // set debounce time to 50 milliseconds
  button2.setDebounceTime(55); // set debounce time to 50 milliseconds
  button3.setDebounceTime(55);
  button4.setDebounceTime(55);
  button1.setCountMode(COUNT_FALLING);
  button2.setCountMode(COUNT_FALLING);
  button3.setCountMode(COUNT_FALLING);
  button4.setCountMode(COUNT_FALLING);
}
void loop() {
  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  button3.loop();
  button4.loop();
  int btn1State = button1.getState();
  int btn2State = button2.getState();
  int btn3State = button3.getState();
  int btn4State = button4.getState();
  unsigned long btn1Count = button1.getCount();
  unsigned long btn2Count = button2.getCount();
  unsigned long btn3Count = button3.getCount();
  unsigned long btn4Count = button4.getCount();
  Serial.print("button 1 count: ");
  Serial.println(btn1Count);
  Serial.print("button 2 count: ");
  Serial.println(btn2Count);
  Serial.print("button 3 count: ");
  Serial.println(btn3Count);
  Serial.print("button 4 count: ");
  Serial.println(btn4Count);
  if (btn1Count %1  ==0);
  ) {
    period = 12500;
  }
  else if (btn1Count % == 2) {
    period = 10000;
  }
  else if (btn1Count % 3 == 0) {
    period = 4000;
  }
  else if (btn1Count % 4 == 0) {
    period = 2000;
  }

  if (btn2Count % 1 == 0) {
      power = 1;
  }
  else if (btn2Count % 2 == 0) {
      power = 0.8;
  }
  else if (btn2Count % 3 == 0) {
       power = 0.6;
  }
  else  if (btn2Count % 4 == 0) {
       power = 0.4;
  }
  else {
    delay (1);
  }

  if (btn3Count % 1 == 0) {
       runtime = 6000;
  }
  else  if (btn3Count % 2 == 0) {
       runtime = 8000;
  }
  else  if (btn3Count % 3 == 0) {
       runtime = 20000;
  }

  else if (btn3Count % 4 == 0) {
       runtime = 30000;
  }
  else {
    delay (1);
  }

  if ( btn4Count > 0) {
    ledon = period * power;
    ledoff = period * (1 - power);
    countt = runtime / period * 1000;
    Serial.print("period: ");
    Serial.println(period);
    Serial.print("power: ");
    Serial.println(power);
  }
  else {
    delay (1);
  }

  for (int x = 0; x < countt; x++) {
    if (x == countt - 1) {
      digitalWrite(ledPin, LOW);
      delay (10000);
    }
    else {
      digitalWrite(ledPin, HIGH);
      delayMicroseconds(ledon);
      digitalWrite(ledPin, LOW);
      delayMicroseconds(ledoff);
    }
  }
}
 if (btn1Count %1  ==0);

The only code that will be run conditionally is the semicolon

You have also made a complete mess of braces and brackets as can be seen if you Auto format the code in the IDE like this

#include <ezButton.h>
const int ledPin = 11;
ezButton button1(3);  // create Button object that attach to pin 2;
ezButton button2(2);  // create Button object that attach to pin 3;
ezButton button3(5);
ezButton button4(4);
int period = 0;
float power = 0;
float runtime = 0;
int ledon = 0;
int ledoff = 0;
int countt = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  button1.setDebounceTime(65); // set debounce time to 50 milliseconds
  button2.setDebounceTime(55); // set debounce time to 50 milliseconds
  button3.setDebounceTime(55);
  button4.setDebounceTime(55);
  button1.setCountMode(COUNT_FALLING);
  button2.setCountMode(COUNT_FALLING);
  button3.setCountMode(COUNT_FALLING);
  button4.setCountMode(COUNT_FALLING);
}
void loop()
{
  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  button3.loop();
  button4.loop();
  int btn1State = button1.getState();
  int btn2State = button2.getState();
  int btn3State = button3.getState();
  int btn4State = button4.getState();
  unsigned long btn1Count = button1.getCount();
  unsigned long btn2Count = button2.getCount();
  unsigned long btn3Count = button3.getCount();
  unsigned long btn4Count = button4.getCount();
  Serial.print("button 1 count: ");
  Serial.println(btn1Count);
  Serial.print("button 2 count: ");
  Serial.println(btn2Count);
  Serial.print("button 3 count: ");
  Serial.println(btn3Count);
  Serial.print("button 4 count: ");
  Serial.println(btn4Count);
  if (btn1Count % 1  == 0);
  )
  {
    period = 12500;
  }
  else if (btn1Count % == 2)
{
  period = 10000;
}
else if (btn1Count % 3 == 0)
{
  period = 4000;
}
else if (btn1Count % 4 == 0)
{
  period = 2000;
}
if (btn2Count % 1 == 0)
{
  power = 1;
}
else if (btn2Count % 2 == 0)
{
  power = 0.8;
}
else if (btn2Count % 3 == 0)
{
  power = 0.6;
}
else  if (btn2Count % 4 == 0)
{
  power = 0.4;
}
else
{
  delay (1);
  }
  if (btn3Count % 1 == 0)
{
  runtime = 6000;
}
else  if (btn3Count % 2 == 0)
{
  runtime = 8000;
}
else  if (btn3Count % 3 == 0)
{
  runtime = 20000;
}
else if (btn3Count % 4 == 0)
{
  runtime = 30000;
}
else
{
  delay (1);
  }
  if ( btn4Count > 0)
{
  ledon = period * power;
  ledoff = period * (1 - power);
    countt = runtime / period * 1000;
    Serial.print("period: ");
    Serial.println(period);
    Serial.print("power: ");
    Serial.println(power);
  }
  else
  {
    delay (1);
  }
  for (int x = 0; x < countt; x++)
{
  if (x == countt - 1)
    {
      digitalWrite(ledPin, LOW);
      delay (10000);
    }
    else
    {
      digitalWrite(ledPin, HIGH);
      delayMicroseconds(ledon);
      digitalWrite(ledPin, LOW);
      delayMicroseconds(ledoff);
    }
  }
}

Sorry I don't understand your comments. What does semicolon mean?
Do I use wrong brackets?

An if statement has code that is executed if the test is true, like this

if (this is true)
{
  //execute this code
}

but if you do this

if (this is true);  //<<<< note the semicolon
{
  //always execute this code
}

then the semicolon is the only code that depends on the test and the code in the code block will always be executed

In some places like this I can't understand what you are trying to do

  Serial.println(btn4Count);
  if (btn1Count % 1  == 0);
  )                                //what is this for ?
  {
    period = 12500;
  }

Why did you start a new thread? This is obvsly the same problem, same questions, same answers.

a7

I got the semicolon.
Depending on the btn count I want to enter different period values.
Serial.println(btn4Count) is to check if debounce work.

if (btn1Count % 1  == 0);
  )                                         // if btn1 pressed one time the program will assign 12500 
                                                   value  to the period.
  {
    period = 12500;
  }

I want to assign different period values depending on how many times the button is pressed.

So what is that ) doing there ?

Is this a duplicate topic as suggested by @alto777 ?

Yes. Its a duplicate topic per suggestion of @alto777 .

Then why did you create it ?

"Now you have real problems, so post the new code in a new post and we can get started… I’m prolly not the only one who stopped thinking about it after seeing the mistake.

a7"

I'm just following your suggestion.

Good grief. Sorry, I meant a new post on the existing thread rather than modifying an earlier posting in that thread.

a7

Not a new topic !

Because I followed the suggestion of @alto777 which made sense to me.

@alto777 Please post a link to the other topic

Do I just copy and paste the title of my previous topic or there is a way to post a link?

Sorry, I thought you meant new post.

I am going to close this topic

Please continue in the original one