skizziks_53

Reddit traffic light blinker 1.0

Aug 18th, 2020 (edited)
1,578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. /*
  2.    Reddit traffic light controller --- August 17, 2020
  3.    This just controls three LEDs using delays. It has no inputs at all.
  4.  
  5. */
  6.  
  7. int red_led_pin = 10;
  8. int yellow_led_pin = 9;
  9. int green_led_pin = 8;
  10.  
  11. // Each LED has its own delay time set below.
  12. unsigned long red_light_time = 5000;
  13. unsigned long red_and_yellow_light_time = 2000;
  14. unsigned long yellow_light_time = 3000;
  15. unsigned long green_light_time = 3000;
  16. // 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).
  17. // 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.
  18.  
  19. // Normally I would say it's not a great idea to just use delays like this in a program,
  20. //   but since all that this program does is blink some LEDs, delay() is good enough.
  21.  
  22. void setup() {
  23.   pinMode(red_led_pin, OUTPUT);
  24.   pinMode(yellow_led_pin, OUTPUT);
  25.   pinMode(green_led_pin, OUTPUT);
  26.   change_to_yellow(); // This makes the lights start with yellow only on.
  27. }
  28.  
  29.  
  30. void loop() {
  31.   delay(yellow_light_time);
  32.  
  33.   change_to_red();
  34.   delay(red_light_time);
  35.  
  36.   change_to_red_and_yellow();
  37.   delay(red_and_yellow_light_time);
  38.  
  39.   change_to_green();
  40.   delay(green_light_time);
  41.  
  42.   change_to_yellow();
  43. }
  44.  
  45.  
  46.  
  47. void change_to_red() {
  48.   changeLights(true, false, false);
  49. }
  50.  
  51. void change_to_red_and_yellow() {
  52.   changeLights(true, true, false);
  53. }
  54.  
  55. void change_to_yellow() {
  56.   changeLights(false, true, false);
  57. }
  58.  
  59. void change_to_green() {
  60.   changeLights(false, false, true);
  61. }
  62.  
  63. void changeLights(bool red_on, bool yellow_on, bool green_on) {
  64.   // This function accepts three boolean parameters.
  65.   // The values set the red, yellow and green LED states in that order.
  66.   digitalWrite(green_led_pin, green_on);
  67.   digitalWrite(yellow_led_pin, yellow_on);
  68.   digitalWrite(red_led_pin, red_on);
  69. }
  70.  
  71.  
  72. // [end of file]
Advertisement
Add Comment
Please, Sign In to add comment