2012-10-12 19:23:48 +02:00
|
|
|
|
/*
|
|
|
|
|
Arduino Starter Kit example
|
|
|
|
|
Project 2 - Spaceship Interface
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
2012-10-12 19:23:48 +02:00
|
|
|
|
This sketch is written to accompany Project 2 in the
|
|
|
|
|
Arduino Starter Kit
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
2012-10-12 19:23:48 +02:00
|
|
|
|
Parts required:
|
2013-10-21 09:58:40 +02:00
|
|
|
|
1 green LED
|
2012-10-12 19:23:48 +02:00
|
|
|
|
2 red LEDs
|
|
|
|
|
pushbutton
|
|
|
|
|
10 kilohm resistor
|
|
|
|
|
3 220 ohm resistors
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
2012-10-12 19:23:48 +02:00
|
|
|
|
Created 13 September 2012
|
|
|
|
|
by Scott Fitzgerald
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
2012-10-12 19:23:48 +02:00
|
|
|
|
http://arduino.cc/starterKit
|
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
|
This example code is part of the public domain
|
2012-10-12 19:23:48 +02:00
|
|
|
|
*/
|
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
|
// Create a global variable to hold the
|
|
|
|
|
// state of the switch. This variable is persistent
|
|
|
|
|
// throughout the program. Whenever you refer to
|
2012-10-12 19:23:48 +02:00
|
|
|
|
// switchState, you’re talking about the number it holds
|
|
|
|
|
int switchstate = 0;
|
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
|
void setup() {
|
|
|
|
|
// declare the LED pins as outputs
|
|
|
|
|
pinMode(3, OUTPUT);
|
|
|
|
|
pinMode(4, OUTPUT);
|
|
|
|
|
pinMode(5, OUTPUT);
|
2012-10-12 19:23:48 +02:00
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
|
// declare the switch pin as an input
|
|
|
|
|
pinMode(2, INPUT);
|
2012-10-12 19:23:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
|
void loop() {
|
2012-10-12 19:23:48 +02:00
|
|
|
|
|
|
|
|
|
// read the value of the switch
|
|
|
|
|
// digitalRead() checks to see if there is voltage
|
2013-10-21 09:58:40 +02:00
|
|
|
|
// on the pin or not
|
2012-10-12 19:23:48 +02:00
|
|
|
|
switchstate = digitalRead(2);
|
|
|
|
|
|
|
|
|
|
// if the button is not pressed
|
2014-05-01 22:22:04 +02:00
|
|
|
|
// turn on the green LED and off the red LEDs
|
2012-10-12 19:23:48 +02:00
|
|
|
|
if (switchstate == LOW) {
|
|
|
|
|
digitalWrite(3, HIGH); // turn the green LED on pin 3 on
|
|
|
|
|
digitalWrite(4, LOW); // turn the red LED on pin 4 off
|
|
|
|
|
digitalWrite(5, LOW); // turn the red LED on pin 5 off
|
|
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
|
// this else is part of the above if() statement.
|
2012-10-12 19:23:48 +02:00
|
|
|
|
// if the switch is not LOW (the button is pressed)
|
2014-05-01 22:22:04 +02:00
|
|
|
|
// turn off the green LED and blink alternatively the red LEDs
|
2012-10-12 19:23:48 +02:00
|
|
|
|
else {
|
|
|
|
|
digitalWrite(3, LOW); // turn the green LED on pin 3 off
|
|
|
|
|
digitalWrite(4, LOW); // turn the red LED on pin 4 off
|
|
|
|
|
digitalWrite(5, HIGH); // turn the red LED on pin 5 on
|
|
|
|
|
// wait for a quarter second before changing the light
|
|
|
|
|
delay(250);
|
|
|
|
|
digitalWrite(4, HIGH); // turn the red LED on pin 4 on
|
|
|
|
|
digitalWrite(5, LOW); // turn the red LED on pin 5 off
|
|
|
|
|
// wait for a quarter second before changing the light
|
|
|
|
|
delay(250);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|