Pages: [1]   Go Down
Author Topic: Easy question about a boolean return value  (Read 587 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
/*
Arduino Tutorials
Episode 3
Switch4 Program (pwm)
Written by: Jeremy Blum
*/

int switchPin = 8;
int ledPin = 11;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledLevel = 0;

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)
  {
    ledLevel = ledLevel + 51;
  }
  lastButton = currentButton;
 
  if (ledLevel > 255) ledLevel = 0;
  analogWrite(ledPin, ledLevel);

}

First of all, thanks for your time. The part of this code I dont really understand is the part of the beginning of the loop. currentButton calls for the debounce return, but what is (lastButton); doing? Video I am watching says he passes it to lastButton ,but I have no idea what that really means.

I realize this is on the lowend of programming, I promise I ordered an intro to programming book and plan to use it when it comes. In the mean time, can anyone help me understand this?

Much appreciated.
« Last Edit: February 15, 2013, 06:00:16 pm by lambss » Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like the code was cut-off, but it's likely the variable that hold the last reading, of the button so that you can detect a signal edge, rather than just the state of the input pin.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 615
Posts: 49420
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
. currentButton calls for the debounce return, but what is (lastButton); doing?
currentButton does not "call for the debounce return". currentButton is assigned the value returned by the call to debounce(). The argument to that function call is lastButton.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

did a lil more digging on the forums, found this answer to the same issue i was having.
Quote
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().

What does this mean exactly? To me it means lastbutton will become HIGH after running it once. Is that right? Wouldnt that cancel out the button press next time?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I figured it out, thank you all for the help
Logged

Pages: [1]   Go Up
Jump to: