Tal O
Published © GPL3+

DIY ATtiny85 Digispark-Inspired Traffic Light

A kids traffic light!

IntermediateFull instructions provided142
DIY ATtiny85 Digispark-Inspired Traffic Light

Things used in this project

Story

Read more

Code

Traffic light code

Arduino
// Code by Tal Ofer - talofer99@hotmail.com
// requires the FastLed library.
// Manual button pin - PB0
// Auto button pin - PB1
// Addressable LED data pin - PB2
// led pin (auto mode blink) - PB5


#include <FastLED.h>
#define NUM_LEDS 3
#define DATA_PIN PB2

// BUTTON PINS
#define AUTOMODEPIN PB1
#define MANUALMODEPIN PB0

// LED PIN 
#define LED_PIN PB5

// LED COLOR
#define LED_COLOR_GREEN 0
#define LED_COLOR_ORANGE 1
#define LED_COLOR_RED 2

// LED STATE
#define LEDSTATE_GREEN 0
#define LEDSTATE_ORANGE 1
#define LEDSTATE_RED 2
#define LEDSTATE_RED_ORANGE 3

// system state
#define SYSTEMSTATE_MANUAL 0
#define SYSTEMSTATE_AUTO 1


// Define the array of leds
CRGB leds[NUM_LEDS];
//colord per led
uint32_t  colorsIDX[] = {CRGB::Green, CRGB::Orange, CRGB::Red};

// led state
byte ledState;
// system state
byte systemState;
unsigned long lastLEDStateChange;
int waitPeriod[4] = {7500,1000,5000,500};



void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  pinMode(AUTOMODEPIN, INPUT_PULLUP);
  pinMode(MANUALMODEPIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);

  
  // start led state
  systemState = SYSTEMSTATE_AUTO;
  setLEDState(0);
}




void loop() {
  if (!digitalRead(MANUALMODEPIN)) {
    setLEDState((ledState + 1) % 4);
    systemState = SYSTEMSTATE_MANUAL;
    // quick and dirty debounce
    delay(500);
  } //end if

  if (!digitalRead(AUTOMODEPIN)) {
    systemState = SYSTEMSTATE_AUTO;
    // quick and dirty debounce
    digitalWrite(LED_PIN, HIGH);
    delay(500);
    digitalWrite(LED_PIN, LOW);
  } //end if

  // if we are on auto
  if (systemState == SYSTEMSTATE_AUTO) {
    //check time pass ... we do it with int the other if - not to do the math all the time
    if (lastLEDStateChange + waitPeriod[ledState] < millis()) {
      setLEDState((ledState + 1) % 4); // move to nect led state
    } //end if
  }//end if

} //end loop


void setLEDState(byte newState) {
  switch (newState) {
    case LEDSTATE_GREEN:
      leds[LED_COLOR_RED] = CRGB::Black;
      leds[LED_COLOR_ORANGE] = CRGB::Black;
      leds[LED_COLOR_GREEN] = colorsIDX[LED_COLOR_GREEN];
      break;
    case LEDSTATE_ORANGE:
      leds[LED_COLOR_RED] = CRGB::Black;
      leds[LED_COLOR_ORANGE] = colorsIDX[LED_COLOR_ORANGE];
      leds[LED_COLOR_GREEN] = CRGB::Black;
      break;
    case LEDSTATE_RED:
      leds[LED_COLOR_RED] = colorsIDX[LED_COLOR_RED];
      leds[LED_COLOR_ORANGE] = CRGB::Black;
      leds[LED_COLOR_GREEN] = CRGB::Black;
      break;
    case LEDSTATE_RED_ORANGE:
      leds[LED_COLOR_RED] = colorsIDX[LED_COLOR_RED];
      leds[LED_COLOR_ORANGE] = colorsIDX[LED_COLOR_ORANGE];
      leds[LED_COLOR_GREEN] = CRGB::Black;
      break;
  } //end switch

  // set to current
  ledState = newState;
  //set last millis chnage
  lastLEDStateChange = millis();
  // show
  FastLED.show();
}

Credits

Tal O

Tal O

2 projects • 0 followers
Maker @ heart

Comments