I'm trying to code turn signals, but I can't get the light to blink. Any tips?

I have my Arduino and code set up to have a left and right turn signals and a brake light.

What works:

  • Hold the Brake button to activate, release to deactivate
  • Press left turn signal once to activate, press again to deactivate
  • Press right turn signal once to activate, press again to deactivate
  • If one turn signal is activated pressing the opposite one cancels the first and activates the second

What doesn't work:

  • When either of the turn signals are activated they only blink once and then stay activated (they need to keep blinking until turned off)
  • When I turn on the Arduino and start the code the default state of both turn signals is on (they need to start "off")

Do you mind looking at what I'm doing wrong?

My Code: (beware, I don't really know what I'm doing and I'm just reverse engineering bits of other code :stuck_out_tongue: )

int ledPinRed = 5;     // choose the pin for the LED
int inPinRed = 2;      // choose the input pin (for a pushbutton)
int valRed = 0;        // variable for reading the pin status

int inPinLeft = 3;     // Left Signal button
int outPinLeft = 6;    // Left Signal LED

int inPinRight = 4;    // Right Signal button
int outPinRight = 7;   // Right Signal LED

int stateLeft = HIGH;      // Left current state of the output pin
int readingLeft;           // Left current reading from the input pin
int previousLeft = LOW;    // Left previous reading from the input pin

int stateRight = HIGH;      // Right current state of the output pin
int readingRight;           // Right current reading from the input pin
int previousRight = LOW;    // Right previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(inPinLeft, INPUT);
  pinMode(outPinLeft, OUTPUT);
  pinMode(inPinRight, INPUT);
  pinMode(outPinRight, OUTPUT);

  pinMode(ledPinRed, OUTPUT);  // declare LED as output
  pinMode(inPinRed, INPUT);    // declare pushbutton as input
}

void loop()
{
  valRed = digitalRead(inPinRed);  // read input value
  if (valRed == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPinRed, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPinRed, HIGH);  // turn LED ON
}

  readingLeft = digitalRead(inPinLeft);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (readingLeft == HIGH && previousLeft == LOW && millis() - time > debounce) {
    if (stateLeft == HIGH)
      stateLeft = LOW;
    else
      stateLeft = HIGH,
      stateRight = LOW;

  digitalWrite(6, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(400);              // wait for a second
  digitalWrite(6, LOW);    // turn the LED off by making the voltage LOW
  delay(400);              // wait for a second

    time = millis();    
  }

  digitalWrite(outPinLeft, stateLeft);
  digitalWrite(outPinRight, stateRight);

  previousLeft = readingLeft;


readingRight = digitalRead(inPinRight);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (readingRight == HIGH && previousRight == LOW && millis() - time > debounce) {
    if (stateRight == HIGH)
      stateRight = LOW;
    else
      stateRight = HIGH,
      stateLeft = LOW;

  digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(400);              // wait for a second
  digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
  delay(400);              // wait for a second

    time = millis();    
  }

  digitalWrite(outPinRight, stateRight);
  digitalWrite(outPinLeft, stateLeft);

  previousRight = readingRight;

}

You want a state machine, so you can remember the difference between the light being on (and thus pressing the switch cancels it) and being off (so pressing the switch starts it).

I already had answered something similar recently, so here is a modified version which does what you ask. (Your stuff about the brake light was fine).

I'm using a small switch-management library from Gammon Forum : Electronics : Microprocessors : Switches tutorial which handles the state changes of the switches, and debouncing them.

#include <SwitchManager.h>

typedef enum {
    NONE,
    LH_DOWN,
    RH_DOWN,
    LH_LIGHT_ON,
    RH_LIGHT_ON
};

const unsigned long BLINK_INTERVAL = 500; // ms

// pin assignments
const byte LH_SWITCH_PIN = 2;
const byte RH_SWITCH_PIN = 3;
const byte BRAKE_SWITCH_PIN = 4;
const byte LH_LIGHT = A3;
const byte RH_LIGHT = A4;
const byte BRAKE_LIGHT = A5; 

SwitchManager LHswitch; 
SwitchManager RHswitch; 
 
byte state = NONE;
 
void handleLHPress (const byte newState, const unsigned long interval, const byte whichPin)
  {
  // switch down?
  if (newState == LOW)
     {
     switch (state)
       {
       // if already on, turn all off
       case LH_LIGHT_ON:
         state = NONE;
         break;
         
       // otherwise switch is now down, but not yet released
       default:
         state = LH_DOWN;
         break;
       }  // end of switch
     return;
     }  // end of LH switch down
  
  // switch must be up

  if (state == LH_DOWN)  // if down, switch to down-and-released mode
    state = LH_LIGHT_ON;  
  }  // end of handleLHPress
 
void handleRHPress (const byte newState, const unsigned long interval, const byte whichPin)
  {
  // switch down?
  if (newState == LOW)
     {
     switch (state)
       {
         
       // if already on, turn all off
       case RH_LIGHT_ON:
         state = NONE;
         break;
         
       // otherwise switch is now down, but not yet released
       default:
         state = RH_DOWN;
         break;
       }  // end of switch
     return;
     }  // end of RH switch down
  
  // switch must be up

  if (state == RH_DOWN)  // if down, switch to down-and-released mode
    state = RH_LIGHT_ON;  
  }  // end of handleRHPress
  
 
void setup ()
  {
  LHswitch.begin (LH_SWITCH_PIN, handleLHPress);
  RHswitch.begin (RH_SWITCH_PIN, handleRHPress);
  pinMode (BRAKE_SWITCH_PIN, INPUT_PULLUP);
  pinMode (LH_LIGHT, OUTPUT);
  pinMode (RH_LIGHT, OUTPUT);
  pinMode (BRAKE_LIGHT, OUTPUT);
  }  // end of setup

unsigned long lastBlink;
bool onCycle;
 
void blinkLights ()
  {
  lastBlink = millis ();
  onCycle = !onCycle;
  
  // default to off
  digitalWrite (LH_LIGHT, LOW);
  digitalWrite (RH_LIGHT, LOW);

  // every second time, turn them all off
  if (!onCycle)
    return;
    
  // blink light
  switch (state)
    {
    case NONE:
      break;
    
    case LH_DOWN:
    case LH_LIGHT_ON:
      digitalWrite (LH_LIGHT, HIGH);
      break;
      
    case RH_DOWN:
    case RH_LIGHT_ON:
      digitalWrite (RH_LIGHT, HIGH);
      break;
    
    }  // end of switch on state
    
  }  // end of blinkLights
  

void loop ()
  {
  LHswitch.check ();  // check for presses
  RHswitch.check ();  
  
  if (digitalRead (BRAKE_SWITCH_PIN) == LOW)
    digitalWrite (BRAKE_LIGHT, HIGH);
  else  
    digitalWrite (BRAKE_LIGHT, LOW);

  if (millis () - lastBlink >= BLINK_INTERVAL)
    blinkLights ();
    
  // other stuff
  }  // end of loop

Awesome! Thank you very much! This helps a lot, and I'll be sure to look over your code to learn for the future. :slight_smile: