/* "incrementer"
* -This is a simple program that responds to the state
* of a switch. If it is closed, it increments a variable
* lights an LED and waits half a second.
* -Otherwise it does nothing.
*
*--- Set up: > Wire a switch between pin 5 and Gnd
*
*******************************************************/
int numVariable = 0; // Just a number variable, not connected to anything
int LEDpin = 7; // pin number of the LED
int switchPin = 5; // pin number of the switch
boolean swState = 0; // create variable for storing switch "state"
void setup() {
pinMode(LEDpin, OUTPUT); // Define LED pin
pinMode(switchPin, INPUT_PULLUP); // Define Switch pin as INPUT
// (It's default state will be HIGH.)
Serial.begin(9600); // Start a serial data connection with USB
}
void loop() {
swState = digitalRead(switchPin); // "Read" the voltage on the pin
if (swState == LOW) { // (If button is pressed)
numVariable++; // Add one to the variable
Serial.print("Switch closed. Variable is now: ");
Serial.println(numVariable);
digitalWrite(LEDpin, HIGH); // turn LED on:
delay(500); // Delay 500 milliseconds
}
// Otherwise, assume the switch is open.
else {
digitalWrite(LEDpin, LOW); // turn LED off:
}
}
|