[Solved] Control blinkers with Remote on model car

Hi,
total newbie trying to learn. I have successfully managed to get the remote to turn the blinkers on and off by pressing LEFT resp. RIGHT, but I want them to blink instead. Start to blink when I press LEFT, and stops when I press LEFT again.

I have attached my code.

#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>

int receiver = 11; // Signal Pin of IR receiver to Arduino Digital Pin 11
int LEDstatusFL = 0;
int LEDstatusBL = 0;
int LEDstatusLB = 0;
int LEDstatusRB = 0;
int LEDstatusLP = 0;

/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  irrecv.enableIRIn(); // Start the receiver
  pinMode(12, OUTPUT);
  digitalWrite(12, LOW);
  
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);

  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);

  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);
  

}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  if (irrecv.decode(&results)) // have we received an IR signal?

  {
    translateIR();
    irrecv.resume(); // receive the next value
  }
}/* --(end main loop )-- */

/*-----( Function )-----*/
void translateIR() // takes action based on IR code received

// describing Remote IR codes

{

  switch (results.value)

  {

    case 0xFF629D: Serial.println(" FORWARD"); TurnOnFrontLights(); break;
    case 0xFF22DD: Serial.println(" LEFT"); TurnOnLeftBlinker(); break;
    case 0xFF02FD: Serial.println(" -OK-"); TurnOnLicensePlate(); break;
    case 0xFFC23D: Serial.println(" RIGHT"); TurnOnRightBlinker(); break;
    case 0xFFA857: Serial.println(" REVERSE"); TurnOnBackLights(); break;
    case 0xFF6897: Serial.println(" 1");    break;
    case 0xFF9867: Serial.println(" 2");    break;
    case 0xFFB04F: Serial.println(" 3");    break;
    case 0xFF30CF: Serial.println(" 4");    break;
    case 0xFF18E7: Serial.println(" 5");    break;
    case 0xFF7A85: Serial.println(" 6");    break;
    case 0xFF10EF: Serial.println(" 7");    break;
    case 0xFF38C7: Serial.println(" 8");    break;
    case 0xFF5AA5: Serial.println(" 9");    break;
    case 0xFF42BD: Serial.println(" *");    break;
    case 0xFF4AB5: Serial.println(" 0");    break;
    case 0xFF52AD: Serial.println(" #");    break;
    case 0xFFFFFFFF: Serial.println(" REPEAT"); break;

    default:
      Serial.println(" other button   ");

  }// End Case

  delay(500); // Do not get immediate repeat

}


void TurnOnFrontLights()
{
if (LEDstatusFL==0){
 LEDstatusFL = 1;
digitalWrite(12,HIGH);
Serial.print("Status Strålkastare:");
Serial.println(LEDstatusFL);
}
else {
  digitalWrite(12,LOW);
  LEDstatusFL=0;
  Serial.print("Status Strålkastare:");
Serial.println(LEDstatusFL);
}
}

void TurnOnLeftBlinker()
{
if (LEDstatusLB==0){
 LEDstatusLB = 1;
digitalWrite(10,HIGH);
Serial.print("Status Vänster Blinkers:");
Serial.println(LEDstatusLB);
}
else {
  digitalWrite(10,LOW);
  LEDstatusLB=0;
  Serial.print("Status Vänster Blinkers:");
Serial.println(LEDstatusLB);
}
}

void TurnOnRightBlinker()
{
if (LEDstatusRB==0){
 LEDstatusRB = 1;
digitalWrite(9,HIGH);
Serial.print("Status Höger Blinkers:");
Serial.println(LEDstatusRB);
}
else {
  digitalWrite(9,LOW);
  LEDstatusRB=0;
  Serial.print("Status Höger Blinkers:");
Serial.println(LEDstatusRB);
}
}

