//--- Reads the state of a switch, lights LED and sends serial data accordingly // // -- Wire an LED from Digital Pin 7 thru a 330 ohm resistor to ground // -- Wire a switch between Digital Pin 5 and ground //---------------------------------------------------------------------------------- int buttonState = 0; // create variable for storing "state" of the button int ledPin = 7; // create variable for LED pin number int buttonPin = 5; // create variable for button pin number void setup() { Serial.begin(9600); // start serial port at 9600 bps: pinMode(ledPin, OUTPUT); // initialize the digital pin as an output (for LED.) pinMode(buttonPin, INPUT_PULLUP); // initialize the digital pin as an input (for switch.) } void loop() { buttonState = digitalRead(buttonPin); // "Read" the voltage present on the pin (check switch.) // If the pin wired to the button is low, the switch is closed if (buttonState == LOW) { digitalWrite(ledPin, HIGH); // turn LED on: Serial.println(0); // Send ASCII message to serial (USB) } // Otherwise, assume the switch is open. else { digitalWrite(ledPin, LOW); // turn LED off: Serial.println(1); // Send ASCII message to serial (USB) } delay(25); } |