© Royalty Free/CORBIS
© Royalty Free/Corbis


Exercise 1
Exercise 2




Exercise 1—Using a Push Button

Switches complete your circuits:
lightswitch.gif

Using a NO (normally on) push button, add the following to your breadboard:
switch1.png

These little switches are a 1/4" on each side, and can plug directly into a breadboard. These mechanical devices have 4 legs, which may make you think that there are 4 wires that are switched on and off, but in fact, two on each side are actually connected together inside. So really, this switch is just a 2-wire switch.
buttonlegsdiag.jpg
Normally, the two wires are disconnected (normally open) but when you press the little button on top, they are mechanically connected.
pushbuttons.gif

To get the buttons to sit better in the protoshield, you may want to straighten out the legs (just squish them with a pair of pliers) so that they look like the button on the left.
buttonlegs.jpg



Adding the Microcontroller

Now connect the led and the switch to the chip:
NO Push button

The Arduino chip has uncommitted inputs. That is, when an I/O pin is not connected and acting as an input, it cannot be assured to be either HIGH or LOW. Pull-up and pull-down resistors are needed to commit the input to the non-active (open) state for switches.





/* using a push button*/
int LED1=8;
int BTN1=10;
void setup(){
    pinMode(LED1,OUTPUT);
    pinMode(BTN1,INPUT);
}
 void loop(){
 if (digitalRead(BTN1)==0){
        digitalWrite(LED1,LOW);
     } else if  (digitalRead(BTN1)==1){
        digitalWrite(LED1,HIGH);
     }
 }


This code can be rewritten:
/* using a push button*/
int LED1=8;
int BTN1=10;
void setup(){
    pinMode(LED1,OUTPUT);
    pinMode(BTN1,INPUT);
}
 void loop(){
 if (!digitalRead(BTN1)){
        digitalWrite(LED1,LOW);
     } else if  (digitalRead(BTN1)){
        digitalWrite(LED1,HIGH);
     }
 }
or:
/* using a push button*/
int LED1=8;
int BTN1=10;
void setup(){
    pinMode(LED1,OUTPUT);
    pinMode(BTN1,INPUT);
}
 void loop(){
     digitalWrite(LED1,digitalRead(BTN1);  
 }
Instead of a push button create a custom button (water, penny, stapler, sheets of metal, etc).