Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
traffic light
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