Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit traffic light controller --- August 17, 2020
- This just controls three LEDs using delays. It has no inputs at all.
- */
- int red_led_pin = 10;
- int yellow_led_pin = 9;
- int green_led_pin = 8;
- // Each LED has its own delay time set below.
- unsigned long red_light_time = 5000;
- unsigned long red_and_yellow_light_time = 2000;
- unsigned long yellow_light_time = 3000;
- unsigned long green_light_time = 3000;
- // The delay() command can accept {int} and {unsigned int} values, but those only go up to 32 and 64 seconds (32,000 and 64,000).
- // The {unsigned long} type can go up to about 4.2 million seconds, and it's the largest type that delay() accepts so I put it here.
- // Normally I would say it's not a great idea to just use delays like this in a program,
- // but since all that this program does is blink some LEDs, delay() is good enough.
- void setup() {
- pinMode(red_led_pin, OUTPUT);
- pinMode(yellow_led_pin, OUTPUT);
- pinMode(green_led_pin, OUTPUT);
- change_to_yellow(); // This makes the lights start with yellow only on.
- }
- void loop() {
- delay(yellow_light_time);
- change_to_red();
- delay(red_light_time);
- change_to_red_and_yellow();
- delay(red_and_yellow_light_time);
- change_to_green();
- delay(green_light_time);
- change_to_yellow();
- }
- void change_to_red() {
- changeLights(true, false, false);
- }
- void change_to_red_and_yellow() {
- changeLights(true, true, false);
- }
- void change_to_yellow() {
- changeLights(false, true, false);
- }
- void change_to_green() {
- changeLights(false, false, true);
- }
- void changeLights(bool red_on, bool yellow_on, bool green_on) {
- // This function accepts three boolean parameters.
- // The values set the red, yellow and green LED states in that order.
- digitalWrite(green_led_pin, green_on);
- digitalWrite(yellow_led_pin, yellow_on);
- digitalWrite(red_led_pin, red_on);
- }
- // [end of file]
Advertisement
Add Comment
Please, Sign In to add comment