void TurnOnLicensePlate()
{
if (LEDstatusLP==0){
 LEDstatusLP = 1;
digitalWrite(8,HIGH);
Serial.print("Status RegNr:");
Serial.println(LEDstatusLP);
}
else {
  digitalWrite(8,LOW);
  LEDstatusLP=0;
  Serial.print("Status RegNr:");
Serial.println(LEDstatusLP);
}
}

void TurnOnBackLights()
{
if (LEDstatusBL==0){
 LEDstatusBL = 1;
digitalWrite(13,HIGH);
Serial.print("Status Bakljus:");
Serial.println(LEDstatusBL);
}
else {
  digitalWrite(13,LOW);
  LEDstatusBL=0;
  Serial.print("Status Bakljus:");
Serial.println(LEDstatusBL);
}
}

Ok. You aren't far away from getting it to work.

First have the functions which turn on the blinkers also record the value of millis() when they went on. Use a global variable and give it a name like lastBlinkTime, or whatever makes sense to you.

Then write another function, maybe called blink() which looks at lastBlinkTime and whichever blinker is currently on and does a digitalWrite() as appropriate to make it blink.

In the main loop, outside of the if(){} statement, you need to call blink() so that it has an opportunity to do its job as often as possible.

Hello Morgan, and thanks or your help. I have still not been able to get it to work. Either the LED is blinking as soon as the board have booted, or I can only turn it on and off with the remote.

Any advice would be appreciated. At the moment I am concentrating on the Left blinker.

#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>

int receiver = 11; // Signal Pin of IR receiver to Arduino Digital Pin 11
int LEDstatusFL = 0;
int LEDstatusBL = 0;
int LEDstatusLB = 0;
int LEDstatusRB = 0;
int LEDstatusLP = 0;
int LbPressed = 0;
int RbPressed = 0;
unsigned long LastBlinkTime = 0;


/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");

  irrecv.enableIRIn(); // Start the receiver
  pinMode(12, OUTPUT);
  digitalWrite(12, LOW);

  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);

  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);

  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);


}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  if (irrecv.decode(&results)) // have we received an IR signal?

  {
    translateIR();
    irrecv.resume(); // receive the next value
   
  }
    TurnOnLeftBlinker();
  
}/* --(end main loop )-- */

/*-----( Function )-----*/
void translateIR() // takes action based on IR code received

// describing Remote IR codes

{

  switch (results.value)

  {

    case 0xFF629D: Serial.println(" FORWARD"); TurnOnFrontLights(); break;
    case 0xFF22DD: Serial.println(" LEFT");LbPressed=1; TurnOnLeftBlinker();
    if (LbPressed==1){LbPressed=0; break;} else {break;}
    case 0xFF02FD: Serial.println(" -OK-"); TurnOnLicensePlate(); break;
    case 0xFFC23D: Serial.println(" RIGHT"); TurnOnRightBlinker(); break;
    case 0xFFA857: Serial.println(" REVERSE"); TurnOnBackLights(); break;
    case 0xFF6897: Serial.println(" 1");    break;
    case 0xFF9867: Serial.println(" 2");    break;
    case 0xFFB04F: Serial.println(" 3");    break;
    case 0xFF30CF: Serial.println(" 4");    break;
    case 0xFF18E7: Serial.println(" 5");    break;
    case 0xFF7A85: Serial.println(" 6");    break;
    case 0xFF10EF: Serial.println(" 7");    break;
    case 0xFF38C7: Serial.println(" 8");    break;
    case 0xFF5AA5: Serial.println(" 9");    break;
    case 0xFF42BD: Serial.println(" *");    break;
    case 0xFF4AB5: Serial.println(" 0");    break;
    case 0xFF52AD: Serial.println(" #");    break;
    case 0xFFFFFFFF: Serial.println(" REPEAT"); break;

    default:
      Serial.println(" other button   ");

  }// End Case

  delay(500); // Do not get immediate repeat

}


