For anyone paying attention, this is a continuation of "buttoning up this button code. I started a new topic as I now have a different problem. How to best de-bounce this button code?
//Button Incrementor
volatile int buttonPinOn = 1; //arduino 2 // the pin that the ON pushbutton is attached to
volatile int buttonPinOff = 0; //arduino 3 // the pin that the OFF pushbutton is attached to
// Variables will change:
volatile long int buttonPushCounterOn = 0; // counter for the number of button presses
volatile long int buttonPushCounterOff = 0; // counter for the number of button presses
int buttonStateOn = 0; // current state of the button
int buttonStateOff = 0; // current state of the button
int lastButtonStateOn = 0; // previous state of the button
int lastButtonStateOff = 0; // previous state of the button
// include the library code:
#include <LiquidCrystal.h>;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd (7,8,9,10,11,12); //11RS,12E,13D4,14D5,15D6,16D7
void setup(){
pinMode(buttonPinOn, INPUT);
pinMode(buttonPinOff, INPUT);
lcd.begin(16,2); // set up the LCD's number of columns and rows:
lcd.clear();
lcd.setCursor(2,1);
lcd.print("-S.ON");
lcd.setCursor(10,1);
lcd.print("-M.OFF");
attachInterrupt(buttonPinOn, isr1 ,RISING);
attachInterrupt(buttonPinOff, isr2 ,RISING);
}
void isr1()
{
buttonPushCounterOn+=1;
}
void isr2()
{
buttonPushCounterOff+=1;
}
void loop(){
// read the pushbutton ON input pin:
buttonStateOn = digitalRead(buttonPinOn);
// compare the buttonState to its previous state
if (buttonStateOn != lastButtonStateOn)
// if the state has changed, increment the counter
if (buttonStateOn == HIGH)
delay(300);
lastButtonStateOn = buttonStateOn; // save the current state as the last state, for next time through the loop
if(buttonPushCounterOn > 60)buttonPushCounterOn =0; // rollover after 60.
// read the pushbutton OFF input pin:
buttonStateOff = digitalRead(buttonPinOff);
// compare the buttonState to its previous state
if (buttonStateOff != lastButtonStateOff)
// if the state has changed, increment the counter
if (buttonStateOff == HIGH)
delay(100);
lastButtonStateOff = buttonStateOff; // save the current state as the last state, for next time through the loop
if(buttonPushCounterOff > 60)buttonPushCounterOff =0; // rollover after 60.
lcd.setCursor(8,1);
lcd.print(buttonPushCounterOff,DEC);
delay(200);
lcd.setCursor(0,1);
lcd.print(buttonPushCounterOn,DEC);
delay(200);
}
That doesn't exactly answer the question. If you're using polling to read the button state, why are you also using interrupts to read the button state? Which method is it that requires debouncing?
boolean lastButtonOn = LOW;
boolean currentButtonOn = LOW;
boolean lastButtonOff = LOW;
boolean currentButtonOff = LOW;
volatile int buttonPinOn = 0; //arduino 2 // the pin that the ON pushbutton is attached to
volatile int buttonPinOff = 1; //arduino 3 // the pin that the OFF pushbutton is attached to
void setup() //start of the setup() function
//where is the code for setup() ?
boolean debounce1(boolean last) //start of the debounce1() function
{
boolean current = digitalRead(buttonPinOn);
if (last != current); //semi-colons on the end of if stops them working
} //is this really the end of the debounce1 function ?
{ //what is this the start of ?
delay(5);
return current;
}
boolean debounce2(boolean last)
{
boolean current = digitalRead(buttonPinOff);
if (last != current);
}
{
delay(5);
return current;
}
{ //this is the body of the setup() function
pinMode(buttonPinOn, INPUT);
pinMode(buttonPinOff, INPUT);
}
void loop(){
currentButtonOn= debounce1(lastbuttonOn); //wrong variable name
currentButtonOff= debounce2(lastbuttonOff); //wrong variable name
if (lastButtonOn==LOW&¤tButtonOn==HIGH);
if (lastButtonOff==LOW&¤tButtonOff==HIGH);
lastButtonOn= currentButtonOn;
lastButtonOff= currentButtonOff;
}
Your code with amended so that it at least compiles
boolean lastButtonOn = LOW;
boolean currentButtonOn = LOW;
boolean lastButtonOff = LOW;
boolean currentButtonOff = LOW;
volatile int buttonPinOn = 0; //arduino 2 // the pin that the ON pushbutton is attached to
volatile int buttonPinOff = 1; //arduino 3 // the pin that the OFF pushbutton is attached to
boolean debounce1(boolean last) //start of the debounce1() function
{
boolean current = digitalRead(buttonPinOn);
if (last != current) //removed semi colon
//} //commented out
{ //start of code for if == true
delay(5);
return current;
} //end of code for if == true
} //end of function
boolean debounce2(boolean last)
{
boolean current = digitalRead(buttonPinOff);
if (last != current) //removed semi colon
//} //commented out
{ //start of code for if == true
delay(5);
return current;
} //end of code for if == true
} //end of function
void setup() //start of the setup() function
//now the code for setup()
{ //this is the body of the setup() function
pinMode(buttonPinOn, INPUT);
pinMode(buttonPinOff, INPUT);
}
void loop(){
currentButtonOn= debounce1(lastButtonOn); //corrected variable name
currentButtonOff= debounce2(lastButtonOff); //corrected variable name
if (lastButtonOn==LOW&¤tButtonOn==HIGH);
if (lastButtonOff==LOW&¤tButtonOff==HIGH);
lastButtonOn= currentButtonOn;
lastButtonOff= currentButtonOff;
}