Home‎ > ‎Unit 07‎ > ‎

Alternating Action

The following text and code examples were adapted from http://www.ladyada.net/learn/arduino/lesson5.html under http://creativecommons.org/licenses/by-sa/2.5/ as such, this page is subject to the same license. 

Having an LED turn on or off when a button is pressed is quite impressive, but it would be pretty odd if you had to press a button constantly to keep the TV on. What we want is an alternating action switch, where the press-and-release of a button does something, not just press-and-hold. Basically we want to test whether the button was just released, or just pressed. 

To do this, we need to keep track of the button input value, to see if its changed. This is called the state of a button. When the state changes (an action occurs), that's when we want to perform an action.

Try this code:
import processing.serial.*;

import cc.arduino.*;

Arduino arduino; 

int buttonPin = 2;
int buttonState = 0;
int val;

void setup() {
  size(200, 200);    
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  arduino.pinMode(buttonPin, Arduino.INPUT);
  background(0);
}

void draw()
{
  val = arduino.digitalRead(buttonPin);      // read input value and store it in val

  if (val != buttonState) {          // the button state has changed!
    if (val == 1) {                // check if the button is pressed
      println("Button just pressed");
      background(255);
    } 
    else {                         // the button is -not- pressed...
      println("Button just released");
      background(0);
    }
  }

  buttonState = val;                 // save the new state in our variable
}

So, how exactly does this work?
  val = arduino.digitalRead(buttonPin);      // read input value and store it in val
In the draw() function, we begin by first checking the button pin state and storing it an temporary variable val.
if (val != buttonState) {          // the button state has changed!
    if (val == 1) {                // check if the button is pressed
Now we see 2 if statements that are nested, this means that we perform one test and if that test comes out true we go on to perform another test. A buttonState of 1 means Arduino.HIGH and indicates button has been pushed . This is more complex than a simple if statement but not much more different than the kinds of decisions we make all the time.

For example:
  if ( it is raining ) {                // look up, is there water falling on me?
    if ( I have an umbrella ) {         // check my purse
      OpenUmbrella();                   // Perform the umbrella opening procedure
    }
  }
Of course, we can't open the umbrella if we don't have one. And there's no point in checking if we have one if its not raining!

In the first if statement, we check if the current button state (HIGH or LOW, 1 or 0) is different than the last time we looked at the button. If it is different (tested by the != inequality operator ) then we execute the next group of statements, enclosed by the {} braces.

Assignment 7.2 - In your blog, explain how this is different from the previous push button code. You should include why you'd want to use one method or the other. Bonus points for writing a quick pair of programs to illustrate your point and include a Jing video with voice over explaining the difference.
Comments