void TurnOnFrontLights()
{
  if (LEDstatusFL == 0) {
    LEDstatusFL = 1;
    digitalWrite(12, HIGH);
    Serial.print("Status Strålkastare:");
    Serial.println(LEDstatusFL);
  }
  else {
    digitalWrite(12, LOW);
    LEDstatusFL = 0;
    Serial.print("Status Strålkastare:");
    Serial.println(LEDstatusFL);
  }
}

void TurnOnLeftBlinker()
{
  unsigned long now = millis();

  const long interval = 500;
 if (LbPressed==1)
 {
  Serial.print("now: ");
  Serial.println(now);
  Serial.print("LastBlinkTime: ");
  Serial.println(LastBlinkTime);
  Serial.print("interval: ");
  Serial.println(interval);
  Serial.print("Status Vänster Blinker: ");
  Serial.println(LEDstatusLB);
  Serial.print("Now-Lastblinktime= ");
  Serial.println(now - LastBlinkTime);

  if ((now - LastBlinkTime) > interval && LEDstatusLB == 0) {
    LEDstatusLB = 1;
    LastBlinkTime = now;
    digitalWrite(10, HIGH);
    Serial.print("Status Vänster Blinker:");
    Serial.println(LEDstatusLB);
    
    
  }
  if ((now - LastBlinkTime) > interval && LEDstatusLB == 1) {
    LEDstatusLB = 0;
    LastBlinkTime = now;
    digitalWrite(10, LOW);
    Serial.print("Status Vänster Blinker:");
    Serial.println(LEDstatusLB);
       }
 }
}

void TurnOnRightBlinker()
{
  if (LEDstatusRB == 0) {
    LEDstatusRB = 1;
    digitalWrite(9, HIGH);
    Serial.print("Status Höger Blinker:");
    Serial.println(LEDstatusRB);
  }
  else {
    digitalWrite(9, LOW);
    LEDstatusRB = 0;
    Serial.print("Status Höger Blinker:");
    Serial.println(LEDstatusRB);
  }
}

void TurnOnLicensePlate()
{
  if (LEDstatusLP == 0) {
    LEDstatusLP = 1;
    digitalWrite(8, HIGH);
    Serial.print("Status RegNr:");
    Serial.println(LEDstatusLP);
  }
  else {
    digitalWrite(8, LOW);
    LEDstatusLP = 0;
    Serial.print("Status RegNr:");
    Serial.println(LEDstatusLP);
  }
}

void TurnOnBackLights()
{
  if (LEDstatusBL == 0) {
    LEDstatusBL = 1;
    digitalWrite(13, HIGH);
    Serial.print("Status Bakljus:");
    Serial.println(LEDstatusBL);
  }
  else {
    digitalWrite(13, LOW);
    LEDstatusBL = 0;
    Serial.print("Status Bakljus:");
    Serial.println(LEDstatusBL);
  }
}

Spread out your big switch() statement. You have too much to fit on one line.

Now look at the case for the left blinker. Should pressing that button on the remote flip the state of LbPressed? What happens if you hold the button down?

Then look at the code in TurnOnLeftBlinker() I think it should turn the output off when not pressed.

I think you're making it more complicated than it has to be, spehe. The only difference between flashing the left blinker or right blinker is which pin you are setting, so you don't have to call different functions for left and right, just set the proper pin number and toggle it on/off.

The way I would probably do this is to hold the pin number of the blinker that is on in a variable. If no blinker is on it can be an illegal pin number, say -1. When a blinker button is pressed it either turns the blinker on if it is off or off if it is already on.

Each time through loop() if a blinker is on it gets updated, toggling its state based on an interval.

See my demo code below. I can only test it by watching the serial monitor but think that it's working. You could test it on your setup by using the 'L' key for left and 'R' key for right.

const unsigned long interval = 500;
unsigned long LastBlinkTime = 0;

const int leftBlinkerPin = 10;
const int rigthBlinkerPin = 9;
int blinkerPin = (-1);


