Devices & Components
Arduino Uno Rev3
5 mm LED: Green
Resistor 10k ohm
Breadboard (generic)
5 mm LED: Red
Push Button
Jumper wires (generic)
Resistor 1k ohm
Software & Tools
Arduino IDE
Project description
Code
Scpaceship Interface
arduino
1/* 2Spaceship Interface 3*/ 4 5// Create a global variable to hold the state of the switch. This variable is persistent throughout the program. Whenever you refer to switchState, you’re talking about the number it holds. 6 7int switchstate = 0; 8 9void setup() { 10// declare the LED pins as outputs 11pinMode(3, OUTPUT); 12pinMode(4, OUTPUT); 13pinMode(5, OUTPUT); 14 15// declare the switch pin as an input 16pinMode(2, INPUT); 17} 18 19void loop() { 20 21// read the value of the switch. digitalRead() checks to see if there is voltage on the pin or not 22switchstate = digitalRead(2); 23 24// if the button is not pressed turn on the green LED and off the red LEDs 25if (switchstate == LOW) { 26digitalWrite(3, HIGH); // turn the green LED on pin 3 on 27digitalWrite(4, LOW); // turn the red LED on pin 4 off 28digitalWrite(5, LOW); // turn the red LED on pin 5 off 29} 30 31// this else is part of the above if() statement. if the switch is not LOW (the button is pressed) turn off the green LED and blink alternatively the red LEDs 32else { 33digitalWrite(3, LOW); // turn the green LED on pin 3 off 34digitalWrite(4, LOW); // turn the red LED on pin 4 off 35digitalWrite(5, HIGH); // turn the red LED on pin 5 on 36// wait for a quarter second before changing the light 37delay(250); 38digitalWrite(4, HIGH); // turn the red LED on pin 4 on 39digitalWrite(5, LOW); // turn the red LED on pin 5 off 40// wait for a quarter second before changing the light 41delay(250); 42} 43}
Downloadable files
Breadboard Diagram
Breadboard Diagram

Comments
Only logged in users can leave comments