Trying to understand a boolean function

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);

}

Moderator edit: code tags added

1 Like

in function:

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.

Sorry, I'm still unclear where 'last' gets a value. Is it just set to equal whatever current equals the last time through the debounce function?

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?

last in debounce() is the same as lastButton in loop() ...

lastButton in loop() is the stored value of currentButton from the last iteration of loop().

I highly recommend reading a basic C tutorial.

-Enjoy
fh : )_~

So you can say current or currentButton and they mean the same thing? Why put Button in at all?
Thanks for the help so far!

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.

It is slowly making more sense. Thank you for the help.

The query raised by ryannyar is ...

< 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 :wink: 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;

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);

}

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.
Mike_Sterlite.

Can I use the word previous in place of last ?

Of course. You could use vanillaIceCream, if that made sense. Use whatever names make sense to you.

Can I use the word Push in place of Button ?

Yes, but... Button is a noun, It refers to a thing. Push is a verb. It refers to an action. You might well make the code harder to understand.

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?

Thanks,
Mike_Sterlite

Last gets it's value from what was passed to the function debounce.

When you call debounce you have to use a variable with it "bebounce(lastbutton) so what ever the value of

lastbutton becomes the value of last in the routine called debounce.

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.

Hello and Thanks to PaulMurrayCbr

I refer to your statement - debounce(), perhaps regrettably, names its function argument last.

Is last a standard word ( sort of a key word )?
Can I write previous in place of last ?

Thanks,
Mike_Sterlite

Can I write previous in place of last ?

Did you try it ?

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?

Thanks, Mike_Sterlite

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 ?