Advertisement
talofer99

Attiny85 Traffic light

Aug 14th, 2019
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <FastLED.h>
  2. #define NUM_LEDS 3
  3. #define DATA_PIN PB2
  4.  
  5. // BUTTON PINS
  6. #define AUTOMODEPIN PB1
  7. #define MANUALMODEPIN PB0
  8.  
  9. // LED PIN
  10. #define LED_PIN PB5
  11.  
  12. // LED COLOR
  13. #define LED_COLOR_GREEN 0
  14. #define LED_COLOR_ORANGE 1
  15. #define LED_COLOR_RED 2
  16.  
  17. // LED STATE
  18. #define LEDSTATE_GREEN 0
  19. #define LEDSTATE_ORANGE 1
  20. #define LEDSTATE_RED 2
  21. #define LEDSTATE_RED_ORANGE 3
  22.  
  23. // system state
  24. #define SYSTEMSTATE_MANUAL 0
  25. #define SYSTEMSTATE_AUTO 1
  26.  
  27.  
  28. // Define the array of leds
  29. CRGB leds[NUM_LEDS];
  30. //colord per led
  31. uint32_t colorsIDX[] = {CRGB::Green, CRGB::Orange, CRGB::Red};
  32.  
  33. // led state
  34. byte ledState;
  35. // system state
  36. byte systemState;
  37. unsigned long lastLEDStateChange;
  38. int waitPeriod[4] = {7500,1000,5000,500};
  39.  
  40.  
  41.  
  42. void setup() {
  43. FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  44. pinMode(AUTOMODEPIN, INPUT_PULLUP);
  45. pinMode(MANUALMODEPIN, INPUT_PULLUP);
  46. pinMode(LED_PIN, OUTPUT);
  47.  
  48.  
  49. // start led state
  50. systemState = SYSTEMSTATE_AUTO;
  51. setLEDState(0);
  52. }
  53.  
  54.  
  55.  
  56.  
  57. void loop() {
  58. if (!digitalRead(MANUALMODEPIN)) {
  59. setLEDState((ledState + 1) % 4);
  60. systemState = SYSTEMSTATE_MANUAL;
  61. // quick and dirty debounce
  62. delay(500);
  63. } //end if
  64.  
  65. if (!digitalRead(AUTOMODEPIN)) {
  66. systemState = SYSTEMSTATE_AUTO;
  67. // quick and dirty debounce
  68. digitalWrite(LED_PIN, HIGH);
  69. delay(500);
  70. digitalWrite(LED_PIN, LOW);
  71. } //end if
  72.  
  73. // if we are on auto
  74. if (systemState == SYSTEMSTATE_AUTO) {
  75. //check time pass ... we do it with int the other if - not to do the math all the time
  76. if (lastLEDStateChange + waitPeriod[ledState] < millis()) {
  77. setLEDState((ledState + 1) % 4); // move to nect led state
  78. } //end if
  79. }//end if
  80.  
  81. } //end loop
  82.  
  83.  
  84. void setLEDState(byte newState) {
  85. switch (newState) {
  86. case LEDSTATE_GREEN:
  87. leds[LED_COLOR_RED] = CRGB::Black;
  88. leds[LED_COLOR_ORANGE] = CRGB::Black;
  89. leds[LED_COLOR_GREEN] = colorsIDX[LED_COLOR_GREEN];
  90. break;
  91. case LEDSTATE_ORANGE:
  92. leds[LED_COLOR_RED] = CRGB::Black;
  93. leds[LED_COLOR_ORANGE] = colorsIDX[LED_COLOR_ORANGE];
  94. leds[LED_COLOR_GREEN] = CRGB::Black;
  95. break;
  96. case LEDSTATE_RED:
  97. leds[LED_COLOR_RED] = colorsIDX[LED_COLOR_RED];
  98. leds[LED_COLOR_ORANGE] = CRGB::Black;
  99. leds[LED_COLOR_GREEN] = CRGB::Black;
  100. break;
  101. case LEDSTATE_RED_ORANGE:
  102. leds[LED_COLOR_RED] = colorsIDX[LED_COLOR_RED];
  103. leds[LED_COLOR_ORANGE] = colorsIDX[LED_COLOR_ORANGE];
  104. leds[LED_COLOR_GREEN] = CRGB::Black;
  105. break;
  106. } //end switch
  107.  
  108. // set to current
  109. ledState = newState;
  110. //set last millis chnage
  111. lastLEDStateChange = millis();
  112. // show
  113. FastLED.show();
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement