| 1 |
/* |
| 2 |
Arduino Starter Kit example |
| 3 |
Project 2 - Spaceship Interface |
| 4 |
|
| 5 |
This sketch is written to accompany Project 2 in the Arduino Starter Kit |
| 6 |
|
| 7 |
Parts required: |
| 8 |
- one green LED |
| 9 |
- two red LEDs |
| 10 |
- pushbutton |
| 11 |
- 10 kilohm resistor |
| 12 |
- three 220 ohm resistors |
| 13 |
|
| 14 |
created 13 Sep 2012 |
| 15 |
by Scott Fitzgerald |
| 16 |
|
| 17 |
https://store.arduino.cc/genuino-starter-kit |
| 18 |
|
| 19 |
This example code is part of the public domain. |
| 20 |
*/ |
| 21 |
|
| 22 |
// Create a global variable to hold the state of the switch. This variable is |
| 23 |
// persistent throughout the program. Whenever you refer to switchState, you’re |
| 24 |
// talking about the number it holds |
| 25 |
int switchstate = 0; |
| 26 |
|
| 27 |
void setup() { |
| 28 |
// declare the LED pins as outputs |
| 29 |
pinMode(3, OUTPUT); |
| 30 |
pinMode(4, OUTPUT); |
| 31 |
pinMode(5, OUTPUT); |
| 32 |
|
| 33 |
// declare the switch pin as an input |
| 34 |
pinMode(2, INPUT); |
| 35 |
} |
| 36 |
|
| 37 |
void loop() { |
| 38 |
|
| 39 |
// read the value of the switch |
| 40 |
// digitalRead() checks to see if there is voltage on the pin or not |
| 41 |
switchstate = digitalRead(2); |
| 42 |
|
| 43 |
// if the button is not pressed turn on the green LED and off the red LEDs |
| 44 |
if (switchstate == LOW) { |
| 45 |
digitalWrite(3, HIGH); // turn the green LED on pin 3 on |
| 46 |
digitalWrite(4, LOW); // turn the red LED on pin 4 off |
| 47 |
digitalWrite(5, LOW); // turn the red LED on pin 5 off |
| 48 |
} |
| 49 |
// this else is part of the above if() statement. |
| 50 |
// if the switch is not LOW (the button is pressed) turn off the green LED and |
| 51 |
// blink alternatively the red LEDs |
| 52 |
else { |
| 53 |
digitalWrite(3, LOW); // turn the green LED on pin 3 off |
| 54 |
digitalWrite(4, LOW); // turn the red LED on pin 4 off |
| 55 |
digitalWrite(5, HIGH); // turn the red LED on pin 5 on |
| 56 |
// wait for a quarter second before changing the light |
| 57 |
delay(250); |
| 58 |
digitalWrite(4, HIGH); // turn the red LED on pin 4 on |
| 59 |
digitalWrite(5, LOW); // turn the red LED on pin 5 off |
| 60 |
// wait for a quarter second before changing the light |
| 61 |
delay(250); |
| 62 |
} |
| 63 |
} |