so im getting this unnecessary delay with my debounce. so i have debounced 3 buttons which worked fine looking at the serial monitor but as i combined in with another code i've sensed a delay between the debounce and it feels less sensitive. like i have to press it for longer in order for message that iv set up to say 'debouncing ' to show up on the serial monitor. i looked to see if mistakenly duplicated variables but couldnt seem to find any
#define notPRESSED 0
#define partialPRESS 1
#define normalPRESS 2
#define B1pressed !(PINC & B1)
#define B1released !B1pressed
#define B2pressed !(PINC & B2)
#define B2released !B2pressed
#define B3pressed !(PINC & B3)
#define B3released !B3pressed
#define debounce 500 // debounce duration
#define B1 B00000001
#define B2 B00000010
#define B3 B00000100
#define NUMB0_SEG B00111111
#define NUMB1_SEG B00000110
#define NUMB2_SEG B01011011
#define NUMB3_SEG B01001111
#define NUMB4_SEG B01100110
#define NUMB5_SEG B01101101
#define NUMB6_SEG B01111101
#define NUMB7_SEG B00000111
#define NUMB8_SEG B01111111
#define NUMB9_SEG B01100111
#define LETSEG_A B00111111
#define LETSEG_B B01111100
#define LETSEG_C B00111001
#define LETSEG_D B01011110
#define LETSEG_E B01111001
#define LETSEG_F B01110001
#define HB_PORTB PORTB
#define HB_DDRB DDRB
#define HB_PORTB_ST PORTC
#define HB_DDRB_ST DDRC
#define HB_DS_POS PB0 //Data pin (DS) pin location
#define HB_SH_CP_POS PB4 //Shift Clock (SH_CP) pin location
#define HB_ST_CP_POS PC3 //Store Clock (ST_CP) pin location
#define HBDataHigh() (HB_PORTB|=(1<<HB_DS_POS))
#define HBDataLow() (HB_PORTB&=(~(1<<HB_DS_POS)))
void HBInit()
{
//Make the Data(DS), Shift clock (SH_CP), Store Clock (ST_CP) lines output
HB_DDRB |= ((1 << HB_SH_CP_POS) | (1 << HB_DS_POS));
HB_DDRB_ST |= ((1 << HB_ST_CP_POS));
}
//Low level macros to change data (DS)lines
//Sends a clock pulse on SH_CP line
void HBPulse()
{
//Pulse the Shift Clock
HB_PORTB |= (1 << HB_SH_CP_POS); //HIGH
HB_PORTB &= (~(1 << HB_SH_CP_POS)); //LOW
}
//Sends a clock pulse on ST_CP line
void HBLatch()
{
//Pulse the Store Clock
HB_PORTB_ST |= (1 << HB_ST_CP_POS); //HIGH
_delay_loop_1(1);
HB_PORTB_ST &= (~(1 << HB_ST_CP_POS)); //LOW
_delay_loop_1(1);
}
void HBWrite(uint8_t data)
{
//Send each 8 bits serially
//Order is MSB first
for (uint8_t i = 0; i < 8; i++)
{
//Output the data on DS line according to the
//Value of MSB
if (data & B10000000)
{
//MSB is 1 so output high
HBDataHigh();
}
else
{
//MSB is 0 so output high
HBDataLow();
}
HBPulse(); //Pulse the Clock line
data = data << 1; //Now bring next bit at MSB position
}
//Now all 8 bits have been transferred to shift register
//Move them to output latch at one
HBLatch();
}
//module0 variables
bool init_module0_clock;
//module1 variables
bool init_module1_clock;
//module2 variables
bool init_module2_clock;
unsigned char B1_state, B2_state, B3_state; // buttons state variables
void setup() {
HBInit();
DDRC &= ~B1;
PORTC |= B1;
DDRC &= ~B2;
PORTC |= B2;
DDRC &= ~B3;
PORTC |= B3;
init_module0_clock = true;
init_module1_clock = true;
init_module2_clock = true;
B1_state = notPRESSED;
B2_state = notPRESSED;
B3_state = notPRESSED;
Serial.begin(9600);
}
void loop() {
HBWrite(B10000000); //Write the data to HB
_delay_ms(500);
HBWrite(B00000000); //Write the data to HB
_delay_ms(500);
{ // module 0
static unsigned long module_time, module_delay, debounce_count;
static bool module_doStep;
static unsigned char state; // state variable for module 0
if (init_module0_clock) {
module_delay = 17;
module_time = millis();
module_doStep = false;
init_module0_clock = false;
state = 0;
}
else {
unsigned long m = millis();
if ( ( (long)(m - module_time) ) > module_delay ) {
module_time = m; module_doStep = true;
}
else module_doStep = false;
}
if (module_doStep) {
switch (state) {
case 0:
B1_state = notPRESSED;
if (B1released) state = 0;
else {
debounce_count = module_time;
state = 1;
}
break;
case 1:
B1_state = partialPRESS;
if (B1released) state = 0;
else if ((long)(millis() - debounce_count) < debounce) state = 1;
else state = 2;
break;
case 2:
B1_state = normalPRESS;
if (B1released) state = 0;
else state = 2;
break;
default: state = 0; break;
}
}
}
{ // module 1
static unsigned long module_time, module_delay, debounce_count;
static bool module_doStep;
static unsigned char state; // state variable for module 1
if (init_module1_clock) {
module_delay = 17;
module_time = millis();
module_doStep = false;
init_module1_clock = false;
state = 0;
}
else {
unsigned long m = millis();
if ( ( (long)(m - module_time) ) > module_delay ) {
module_time = m; module_doStep = true;
}
else module_doStep = false;
}
if (module_doStep) {
switch (state) {
case 0:
B2_state = notPRESSED;
if (B2released) state = 0;
else {
debounce_count = module_time;
state = 1;
}
break;
case 1:
B2_state = partialPRESS;
if (B2released) state = 0;
else if ((long)(millis() - debounce_count) < debounce) state = 1;
else state = 2;
break;
case 2:
B2_state = normalPRESS;
if (B2released) state = 0;
else state = 2;
break;
default: state = 0; break;
}
}
}
{ // module 2
static unsigned long module_time, module_delay, debounce_count;
static bool module_doStep;
static unsigned char state; // state variable for module 2
if (init_module2_clock) {
module_delay = 17;
module_time = millis();
module_doStep = false;
init_module2_clock = false;
state = 0;
}
else {
unsigned long m = millis();
if ( ( (long)(m - module_time) ) > module_delay ) {
module_time = m; module_doStep = true;
}
else module_doStep = false;
}
if (module_doStep) {
switch (state) {
case 0:
B3_state = notPRESSED;
if (B3released) state = 0;
else {
debounce_count = module_time;
state = 1;
}
break;
case 1:
B3_state = partialPRESS;
if (B3released) state = 0;
else if ((long)(millis() - debounce_count) < debounce) state = 1;
else state = 2;
break;
case 2:
B3_state = normalPRESS;
if (B3released) state = 0;
else state = 2;
break;
default: state = 0; break;
}
}
}
{
static char old;
if (old != B1_state) {
Serial.print("B1 = "); Serial.println(B1_state ? ((B1_state == 2) ? "pressed" : "de-bouncing") : "not pressed");
old = B1_state;
}
}
{
static char old;
if (old != B2_state) {
Serial.print("B2 = "); Serial.println(B2_state ? ((B2_state == 2) ? "pressed" : "de-bouncing") : "not pressed");
old = B2_state;
}
}
{
static char old;
if (old != B3_state) {
Serial.print("B3 = "); Serial.println(B3_state ? ((B3_state == 2) ? "pressed" : "de-bouncing") : "not pressed");
old = B3_state;
}
}
}