Writing a code to count button presses on a seven segment counter it needs to wait 200ms before it can count as a press which I have written a function for as well as a function to display the code on the segment. however rather than displaying any numbers or letters it lights up random segments ignores the debounce completely. any advice you can give me to correct this would be appreciated.
const int latchPin = 7;
const int dataPin = 4;
const int clockPin = 8;
int button = 13;
int currentState = 1;
int previousState = 0;
int counter = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 200;
int buttonState;
int lastButtonState = LOW;
bool b;
int number_array[16] = // 0-9 A-F
{
B11111100,//0
B01100000,//1
B11011010,//2
B11110010,//3
B01100110,//4
B10110110,//5
B10111110,//6
B11100000,//7
B11111110,//8
B11100110,//9
B11101110,//A
B00111110,//B
B10011100,//C
B01111010,//D
B10011110,//E
B10001110,//F
};
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(button, INPUT);
debounce(button);
writeToRegister (counter);
}
void loop() {
debounce(button);
if(counter<16){
if(b) {counter++;}writeToShiftRegister(counter);}
else if (counter == 16) {counter = 0;}
}
void writeToRegister (byte data){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST,data);
digitalWrite(latchPin, HIGH);}
bool debounce(int pin){
boolean state;
boolean previousState;
unsigned char count;
if (b != digitalRead(pin)) {
for(count = 0; count<20 && b != digitalRead(pin); count++);
b=((count>=20 && b!=digitalRead(pin))?!b:b);
if(!b) {(millis()-lastDebounceTime > debounceDelay);{
lastDebounceTime = millis();
}}}}