Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
SBRDIYables
Published

Spaceship Interface

If Push Button is pressed, Green LED is turned OFF and Red LEDs blink alternatively. Otherwise Green LED is turned ON and Red LEDs are turne

BeginnerProtip12,439
Spaceship Interface

Things used in this project

Story

Read more

Schematics

Breadboard Diagram

Code

Scpaceship Interface

Arduino
/*
Spaceship Interface
*/

// 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.

int switchstate = 0;

void setup() {
// declare the LED pins as outputs
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);

// declare the switch pin as an input
pinMode(2, INPUT);
}

void loop() {

// read the value of the switch. digitalRead() checks to see if there is voltage on the pin or not
switchstate = digitalRead(2);

// if the button is not pressed turn on the green LED and off the red LEDs
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
}

// 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
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);
}
}

Credits

SBR
37 projects • 52 followers
Mechanical Engineer
Contact
DIYables
0 projects • 88 followers
I would like to invite you to join and add your projects to DIYables platform https://www.hackster.io/diyables
Contact

Comments

Please log in or sign up to comment.