Example Code‎ > ‎Sensor Tutorials‎ > ‎

Push Button Switch

Parts

-Arduino microcontroller and carrier board
-LiPo battery
-Push button


Prepare the breadboard


Program the Microcontroller

/**
* @file: switch sensor example
* @date: 3/29/2011
*
* @DESCRIPTION
* reads a digital input and prints results.
* needs a contact switch
*/
int buttonState =0;
#define buttonPin 2

//--- Function: Setup ()
void setup()
{
    pinMode(buttonPin, INPUT);
    Serial.begin(9600);
    delay(25);
}

//--- Function: loop ()
void loop()
{
    // Reads a digital input on pin 2
    buttonState = digitalRead(buttonPin);
    //prints the result to the serial monitor
    Serial.println(buttonState, DEC);

    if (buttonState == HIGH)
            Serial.println("button pressed");
    else
        Serial.println("not pressed");

}

Comments