Created
April 18, 2021 00:11
-
-
Save Fallenstedt/8fd63d325217fe8a422946f2a8b1bcb0 to your computer and use it in GitHub Desktop.
traffic light
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int switchState = 0; | |
// pins | |
const int BUTTON = 2; | |
const int RED_LED = 3; | |
const int YELLOW_LED = 4; | |
const int GREEN_LED = 5; | |
void setup() { | |
Serial.begin(9600); // open the serial port at 9600 bps: | |
// while the serial stream is not open, do nothing: | |
while (!Serial) ; | |
// Inputs | |
pinMode(BUTTON, INPUT); | |
// Outputs | |
const int outputPinSize = 3; | |
int outputPins[outputPinSize] = {RED_LED, YELLOW_LED, GREEN_LED}; | |
makeOutput(outputPins, outputPinSize); | |
} | |
void loop() { | |
switchState = digitalRead(BUTTON); | |
if (switchState == LOW) { | |
digitalWrite(RED_LED, HIGH); | |
digitalWrite(YELLOW_LED, LOW); | |
digitalWrite(GREEN_LED, LOW); | |
} else { | |
digitalWrite(RED_LED, LOW); | |
digitalWrite(YELLOW_LED, LOW); | |
digitalWrite(GREEN_LED, HIGH); | |
delay(2500); | |
digitalWrite(YELLOW_LED, HIGH); | |
digitalWrite(GREEN_LED, LOW); | |
delay(1500); | |
} | |
} | |
void makeOutput(int arg[], int len) { | |
for (int i = 0; i < len; i++) { | |
Serial.print ( arg[ i ] ) ; | |
pinMode(arg[i], OUTPUT); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment