I'm watching Jeremy Blum's Arduino tutorial number 2 in which he explains how to debounce a push button. But in trying to figure out how it works, I'm stumped by where the value of 'last' comes from and what it means when it says 'return current' at the end of the boolean function for debounce. Where is it sending the value of current and where does it get a value for last. Are the variables current and currentButton the same or are they different like I originally thought? Any help would be hugely appreciated!
/*
Arduino Tutorials
Episode 2
Switch3 Program (debounced)
Written by: Jeremy Blum
*/
int switchPin = 8;
int ledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
void setup()
{
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
}
void loop()
{
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
ledOn = !ledOn;
}
lastButton = currentButton;
digitalWrite(ledPin, ledOn);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
}
Moderator edit: more code tags added
in the third line he reads the value of the switch(high or low). in the fourth line he compares with the stored value. if they are different he waits 5 for the switch to settle then saves the new value in line 6 and returns it to the mian loop. Next time the "last" value passed to the function is the "new" stored value.
Is the boolean debounce function determining the value of 'last'? Is that what the first line means when is says:
boolean debounce (boolean last) --- does this mean that the value of last will be determined by the function that follows?
They aren't the same thing. They differ in scope (scope - Arduino Reference), where currentButton is a global variable, and current is local to the debounce() function.
< What is the value of "last" in "boolean debounce(boolean last)". >
I reached this forum today just to ask the same question.
But, even after reading all the responses over again and again, I am still unable to find an answer to the question.
Can anyone help, please ?
To be able to color code the code there is no code tags.
Debounce function is called with value of lastButton as argument(Correct wording I hope)
It is not the variable that is sent, but the value, and the function put this value into variable last.
lastButton started out as LOW, and first time function is called, last is also LOW.
If button is pressed, current is HIGH so they are not equal. A delay (5) and then button is read again.
Value of current is returned to currentButton
If button is read with no change, only one reading is made. If button as changed, a second reading is done and that value is returned.
I think this is a very simple example to show the concept. You could just put the function in loop when it is one button and one variable. Advantages comes when code grows. If you also send which button you only need to write this one function to debounce all your buttons. Less writing, less memory need as code isn't repeating.
int switchPin = 8;
int ledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
Thanks, Gabriel_swe..
I am deficient in coding knowledge. I wish to learn more, and be proficient. Looks like I need to learn more about functions.
I now think that last is somehow associated with lastButton.
But I do not know how the two are associated.
Can I use the word previous in place of last ?
Can I use the word Push in place of Button ?
Can you please suggest some learning guide for me to be proficient in coding arduino?
Thanks, PaulS.
I was just trying to check whether the words last and Button are any sort of keywords. Well, you have just explained to me that they are not keywords.
Please refer to :- Posted by Gabriel_swe May 01, 2017, 09:17 pm
lastButton started out as LOW, and first time function is called, last is also LOW.
I have not understood how that works.
I believe that lastButton has nothing to do with last. Am I right ?
Also,
I guess Gabriel_swe is referring to the boolean debounce() function.
I need to learn more about coding to understand how these three are related.
Can you please suggest some learning guide for me to be proficient in coding arduino?
The global variables are named lastButton and currentButton. These will retain their values as the loop is called repeatedly by the underlying arduiino code.
loop() passes lastButton as a function argument to debounce().
debounce(), perhaps regrettably, names its function argument last. It also declares a local varuable named current. These two variables - last and current are internal to debounce() and only endure for so long as debounce() is executing.
When debounce() terminates, it returns a boolean value (which is calculates by just taking the value of current) to the calling function. This is loop(), which saves that returned boolean value in the global variable currentButton.
The whole design is a little silly, because debounce() is tied to one particular pin, so why write it so that you pass in the bouncing state? But … it's a tutorial.
Consider this:
int switchPinA = 8;
int switchPinB = 9;
boolean lastButtonA = LOW;
boolean currentButtonA = LOW;
boolean lastButtonB = LOW;
boolean currentButtonB = LOW;
int ledPin = 13;
boolean ledOn = false;
void setup()
{
pinMode(switchPinA, INPUT);
pinMode(switchPinB, INPUT);
pinMode(ledPin, OUTPUT);
}
boolean debounce(byte pin, boolean last)
{
boolean current = digitalRead(pin);
if (last != current)
{
delay(5);
current = digitalRead(pin);
}
return current;
}
void loop()
{
currentButtonA = debounce(switchPinA, lastButtonA);
currentButtonB = debounce(switchPinB, lastButtonB);
if (lastButtonA == LOW && currentButtonA == HIGH)
{
ledOn = !ledOn;
}
// we are not using button B for anything … but we could
lastButtonA = currentButtonA;
lastButtonB = currentButtonB;
digitalWrite(ledPin, ledOn);
}
Here you can see why you might want to rename your function arguments. Inside debounce(),
the local variables pin, last, and current variously mean A and B when loop() passes the particular global variables that it cares about into that function.
Keyword colouring in the IDE is severely broken and largely a waste of time. Keywords in any library you have installed are coloured in your code whether or not you are using the library in your program.
If in doubt use a different name for the variable. You are usually safe if you use camelCase names.
I know that I must learn something. I know that my knowledge is deficient. I am trying to be proficient. I searched a lot on internet, and still did not understand how this debounce() works.
I think I do not even know what to ask. So, at least, can you please guide me to frame my question?
I propose to reach up to my "right" question through some preliminary silly ( idiotic.. please pardon me) questions
My preliminary question no.1 - PQ1
PQ1 : What is the relationship/ association/ dependancy between last and lastButton ?
My preliminary question no.2 - PQ2
PQ2 : Is last some sort of a keyword? Can I replace it with any other word I fancy? For example, can I write boolean debounce(boolean previous) in place of boolean debounce(boolean last) ?
My preliminary question no.3 - PQ3
PQ3 : Can I use any word in place of debounce ? For example, can I write boolean stabilize(boolean last) in place of boolean debounce(boolean last) ?
If I receive answer Yes or No to the above two questions, I think I will be able to ask my "right" question as the next step.
Can some one please suggest/ advise some learning guide?
PQ1 : What is the relationship/ association/ dependancy between last and lastButton ?
last is the name of the variable used locally within the debounce function to hold the previous state of the pin. lastButton is name of the variable holding the previous state globally in the program. This allows the function to be used with several different pins, each potentially having a different state.
PQ2 : Is last some sort of a keyword? Can I replace it with any other word I fancy? For example, can I write boolean debounce(boolean previous) in place of boolean debounce(boolean last) ?
You can use any name for the previous state that you like as long as it is not already used by the program or a library used by the program. Incidentally, in your question you omitted the pin number parameter from the function call.
PQ3 : Can I use any word in place of debounce ? For example, can I write boolean stabilize(boolean last) in place of boolean debounce(boolean last) ?
You can use any valid name for the function that you like as it is not already used by the program or a library used by the program.
Can some one please suggest/ advise some learning guide?
Any of the multiple hundreds (thousands) of online C and C++ tutorials will explain how to write and use functions.
UKHeliBob:
last is the name of the variable used locally within the debounce function to hold the previous state of the pin.
At the first run of the loop, when it encounters if (last != current), the value of last is not yet declared. So, I think I should see an error message. But the loop works as expected.
So, My final question no.1 - FQ1 is:-
FQ1 :- How does last receive a value during the first run of the loop ?