Debounce on a Pushbutton

Hello,
i have a prewritten program and it involves a memory game that has 16 pushbuttons. each round round random buttons will light up that you must remember and then press. the first round is a sequence of 4 then 5 then 6. my question is when the buttons get pressed quickly they wont work. I believe it has to do with the debounce from what i have researched. Can someone help me locate this in the code please? I think I would start with just trying to lower the time that the program checks to see if the push was correct. I will post some of the code. (its divided in a number of libraries) If I am missing something let me know and I can post more.

``` byte programmingSequence[100]; int programmingSequence_length = 0; int programmingSequence_counter = 0;

extern void programmingSequence_onFinished();
extern void programmingSequence_onFailed();

void programmingSequence_add(int newNumber) {
programmingSequence[programmingSequence_length] = newNumber;
programmingSequence_length++;
}

void programmingSequence_clear() {
memset(programmingSequence, 0, programmingSequence_length);
programmingSequence_length = 0;
}

void programmingSequence_reset() {
programmingSequence_counter = 0;
}

void programmingSequence_checkNumber(int newNumber) {
// check btn correct //
if(programmingSequence[programmingSequence_counter] == newNumber) {
programmingSequence_counter++;

//  check last button correct  //
if(programmingSequence_counter == programmingSequence_length) {
  programmingSequence_onFinished();

  //  clear counter  //
  programmingSequence_counter = 0;
}

}

// check btn repeat //
else if(programmingSequence_counter > 0
&& programmingSequence[programmingSequence_counter - 1] == newNumber) {
}

// btn is not correct //
else {
// clear counter //
programmingSequence_counter = 0;

programmingSequence_onFailed();

}
}

bool programmingSequence_isNumberExist(int number) {
for(int i = 0; i < programmingSequence_length; i++) {
if(number == programmingSequence[i]) {
return true;
}
}

return false;
}


another library
#define BTNS_LEDS_COUNT 16

byte PINS_BTNS_LEDS[BTNS_LEDS_COUNT] = {6,8,16,17,5,9,7,19,11,10,12,2,3,18,4,20};

void btnsLeds_setOn(int ledIndex) {
digitalWrite(PINS_BTNS_LEDS[ledIndex], HIGH);
}

void btnsLeds_setOff(int ledIndex) {
digitalWrite(PINS_BTNS_LEDS[ledIndex], LOW);
}

void btnsLeds_clear() {
for(int i = 0; i < BTNS_LEDS_COUNT; i++) {
btnsLeds_setOff(i);
}
}

void btnsLeds_setOnAll() {
for(int i = 0; i < BTNS_LEDS_COUNT; i++) {
btnsLeds_setOn(i);
}
}

void btnsLeds_init() {
for(int i = 0; i < BTNS_LEDS_COUNT; i++) {
pinMode(PINS_BTNS_LEDS[i], OUTPUT);
}

btnsLeds_clear();

// delay(1000);
// for(int i = 0; i < BTNS_LEDS_COUNT; i++) {
// btnsLeds_setOn(i);
// delay(200);
// }
}


#include <ArdPin.h>
#include <ArdSensor.h>

#define BTNS_COUNT 16

int PINS_BTNS[BTNS_COUNT] = {36,38,31,30,35,39,37,28,41,40,42,32,33,29,34,27};

bool btns_lastValues[BTNS_COUNT];

ArdSensor* btnsList[BTNS_COUNT];

void btns_init() {
for(int i = 0; i < BTNS_COUNT; i++) {
btnsList[i] = new ArdSensor(PINS_BTNS[i], LOW, 50);
btns_lastValues[i] = 0;
}
}

extern void btns_onNewPressed(int index);

void btns_check() {
for(int i = 0; i < BTNS_COUNT; i++) {
btnsList[i]->check();
// check new //
bool current = btnsList[i]->isActivating();
if(current != btns_lastValues[i]) {
btns_lastValues[i] = current;
if (current == 1) {
btns_onNewPressed(i);
}
}
}
}

void btns_debug() {
for(int i = 0; i < BTNS_COUNT; i++) {
btnsList[i]->check();
Serial.println("Table reeds " + String(i) + ": " + String(btnsList[i]->isActivating())
+ ": " + String(btnsList[i]->getDirectValue()));
}

Serial.println();
}


#include <ArdSensor.h>
#include <ArdPin.h>

#define PIN_ADMIN_BTN_1 A0

ArdSensor *adminBtn1;

extern void adminBtn1_onPressed();

void admin_init() {
adminBtn1 = new ArdSensor(PIN_ADMIN_BTN_1);
adminBtn1->setCallbackOnActivated(adminBtn1_onPressed);
}

bool admin_check() {
adminBtn1->check();
}

bool admin_debug() {
adminBtn1->check();
Serial.println("Admin btn 1: " + String(adminBtn1->isActivating()));
}


#include "admin.h"
#include "box.h"
#include "btnLaser.h"
#include "btns.h"
#include "btnsLeds.h"
#include "cpz.h"
#include "hwSignals.h"
#include "laser.h"
#include "led.h"
#include "programmingSequence.h"
#include "randomNumber.h"

bool cpz_isDone = false;
bool activateMemmory_isDone = false;

//
// Cpz //
//

void cpz_onNewCount(int cpzCount) {
Serial.println("Cpz: Found: " + String(cpzCount));

// check all found //
if(cpzCount == CPZ_COUNT) {
Serial.println("Cpz: All Found");
cpz_isDone = true;
ledBtnLaser_on();
}else{
cpz_isDone = false;
ledBtnLaser_off();
laser_off();
}
}

//
// Btn laser //
//

void btnLaser_onPressed() {
Serial.println("btnLaser_onPressed");
laser_on();
}

void btnLaser_notPressed() {
Serial.println("btnLaser_notPressed");
laser_off();
}

//
// Btns Leds //
//

void btnsLeds_showTarget() {
for(int i = 0; i < programmingSequence_length; i++) {
btnsLeds_setOn(programmingSequence[i]);
delay(500);
btnsLeds_clear();
delay(200);
}
btnsLeds_clear();
}

void btnsLeds_showFailed() {
for(int j = 0; j < 5; j++) {
btnsLeds_setOnAll();
delay(100);
btnsLeds_clear();
delay(100);
}
}

void btnsLeds_showCorrect() {
for(int j = 0; j < 5; j++) {
btnsLeds_clear();
for(int i = 0; i < programmingSequence_length; i++) {
btnsLeds_setOn(programmingSequence[i]);
}
delay(100);
btnsLeds_clear();
delay(100);
}
}

//
// Game stage //
//

#define GAME_STAGE_MAX 4
int gameStage = 1;

void checkGameStage() {
// check game win //
if(gameStage == GAME_STAGE_MAX) {
Serial.println("Game: Win");

btnsLeds_setOnAll();
box_unlock();
led_on();

}

else {
Serial.println("Game: Stage " + String(gameStage));

//  set new values  //
programmingSequence_clear();
for(int i = 0; i < gameStage + 3 ; i++ ) {

// Serial.println("programmingSequence i: " + String(i));
// get rand number //
int randNumber = 0;
do {
randNumber = random_getNumber(1, 16);
}
while(programmingSequence_isNumberExist(randNumber) || randNumber == 0);

  Serial.println(randNumber);

  //  add to sequence  //
  programmingSequence_add(randNumber);
}

//  show it  //
btnsLeds_showTarget();

}
}

//
// Btns //
//

void btns_onNewPressed(int btnNumber) {
Serial.println("Btn pressed: " + String(btnNumber));

btnsLeds_setOn(btnNumber);
programmingSequence_checkNumber(btnNumber);
}

//
// Programming sequence //
//

void programmingSequence_onFailed() {
Serial.println("Sequence: Failed");

btnsLeds_showFailed();
delay(2000);

gameStage = 1;
checkGameStage();
}

void programmingSequence_onFinished() {
Serial.println("Sequence: Finished");

// show correct //
btnsLeds_showCorrect();
delay(2000);

gameStage++;
checkGameStage();
}

//
// Charger //
//

void signalChargerDone_onActivated(){
Serial.println("signalChargerDone_onActivated");
activateMemmory_isDone = true;
btnsLeds_setOnAll();
// for(int j = 0; j < 5; j++) {
// btnsLeds_setOnAll();
// delay(100);
// btnsLeds_clear();
// delay(100);
// }
}

//
// Admin //
//

void adminBtn1_onPressed() {
Serial.println("Admin btn 1");
btnsLeds_setOnAll();
box_unlock();
led_on();
}

void adminBtn2_onPressed() {
Serial.println("adminBtn2_onPressed");
laser_on();
}

void adminBtn2_notPressed() {
Serial.println("adminBtn2_notPressed");
laser_off();
}

//
// Main //
//

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

btns_init();
btnsLeds_init();
box_init();
random_init();
admin_init();
signalChargerDone_init();
led_init();

btnLaser_init();
cpz_init();
laser_init();

// checkGameStage();

Serial.println("Guardians_K4_Memmory_v3_Transistor");
delay(1000);
}

void loop() {
admin_check();

// transistor //
cpz_check();
if(cpz_isDone){
btnLaser_check();
}

// memmory //
if(activateMemmory_isDone){
if(gameStage < GAME_STAGE_MAX) {
btns_check();
}
}
if(!activateMemmory_isDone){
signalChargerDone_check();
}
}

Welcome to the forum

It would probably be more helpful to see the code that reads the buttons

Post a link to where you got this alleged pre-written code. We would prefer to see the entire sketch that you are working with.

Post code better.

Use the IDE Auto Format tool. Then use the IDE Copy for Forum tool, and come back here and paste it.

It should end up looking like code:

void setup() {

}

void loop() {

}

It's hard to read what code you did post already, but the frequent appearance of delay() as a timing method may mean the system is only going to seem sluggish to some ppl.

a7

hue001t

Welcome to the best Arduino forum ever :slight_smile:

When I took a quick look at the unformatted sketch for the first time, I discovered some calls to the delay() function.
In general, delay() functions and actions with buttons are not friends.
My recommendation: Rethink the programme design.

p.s.

Line  56:     delay(500);
	Line  58:     delay(200);
	Line  66:     delay(100);
	Line  68:     delay(100);
	Line  78:     delay(100);
	Line  80:     delay(100);
	Line 144:   delay(2000);
	Line 155:   delay(2000);
	Line 171:   //    delay(100);
	Line 173:   //    delay(100);
	Line 220:   delay(1000);

Is this better?
sorry I have never posted before.

#include "admin.h"
#include "box.h"
#include "btnLaser.h"
#include "btns.h"
#include "btnsLeds.h"
#include "cpz.h"
#include "hwSignals.h"
#include "laser.h"
#include "led.h"
#include "programmingSequence.h"
#include "randomNumber.h"

bool cpz_isDone = false;
bool activateMemmory_isDone = false;

//
//  Cpz  //
//

void cpz_onNewCount(int cpzCount) {
  Serial.println("Cpz: Found: " + String(cpzCount));

  //  check all found  //
  if (cpzCount == CPZ_COUNT) {
    Serial.println("Cpz: All Found");
    cpz_isDone = true;
    ledBtnLaser_on();
  } else {
    cpz_isDone = false;
    ledBtnLaser_off();
    laser_off();
  }
}

//
//  Btn laser  //
//

void btnLaser_onPressed() {
  Serial.println("btnLaser_onPressed");
  laser_on();
}

void btnLaser_notPressed() {
  Serial.println("btnLaser_notPressed");
  laser_off();
}

//
//  Btns Leds  //
//

void btnsLeds_showTarget() {
  for (int i = 0; i < programmingSequence_length; i++) {
    btnsLeds_setOn(programmingSequence[i]);
    delay(500);
    btnsLeds_clear();
    delay(200);
  }
  btnsLeds_clear();
}

void btnsLeds_showFailed() {
  for (int j = 0; j < 5; j++) {
    btnsLeds_setOnAll();
    delay(100);
    btnsLeds_clear();
    delay(100);
  }
}

void btnsLeds_showCorrect() {
  for (int j = 0; j < 5; j++) {
    btnsLeds_clear();
    for (int i = 0; i < programmingSequence_length; i++) {
      btnsLeds_setOn(programmingSequence[i]);
    }
    delay(100);
    btnsLeds_clear();
    delay(100);
  }
}

//
//  Game stage  //
//

#define GAME_STAGE_MAX 4
int gameStage = 1;

void checkGameStage() {
  //  check game win  //
  if (gameStage == GAME_STAGE_MAX) {
    Serial.println("Game: Win");

    btnsLeds_setOnAll();
    box_unlock();
    led_on();
  }

  else {
    Serial.println("Game: Stage " + String(gameStage));

    //  set new values  //
    programmingSequence_clear();
    for (int i = 0; i < gameStage + 3; i++) {
      //      Serial.println("programmingSequence i: " + String(i));
      //  get rand number  //
      int randNumber = 0;
      do {
        randNumber = random_getNumber(1, 16);
      } while (programmingSequence_isNumberExist(randNumber) || randNumber == 0);

      Serial.println(randNumber);

      //  add to sequence  //
      programmingSequence_add(randNumber);
    }

    //  show it  //
    btnsLeds_showTarget();
  }
}

//
//  Btns  //
//

void btns_onNewPressed(int btnNumber) {
  Serial.println("Btn pressed: " + String(btnNumber));

  btnsLeds_setOn(btnNumber);
  programmingSequence_checkNumber(btnNumber);
}

//
//  Programming sequence  //
//

void programmingSequence_onFailed() {
  Serial.println("Sequence: Failed");

  btnsLeds_showFailed();
  delay(2000);

  gameStage = 1;
  checkGameStage();
}

void programmingSequence_onFinished() {
  Serial.println("Sequence: Finished");

  //  show correct  //
  btnsLeds_showCorrect();
  delay(2000);

  gameStage++;
  checkGameStage();
}

//
//  Charger  //
//

void signalChargerDone_onActivated() {
  Serial.println("signalChargerDone_onActivated");
  activateMemmory_isDone = true;
  btnsLeds_setOnAll();
  //  for(int j = 0; j < 5; j++) {
  //    btnsLeds_setOnAll();
  //    delay(100);
  //    btnsLeds_clear();
  //    delay(100);
  //  }
}

//
//  Admin  //
//

void adminBtn1_onPressed() {
  Serial.println("Admin btn 1");
  btnsLeds_setOnAll();
  box_unlock();
  led_on();
}

void adminBtn2_onPressed() {
  Serial.println("adminBtn2_onPressed");
  laser_on();
}

void adminBtn2_notPressed() {
  Serial.println("adminBtn2_notPressed");
  laser_off();
}

//
//  Main  //
//

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

  btns_init();
  btnsLeds_init();
  box_init();
  random_init();
  admin_init();
  signalChargerDone_init();
  led_init();

  btnLaser_init();
  cpz_init();
  laser_init();

  //  checkGameStage();

  Serial.println("Guardians_K4_Memmory_v3_Transistor");
  delay(1000);
}

void loop() {
  admin_check();

  // transistor //
  cpz_check();
  if (cpz_isDone) {
    btnLaser_check();
  }

  // memmory //
  if (activateMemmory_isDone) {
    if (gameStage < GAME_STAGE_MAX) {
      btns_check();
    }
  }
  if (!activateMemmory_isDone) {
    signalChargerDone_check();
  }
}

i posted it. should I click each of the libraries in the ide and do the same thing?

Can you provide the link to where this program comes from.

Where do the libraries come from? Many are not in the library manager.

Maybe. If you can't lead us to the code in libraries that are not commonly used.

Post a link to where you got this alleged pre-written code.

a7

it is part of a puzzle and so i just received the code on a usb. I can copy each library onto here if that helps?

this is most of it. I left out the libraries for other functions as far as i can tell.

#include "admin.h"
#include "box.h"
#include "btnLaser.h"
#include "btns.h"
#include "btnsLeds.h"
#include "cpz.h"
#include "hwSignals.h"
#include "laser.h"
#include "led.h"
#include "programmingSequence.h"
#include "randomNumber.h"

bool cpz_isDone = false;
bool activateMemmory_isDone = false;

//
//  Cpz  //
//

void cpz_onNewCount(int cpzCount) {
  Serial.println("Cpz: Found: " + String(cpzCount));

  //  check all found  //
  if (cpzCount == CPZ_COUNT) {
    Serial.println("Cpz: All Found");
    cpz_isDone = true;
    ledBtnLaser_on();
  } else {
    cpz_isDone = false;
    ledBtnLaser_off();
    laser_off();
  }
}

//
//  Btn laser  //
//

void btnLaser_onPressed() {
  Serial.println("btnLaser_onPressed");
  laser_on();
}

void btnLaser_notPressed() {
  Serial.println("btnLaser_notPressed");
  laser_off();
}

//
//  Btns Leds  //
//

void btnsLeds_showTarget() {
  for (int i = 0; i < programmingSequence_length; i++) {
    btnsLeds_setOn(programmingSequence[i]);
    delay(500);
    btnsLeds_clear();
    delay(200);
  }
  btnsLeds_clear();
}

void btnsLeds_showFailed() {
  for (int j = 0; j < 5; j++) {
    btnsLeds_setOnAll();
    delay(100);
    btnsLeds_clear();
    delay(100);
  }
}

void btnsLeds_showCorrect() {
  for (int j = 0; j < 5; j++) {
    btnsLeds_clear();
    for (int i = 0; i < programmingSequence_length; i++) {
      btnsLeds_setOn(programmingSequence[i]);
    }
    delay(100);
    btnsLeds_clear();
    delay(100);
  }
}

//
//  Game stage  //
//

#define GAME_STAGE_MAX 4
int gameStage = 1;

void checkGameStage() {
  //  check game win  //
  if (gameStage == GAME_STAGE_MAX) {
    Serial.println("Game: Win");

    btnsLeds_setOnAll();
    box_unlock();
    led_on();
  }

  else {
    Serial.println("Game: Stage " + String(gameStage));

    //  set new values  //
    programmingSequence_clear();
    for (int i = 0; i < gameStage + 3; i++) {
      //      Serial.println("programmingSequence i: " + String(i));
      //  get rand number  //
      int randNumber = 0;
      do {
        randNumber = random_getNumber(1, 16);
      } while (programmingSequence_isNumberExist(randNumber) || randNumber == 0);

      Serial.println(randNumber);

      //  add to sequence  //
      programmingSequence_add(randNumber);
    }

    //  show it  //
    btnsLeds_showTarget();
  }
}

//
//  Btns  //
//

void btns_onNewPressed(int btnNumber) {
  Serial.println("Btn pressed: " + String(btnNumber));

  btnsLeds_setOn(btnNumber);
  programmingSequence_checkNumber(btnNumber);
}

//
//  Programming sequence  //
//

void programmingSequence_onFailed() {
  Serial.println("Sequence: Failed");

  btnsLeds_showFailed();
  delay(2000);

  gameStage = 1;
  checkGameStage();
}

void programmingSequence_onFinished() {
  Serial.println("Sequence: Finished");

  //  show correct  //
  btnsLeds_showCorrect();
  delay(2000);

  gameStage++;
  checkGameStage();
}

//
//  Charger  //
//

void signalChargerDone_onActivated() {
  Serial.println("signalChargerDone_onActivated");
  activateMemmory_isDone = true;
  btnsLeds_setOnAll();
  //  for(int j = 0; j < 5; j++) {
  //    btnsLeds_setOnAll();
  //    delay(100);
  //    btnsLeds_clear();
  //    delay(100);
  //  }
}

//
//  Admin  //
//

void adminBtn1_onPressed() {
  Serial.println("Admin btn 1");
  btnsLeds_setOnAll();
  box_unlock();
  led_on();
}

void adminBtn2_onPressed() {
  Serial.println("adminBtn2_onPressed");
  laser_on();
}

void adminBtn2_notPressed() {
  Serial.println("adminBtn2_notPressed");
  laser_off();
}

//
//  Main  //
//

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

  btns_init();
  btnsLeds_init();
  box_init();
  random_init();
  admin_init();
  signalChargerDone_init();
  led_init();

  btnLaser_init();
  cpz_init();
  laser_init();

  //  checkGameStage();

  Serial.println("Guardians_K4_Memmory_v3_Transistor");
  delay(1000);
}

void loop() {
  admin_check();

  // transistor //
  cpz_check();
  if (cpz_isDone) {
    btnLaser_check();
  }

  // memmory //
  if (activateMemmory_isDone) {
    if (gameStage < GAME_STAGE_MAX) {
      btns_check();
    }
  }
  if (!activateMemmory_isDone) {
    signalChargerDone_check();
  }
}

#include <ArdSensor.h>
#include <ArdPin.h>

#define PIN_ADMIN_BTN_1   A0
#define PIN_ADMIN_BTN_2   A1

ArdSensor *adminBtn1;
ArdSensor *adminBtn2;

extern void adminBtn1_onPressed();
extern void adminBtn2_onPressed();
extern void adminBtn2_notPressed();

void admin_init() {
  adminBtn1 = new ArdSensor(PIN_ADMIN_BTN_1, LOW, 50);
  adminBtn1->setCallbackOnActivated(adminBtn1_onPressed);
  
  adminBtn2 = new ArdSensor(PIN_ADMIN_BTN_2, LOW, 50);
  adminBtn2->setCallbackOnActivated(adminBtn2_onPressed);
  adminBtn2->setCallbackOnDeactivated(adminBtn2_notPressed);
}

void admin_check() {
  adminBtn1->check();
  adminBtn2->check();
}

bool admin_debug() {
  adminBtn1->check();
  Serial.println("Admin btn 1: " + String(adminBtn1->isActivating()));
}

#define PIN_RELAY_BOX  A8

void box_lock() {
  digitalWrite(PIN_RELAY_BOX, HIGH);
}

void box_unlock() {
  digitalWrite(PIN_RELAY_BOX, LOW);
}

void box_init() {
  pinMode(PIN_RELAY_BOX, OUTPUT);
  box_lock();
}

#include <ArdPin.h>
#include <ArdSensor.h>

#define BTNS_COUNT 16

int PINS_BTNS[BTNS_COUNT] = {36,38,31,30,35,39,37,28,41,40,42,32,33,29,34,27};
                             
bool btns_lastValues[BTNS_COUNT];

ArdSensor* btnsList[BTNS_COUNT];

void btns_init() {
  for(int i = 0; i < BTNS_COUNT; i++) {
    btnsList[i] = new ArdSensor(PINS_BTNS[i], LOW, 50);  
    btns_lastValues[i] = 0;
  }
}

extern void btns_onNewPressed(int index);

void btns_check() {
  for(int i = 0; i < BTNS_COUNT; i++) {
    btnsList[i]->check();
    //  check new  //
    bool current = btnsList[i]->isActivating();
    if(current != btns_lastValues[i]) {
      btns_lastValues[i] = current;
      if (current == 1) {
        btns_onNewPressed(i);
      }
    }
  }
}

void btns_debug() {
  for(int i = 0; i < BTNS_COUNT; i++) {
    btnsList[i]->check();
    Serial.println("Table reeds " + String(i) + ": " + String(btnsList[i]->isActivating())
      + ": " + String(btnsList[i]->getDirectValue()));
  }

  Serial.println();
}

#define BTNS_LEDS_COUNT 16

byte PINS_BTNS_LEDS[BTNS_LEDS_COUNT] = {6,8,16,17,5,9,7,19,11,10,12,2,3,18,4,20};

void btnsLeds_setOn(int ledIndex) {
  digitalWrite(PINS_BTNS_LEDS[ledIndex], HIGH);
}

void btnsLeds_setOff(int ledIndex) {
  digitalWrite(PINS_BTNS_LEDS[ledIndex], LOW);
}

void btnsLeds_clear() {
  for(int i = 0; i < BTNS_LEDS_COUNT; i++) {
    btnsLeds_setOff(i);
  }
}

void btnsLeds_setOnAll() {
  for(int i = 0; i < BTNS_LEDS_COUNT; i++) {
    btnsLeds_setOn(i);
  }
}

void btnsLeds_init() {
  for(int i = 0; i < BTNS_LEDS_COUNT; i++) {
    pinMode(PINS_BTNS_LEDS[i], OUTPUT);
  }

  btnsLeds_clear();

//  delay(1000);
//  for(int i = 0; i < BTNS_LEDS_COUNT; i++) {
//    btnsLeds_setOn(i);
//    delay(200);
//  }
}




#include <OneWire.h>
#include <ArdCPZ2L.h>

#define CPZ_COUNT 8
int CPZ_PINS[] = {43,44,45,46,47,48,50,51};

byte BEACONS_SERIAL1[][6] = {{0x4E, 0x78, 0x17, 0x00, 0x0E, 0x00}, 
                             {0x7D, 0xE2, 0x17, 0x00, 0x0E, 0x00},
                             {0xC1, 0xDA, 0x17, 0x00, 0x0E, 0x00},
                             {0x49, 0x9A, 0x17, 0x00, 0x0E, 0x00},
                             {0x10, 0xB6, 0x17, 0x00, 0x0E, 0x00},
                             {0xBE, 0xF4, 0x85, 0x00, 0x0D, 0x00},
                             {0xAF, 0xBA, 0x17, 0x00, 0x0E, 0x00},
                             {0xB3, 0x90, 0x17, 0x00, 0x0E, 0x00}};

byte BEACONS_SERIAL2[][6] = {{0x5D, 0xEE, 0x17, 0x00, 0x0E, 0x00}, 
                             {0xC6, 0x8E, 0x17, 0x00, 0x0E, 0x00},
                             {0xB6, 0x9C, 0x17, 0x00, 0x0E, 0x00},
                             {0x62, 0xD9, 0x17, 0x00, 0x0E, 0x00},
                             {0x9C, 0xA6, 0x17, 0x00, 0x0E, 0x00},
                             {0xF2, 0xD1, 0x17, 0x00, 0x0E, 0x00},
                             {0xC1, 0xEC, 0x17, 0x00, 0x0E, 0x00},
                             {0x45, 0xF1, 0x85, 0x00, 0x0D, 0x00}};

//#define CPZ_COUNT 1
//int CPZ_PINS[] = {8};
//
//byte BEACONS_SERIAL1[][6] = {{0xBD, 0x2D, 0x23, 0x00, 0x81, 0x00}};
//
//byte BEACONS_SERIAL2[][6] = {{0xC0, 0xC1, 0x38, 0x00, 0x81, 0x00}};
                                  
ArdCPZ2L* cpzList[CPZ_COUNT];

extern void cpz_onNewCount(int cpzCount); 

void cpz_init() {
  //  create cpz  //
  for(int i = 0; i < CPZ_COUNT; i++) {
    cpzList[i] = new ArdCPZ2L(CPZ_PINS[i]);
  }
}

int cpz_getCorrectCount()
{
  int correctCounter = 0;
  
  //  check all beacons  //
  for(int i = 0; i < CPZ_COUNT; i++) {
    cpzList[i]->check();
  }

  //  check all neccessary beacons found  //
  for(int i = 0; i < CPZ_COUNT; i++) {
    //  beacon found  //
    if(cpzList[i]->isBeaconConnecting()) {
      //  beacon correct  //
      if(cpzList[i]->isAddress(BEACONS_SERIAL1[i])
      || cpzList[i]->isAddress(BEACONS_SERIAL2[i])) {
        correctCounter++;
      }
      //  beacon not correct  //
      else {
        continue;
      }
    }
    //  beacon not found  //
    else {
      continue;
    }
  }

  return correctCounter;
}

int cpz_lastCount = 0;
void cpz_check() {
  //  get number  //
  int cpzCount = cpz_getCorrectCount();

  //  check new  //
  if(cpzCount != cpz_lastCount) {
    cpz_onNewCount(cpzCount);

    //  save it  //
    cpz_lastCount = cpzCount;
  }
}


byte programmingSequence[100];
int programmingSequence_length = 0;
int programmingSequence_counter = 0;

extern void programmingSequence_onFinished();
extern void programmingSequence_onFailed();

void programmingSequence_add(int newNumber) {
  programmingSequence[programmingSequence_length] = newNumber;
  programmingSequence_length++;
}

void programmingSequence_clear() {
  memset(programmingSequence, 0, programmingSequence_length);
  programmingSequence_length = 0;
}

void programmingSequence_reset() {
  programmingSequence_counter = 0;
}

void programmingSequence_checkNumber(int newNumber) {
  //  check btn correct  //
  if(programmingSequence[programmingSequence_counter] == newNumber) {
    programmingSequence_counter++;

    //  check last button correct  //
    if(programmingSequence_counter == programmingSequence_length) {
      programmingSequence_onFinished();

      //  clear counter  //
      programmingSequence_counter = 0;
    }
  }

  //  check btn repeat  //
  else if(programmingSequence_counter > 0
  && programmingSequence[programmingSequence_counter - 1] == newNumber) {
  }

  //  btn is not correct  //
  else {
    //  clear counter  //
    programmingSequence_counter = 0;

    programmingSequence_onFailed();
  }
}

bool programmingSequence_isNumberExist(int number) {
  for(int i = 0; i < programmingSequence_length; i++) {
    if(number == programmingSequence[i]) {
      return true;
    }
  }

  return false;
}


void random_init() {
  randomSeed(analogRead(5));
}

int random_getNumber(int startValue, int stopValue) {
  return random(startValue, stopValue);
}


No, I don't think it will help. The code is so unusual and library dependent that the chances of any of anyone on this board taking the time to sort it out is low.

Good luck.

i posted most of it down below now. TY for the reply.

Thank you so much for the reply. Yea it was written in Europe so maybe that is why it is different?

Use a 100nF capacitor for the buttons (parallel to the button) instead of debounce algorithm. Also use pullup functionality.

i really appreciate the feedback. right now hardware wise would be tough to change because of the board everything is in. But in the future I will definitely consider this. It is also possible the current hardware is causing the debounce that's why i posted so hopefully someone can just point me to where if any the debounce actions are taking place so i know what to look for and alter.

This is the extent of a button-debounce without a library (using a library makes this even shorter):

  1. Read button
  2. If state button has changed
  3. record the state change time and
  4. update the state
  5. If the recorded time is longer than the debounce timeout
  6. AND current button state is PRESSED
  7. AND last button state was NOT-PRESSED
  8. A button was pressed and released.
void readButton() {
  currentButtonRead = digitalRead(buttonPin);  // read button pin
  if (currentButtonRead != lastButtonRead) {   // if button pin reading changes...
    timer = millis();                          // ...start a timer
    lastButtonRead = currentButtonRead;        // ... and store current state
  }

  if ((millis() - timer) > timeout) {                           // if button read change was longer than timeout
    if (currentButtonState == HIGH && lastButtonRead == LOW) {  // ... and State NOT pressed while Button PRESSED

        // CALL YOUR FUNCTION

      }
    }
    currentButtonState = currentButtonRead;  // change the state
  }
}

Buttons read here...

... BUT I see no debounce.

Make a simple main program that calls the button read function which includes a debounce function.

I see what you are saying. I wasn't sure if there was a debounce in the code and that what i was asking for really. I have this program for a puzzle and i am trying to figure out why the buttons on this puzzle don't accept if you hit the button to fast. mechanically the button is getting pushed. so I was first told maybe the buttons debounce. So i wanted to see if there was a debounce function but I think what you are saying is that there is not a reason in the code for this.

You should always debounce a press-once button. If your button is "press-and-hold," you probably do not need a debounce.

Please review reply # 4.