I have a book from Jaremy Blum to learn Arduino programming. I am doing some basic stuff right now like button debounce for example.
I set up Arduino Uno as ISP programmer to program ATTiny85 because that's all I need for my future application....
All works OK. Now, my goal was to program 2 separate buttons and 2 LEDs, each button toggling ON&OFF one LED.
I breadboard it and made that (somehow)happen. Buttons work as desired but I am not sure if I did it the right way because I just basically copy/paste and rename some lines from the book. I don't know if that is the right approach because my goal was to have just one general "Boolean debouce" definition but I was unable for some reason to do so. I wanted to implement both buttons (BUTTON1 & BUTTON2) into the debounce statement something like this:
boolean debounce (boolean last)
{
boolean current = digitalRead (BUTTON1), (BUTTON2);
or maybe
boolean current = digitalRead (BUTTON1 && BUTTON2); ??
What would be the right way to do it more simple.
Thanks.
Current code:
const int BUTTON1 = 3; // IO pin #2
const int LED1 = 4; // IO pin #3
const int BUTTON2 = 1; // IO pin #6
const int LED2 = 2; // IO pin #7
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
boolean lastButton1 = LOW;
boolean currentButton1 = LOW;
boolean ledOn1 = false;
void setup ()
{
pinMode (LED1, OUTPUT);
pinMode (BUTTON1, INPUT);
pinMode (LED2, OUTPUT);
pinMode (BUTTON2, INPUT);
}
/*
* Debouncing function
* Pass it to previous button state,
* and get back the current debounced button state.
*/
boolean debounce (boolean last)
{
boolean current = digitalRead (BUTTON1);
if (last != current)
{
delay (5);
current = digitalRead (BUTTON1);
return current;
}
}
boolean debounce1 (boolean last1)
{
boolean current1 = digitalRead (BUTTON2);
if (last1 != current1)
{
delay (5);
current1 = digitalRead (BUTTON2);
return current1;
}
}
void loop ()
{
currentButton = debounce (lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
ledOn = ! ledOn;
}
lastButton = currentButton;
digitalWrite (LED1, ledOn);
currentButton1 = debounce1 (lastButton1);
if (lastButton1 == LOW && currentButton1 == HIGH)
{
ledOn1 = ! ledOn1;
}
lastButton1 = currentButton1;
digitalWrite (LED2, ledOn1);
}