|
|
|
|
switch by BARRAGAN <http://barraganstudio.com> Demonstrates the use of digital pins and a switch. The pin used as input is connected to a switch and the pin used as output is the onboard LED When the switch is pressed, the light blinks, the light goes off when the switch is released. Created 5 December 2003 Revised 4 April 2007 |
||
|
|
// switch
// by BARRAGAN <http://barraganstudio.com>
int direction = 1; // used for the blinking cycle
int switchPin = 0; // digital pin to attach the switch
int ledPin = 48; // Wiring onboard LED
void setup()
{
pinMode(switchPin, INPUT); // sets digital pin 0 as input
pinMode(ledPin, OUTPUT); // sets digital pin 48 as output
}
void loop()
{
if(digitalRead(switchPin) == HIGH) // if the switch is pressed
{
direction *= -1; // alternate between 1 or -1
if (direction == 1) // to decide to turn on or off the light
{
digitalWrite(ledPin, HIGH); // turns the light on
}
else
{
digitalWrite(ledPin, LOW); // turns the light off
}
delay(200); // waits 200 milliseconds
}
else // if switch not pressed
{
digitalWrite(ledPin, LOW); // keeps the light off
}
}
|
![]() |