© Royalty Free/CORBIS
© Royalty Free/Corbis

More Buttons!

Exercise 1
Exercise 2

Exercise 1—Keep it on

Turning on and off an LED when a button is pressed is lovely, but think about switches you live with. If you turn on the TV, you don't want to hold the button to keep it on. What you need is an alternating action switch, where the press-and-release of a button does something, not just press-and-hold. Basically you want to test if the button was just released, or just pressed. To do this, you 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 you want to perform an action.

You'll also need a flag to keep track whether the light is on or off.

Wire up your board, modify the code and upload it to the chip:
int led1=8;
int btn1 = 7;              // switch is connected to pin 2
int val;                        // variable for reading the pin status
int btnState;                // variable to hold the last button state
boolean lightOn=false;


void setup() {
  pinMode( led1, OUTPUT);    // Set the switch pin as input
  pinMode( btn1, INPUT);    // Set the switch pin as input
  btnState = digitalRead(btn1);   // read the initial state
}


void loop(){
  val = digitalRead( btn1);      // read input value and store it in val

  if (val != btnState) {          // the button state has changed!
    if (val == HIGH) {                // check if the button is pressed
      lightOn=!lightOn;
    }
  }
  //control the light here


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

Exercise 2—Counting Presses

This example introduces Serial output. This is a good way to see what values you are receiving.
© Royalty-Free/CORBIS
Sometimes you just can't figure out why your program doesn't work. In Revolution you can use the message window to check your variables, in Flash, the output window. With Arduino, you need to use the Serial Monitor.


Serial means that data is sent or received one bit at a time.

Serial communication is the most common form of communication between electronic devices. Communicating serially involves sending a series of digital pulses back and forth between devices at a mutually agreed upon rate.

There is a Serial built-in class that comes with Arduino.

Serial.print(20,BYTE); //Serial is the class and print is a function of that class.

To set up serial communication:

  1. Plug the board into the computer


  2. Open the Arduino application


  3. From the Tools menu select Serial Port and select the port.


  4. Create a new program (+N). Paste in the following:
    /*Arduino Example #3- Hit The Lights 2
      Purpose: To make a specific number of user-controlled LEDs blink 
      by using switches or buttons.
      */
    
    void setup(){
      Serial.begin(9600);     //open up a serial port at 9600 bits per second
      }
    
    void loop(){
      Serial.println("hello");
      Serial.print("hello\n");  // \is an escape sequence used to differentiate a 
                                //character from that in the lanage
      delay(500);               //slows it down
      
    }

  5. Compile the program 



  6. Upload the program 



  7. Open Serial Monitor 



Getting back to buttons


After you upload the program click on the serial monitor icon
serial_moitor.png
int btn1 = 2;              // switch is connected to pin 2
int val;                        // variable for reading the pin status
int btnState;                // variable to hold the button state
int btnPresses = 0;          // how many times the button has been pressed

void setup() {
  pinMode(btn1, INPUT);    // Set the switch pin as input

  Serial.begin(9600);           // Set up serial communication at 9600bps
  btnState = digitalRead(btn1);   // read the initial state
}


void loop(){
  val = digitalRead(btn1);      // read input value and store it in val

  if (val != btnState) {          // the button state has changed!
    if (val == LOW) {             // check if the button is pressed
      btnPresses++;               // increment the buttonPresses variable
      Serial.print("Button has been pressed ");
      Serial.print(btnPresses);
      Serial.println(" times");
    }
  }
  btnState = val;                 // save the new state in our variable
}

  1. Modify the sketch so that message is only printed when the button is released, not when it's pressed.

  2. Modify the sketch so its a countdown device:
    • Have the btnPresses variable start at 10.
    • Every time the button is pressed, decrement the btnPresses variable (use the -- operator, which does the opposite of ++).
    • Once you have that working, have the Arduino print out We have x presses to go till takeoff! where x is the number of presses remaining, but only if the number of presses left is larger than 0 (check the conditional test table above to see how to test if a variable is larger than a number)
    • Once you have that working, make the Arduino print out EXPLODE! on the last button press.


Source:http://www.ladyada.net