void setup()
{

  Serial.begin(9600);
  Serial.println("blinker test");
  pinMode(leftBlinkerPin, OUTPUT);
  pinMode(rigthBlinkerPin, OUTPUT);
}

void loop()
{
  byte in;

  if ( Serial.available() ) in = Serial.read();

  switch (in)
  {
    case 'l':
    case 'L':
    
      // left blinker button
      Serial.println("Left");
      blinkerButton(leftBlinkerPin);

      break;

    case 'r':
    case 'R':
    
      // right blinker button
      Serial.println("Right");
      blinkerButton(rigthBlinkerPin);
       break;

    default:
      break;

  } // switch

  // if a blinker is on
  if ( blinkerPin > 0 ) updateBlinker();

}

void blinkerButton(int pinNo)
{
  // set both blinkers off
  digitalWrite(leftBlinkerPin, LOW);
  digitalWrite(rigthBlinkerPin, LOW);

  // set new blinker pin or toggle if alrady on
  blinkerPin = (blinkerPin == pinNo) ? -1 : pinNo;

  // mark time
  LastBlinkTime = 0UL;
  
}

void updateBlinker()
{
  unsigned long timeNow = millis();

  // if it's time to blink
  if ( (timeNow - LastBlinkTime) >= interval) {

    // toggle LED
    digitalWrite(blinkerPin, !digitalRead(blinkerPin));

    // mark time
    LastBlinkTime = timeNow;

    
    Serial.print(blinkerPin);
    Serial.print("  ");
    Serial.println(digitalRead(blinkerPin));
  }

}

Hi Morgan and Blue Eyes. Thanks for your help!
I finally managed to get it to work, and now I can turn on both Left and right blinker. This is my working code:

#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>

int receiver = 11; // Signal Pin of IR receiver to Arduino Digital Pin 11
int LEDstatusFL = 0;
int LEDstatusBL = 0;
int LEDstatusLB = 0;
int LEDstatusRB = 0;
int LEDstatusLP = 0;
int LbPressed = 0;
int RbPressed = 0;
unsigned long LastBlinkTime = 0;


/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");

  irrecv.enableIRIn(); // Start the receiver
  pinMode(12, OUTPUT);
  digitalWrite(12, LOW);

  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);

  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);

  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);


}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  if (irrecv.decode(&results)) // have we received an IR signal?

  {
    translateIR();
    irrecv.resume(); // receive the next value
   
  }
    TurnOnLeftBlinker();
    TurnOnRightBlinker();
  
}/* --(end main loop )-- */

/*-----( Function )-----*/
void translateIR() // takes action based on IR code received

// describing Remote IR codes

{

  switch (results.value)

  {

    case 0xFF629D: Serial.println(" FORWARD"); TurnOnFrontLights(); break;
    case 0xFF22DD: Serial.println(" LEFT"); TurnOnLeftBlinker();
    if (LbPressed==1){LbPressed=0; digitalWrite(10, LOW); break;} else {LbPressed=1; break;}
    case 0xFF02FD: Serial.println(" -OK-"); TurnOnLicensePlate(); break;
    case 0xFFC23D: Serial.println(" RIGHT"); TurnOnRightBlinker();
    if (RbPressed==1){RbPressed=0; digitalWrite(9, LOW); break;} else {RbPressed=1; break;}
    case 0xFFA857: Serial.println(" REVERSE"); TurnOnBackLights(); break;
    case 0xFF6897: Serial.println(" 1");    break;
    case 0xFF9867: Serial.println(" 2");    break;
    case 0xFFB04F: Serial.println(" 3");    break;
    case 0xFF30CF: Serial.println(" 4");    break;
    case 0xFF18E7: Serial.println(" 5");    break;
    case 0xFF7A85: Serial.println(" 6");    break;
    case 0xFF10EF: Serial.println(" 7");    break;
    case 0xFF38C7: Serial.println(" 8");    break;
    case 0xFF5AA5: Serial.println(" 9");    break;
    case 0xFF42BD: Serial.println(" *");    break;
    case 0xFF4AB5: Serial.println(" 0");    break;
    case 0xFF52AD: Serial.println(" #");    break;
    case 0xFFFFFFFF: Serial.println(" REPEAT"); break;

    default:
      Serial.println(" other button   ");

  }// End Case

  delay(500); // Do not get immediate repeat

}


