/*
DigitalReadSerial - Reads a digital input on pin 2, prints the result to the serial monitor
*/
// digital pin 2 has a pushbutton attached to it. Give it a name:
const int pushButtonPin = 2;
const int ledPin = 13;
boolean ledState = 0;
boolean currentButtonState;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButtonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
// the loop routine runs over and over againforever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButtonPin);
// print out the state of the button:
Serial.println(buttonState);
delay(100); // delay in between reads for stability
// if the button state has changed:
if (buttonState != currentButtonState){
currentButtonState = buttonState;
if (currentButtonState == 1) {
ledState = !ledState;
}
digitalWrite(ledPin,ledState);
}
}