Arduino boolean question - Page 1
Forum section: electronics / general

author: IvoS
author_rank: Frequent Contributor
timestamp: January 29, 2015, 11:12:18 pm
content: 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 maybeboolean current = digitalRead(BUTTON1 && BUTTON2);??What would be the right way to do it more simple.Thanks.Current code:Code:const int BUTTON1 = 3; // IO pin #2const int LED1 = 4; // IO pin #3const int BUTTON2 = 1; // IO pin #6const int LED2 = 2; // IO pin #7boolean 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);}

author: bobcat
author_rank: Regular Contributor
timestamp: January 30, 2015, 12:34:37 am
content: digitalRead() takes an Arduino pin number.Using a boolean operator will only return true or false. (what value that resolves to depends on how arduino defines it)You have to take inputs one at a time.

author: dannyf
author_rank: Super Contributor
timestamp: January 30, 2015, 12:40:00 am
content: 1. The two debounce routines are highly alike (almost identical). You can probably combine them;2. The routine needs to return a value under all conditions - it doesn't currently.

author: leo_r
author_rank: Newbie
timestamp: January 30, 2015, 01:24:00 pm
content: Have you done much programming before? Your question is slightly unclear - I think you could interpret it two ways.(I don't think this is what you want:) You want your debounce function to return true only when both buttons are pressed. I don't think the debounce function would be the place to do this, but you would, somewhere, have the code (button1 && button2).You want to have a generic function which you can call for debouncing either switch.Assuming it's the second one, you need to have a function which takes 2 arguments.Argument 1 is the boolean "last"Argument 2 is the pin of the button you wish to read.The function would then look like this:Code:boolean debounce (boolean last, int buttonPin){boolean current = digitalRead (buttonPin);if (last != current ){delay (5);current = digitalRead (buttonPin);}// Putting this outside the "if" statement means the code will always return a valuereturn current ;}and then to call this function, you'd do the following:Code:currentButton1 = debounce (lastButton1, BUTTON1);If you're new to programming in general then I'd recommend learning the basics on your PC rather than trying to do embedded stuff. The Arduino IDE is terrible for telling you where you've made mistakes and the embedded nature of it means you can't step through it line by line and see what the values each of your variables have at each execution step. If you're still trying to understand the basics building blocks of a program then having the added complexity of an Arduino and the Arduino IDE won't help.A fairly basic introduction to C would be a good start, or perhaps something like Python or Ruby would be good to introduce the basic building blocks of programs - functions, loops, conditional statements and the like.

author: IvoS
author_rank: Frequent Contributor
timestamp: January 30, 2015, 08:07:42 pm
content: Quote from: leo_r on January 30, 2015, 01:24:00 pm