void TurnOnFrontLights()
{
  if (LEDstatusFL == 0) {
    LEDstatusFL = 1;
    digitalWrite(12, HIGH);
    Serial.print("Status Strålkastare:");
    Serial.println(LEDstatusFL);
  }
  else {
    digitalWrite(12, LOW);
    LEDstatusFL = 0;
    Serial.print("Status Strålkastare:");
    Serial.println(LEDstatusFL);
  }
}

void TurnOnLeftBlinker()
{
  unsigned long now = millis();

  const long interval = 500;
 if (LbPressed==1)
 {
  Serial.print("now: ");
  Serial.println(now);
  Serial.print("LastBlinkTime: ");
  Serial.println(LastBlinkTime);
  Serial.print("interval: ");
  Serial.println(interval);
  Serial.print("Status Vänster Blinker: ");
  Serial.println(LEDstatusLB);
  Serial.print("Now-Lastblinktime= ");
  Serial.println(now - LastBlinkTime);

  if ((now - LastBlinkTime) > interval && LEDstatusLB == 0) {
    LEDstatusLB = 1;
    LastBlinkTime = now;
    digitalWrite(10, HIGH);
    Serial.print("Status Vänster Blinker:");
    Serial.println(LEDstatusLB);
    
    
  }
  if ((now - LastBlinkTime) > interval && LEDstatusLB == 1) {
    LEDstatusLB = 0;
    LastBlinkTime = now;
    digitalWrite(10, LOW);
    Serial.print("Status Vänster Blinker:");
    Serial.println(LEDstatusLB);
       }
 }
}

void TurnOnRightBlinker()
{
  unsigned long now = millis();

  const long interval = 500;
 if (RbPressed==1)
 {
  Serial.print("now: ");
  Serial.println(now);
  Serial.print("LastBlinkTime: ");
  Serial.println(LastBlinkTime);
  Serial.print("interval: ");
  Serial.println(interval);
  Serial.print("Status Höger Blinker: ");
  Serial.println(LEDstatusRB);
  Serial.print("Now-Lastblinktime= ");
  Serial.println(now - LastBlinkTime);

  if ((now - LastBlinkTime) > interval && LEDstatusRB == 0) {
    LEDstatusRB = 1;
    LastBlinkTime = now;
    digitalWrite(9, HIGH);
    Serial.print("Status Höger Blinker:");
    Serial.println(LEDstatusRB);
    
    
  }
  if ((now - LastBlinkTime) > interval && LEDstatusRB == 1) {
    LEDstatusRB = 0;
    LastBlinkTime = now;
    digitalWrite(9, LOW);
    Serial.print("Status Höger Blinker:");
    Serial.println(LEDstatusRB);
       }
 }
}

void TurnOnLicensePlate()
{
  if (LEDstatusLP == 0) {
    LEDstatusLP = 1;
    digitalWrite(8, HIGH);
    Serial.print("Status RegNr:");
    Serial.println(LEDstatusLP);
  }
  else {
    digitalWrite(8, LOW);
    LEDstatusLP = 0;
    Serial.print("Status RegNr:");
    Serial.println(LEDstatusLP);
  }
}

void TurnOnBackLights()
{
  if (LEDstatusBL == 0) {
    LEDstatusBL = 1;
    digitalWrite(13, HIGH);
    Serial.print("Status Bakljus:");
    Serial.println(LEDstatusBL);
  }
  else {
    digitalWrite(13, LOW);
    LEDstatusBL = 0;
    Serial.print("Status Bakljus:");
    Serial.println(LEDstatusBL);
  }
}