Go Down

Topic: Easy question about a boolean return value (Read 668 times) previous topic - next topic

lambss

Feb 15, 2013, 11:50 pm Last Edit: Feb 16, 2013, 12:00 am by lambss Reason: 1
Code: [Select]

/*
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.

Arrch

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.

PaulS

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.

lambss

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?

lambss

I figured it out, thank you all for the help

Go Up