IR remote control

Hi all,
spend more than a day to solve my problem, no success......

I have written a program for a model railway, controlling a car.
It has a build in ESP D1. So far so good. all the routines work fine.

Now the next step, to control it remotely with an onboard IR receiver and a transmitter.

I have split all the actions into several loops.

the IR receiver loop are remote() and decode()
If I have both active I see the codes I transmit.
If I active the loop Flashleds it still works fine.

But if I active the loop blinker ( I want to control the left and the right car blinker) it goes wrong.

What happends:
After startup all the codes I sent are recognised. But as soon as I sent either the code for the left blinker or the code for the right blinker the right blinker starts blinking but from then on no codes are seen by the receiver.
This is a result seen on the serial monitor:
remote_irResult DEC / HEX: 28 / 1C

remote_irResult DEC / HEX: 28 / 1C

remote_irResult DEC / HEX: 28 / 52

remote_irResult DEC / HEX: 82 / 52

remote_irResult DEC / HEX: 82 / 52

remote_irResult DEC / HEX: 82 / 52

remote_irResult DEC / HEX: 82 / 5A

remote_irResult DEC / HEX: 90 / 5A

remote_irResult DEC / HEX: 90 / 5A

remote_irResult DEC / HEX: 90 / 5A

remote_irResult DEC / HEX: 90 / 5A

so a number of codes are listed, as soon as I sent code 5A (line 140 on the sketch) for the right blinker its the last code I see.

Its even worse if I uncomment everything in the main loop. Then I do not see a single IR reading

Hope for some good ideas, code below:

/*
V10
To do:
Richtingaanwijzers werken maar nog remote aan te sturen
Remote aansturen snelheid, maxspeed?

Led helderheid aansturen met analogwrite:
  done on Blinkers
  done on Brake
  not possible on Flash lights
  on front and rear lights?
  D0 is planned to be used for IR data input, temporaly D5 is used for cruise, 
  has to be combined, one output free for front and/or rear lights

Done:
Led helderheid aansturen met analogwrite:
  done on Blinkers
  done on Brake
  not possible on Flash lights
  on front and rear lights?
  D0 is planned to be used for IR data input, temporaly D5 is used for cruise, 
  has to be combined, one output free for front and/or rear lights
check Powersupply 
Accelerate, Cruise and brake working
Blauw Flash werkend
Delay na break ok

The ESP module uses a different numbering as the Arduino's.
Port    GPIO
D0      16    Cruise
D1      5     Flash
D2      4     stopplace (IR receive Bottom)
D3      0     speed motor
D4      2     Brake Led
D5      14    IR input Front / later output rear red lights
D6      12    Blink Left
D7      13    Blink Right
D8      15    Front white leds
A0      A0    Power supply check
*/

#include <IRremote.h>
#include "ESP8266WiFi.h"

ADC_MODE(ADC_VCC);                             // Read power status.
int leftBlink = 0;                             // on 1 leftblinker is on, IR controlled
int rightBlink = 0;                            // on 1 rightblinker is on, IR controlled
int irResult;                                  //stores IR input
int brakeOff = 0;                              //Brake off Led intensity
int brakeOn = 200;                             //Brake on Led intensity
int blinkOff = 0;                              //Brake off Led intensity
int blinkOn = 200;                             //Brake on Led intensity
int RECEIVER_PIN = 14;                         // IR input Front
int analogPin = A0;                            // input battery value
int val = 0;                                   // variable to store the value read
int maxspeed = 250;                            // Max allowed speed
int minspeed = 0;                              // Minimal speed
int speed;                                     // stores actual speed
int currentspeed;                              // remembers actual speed
int flash;                                     // Blue Flashing light on top Cabin
int stopPlace;                                 // stopPlace detected, port 4
int stop = 1;                                  // remembers detected stopplace
int wait = 0;                                  // WaitTime on stopplace
int distance;                                  // car detects another vehicle in front, port 14
const int blinkLeft = (12);                    // blinker Left, port 12. (D6)
const int blinkRight = (13);                   // blinker Right, port 13 (D7)
const int FlashLed = (5);                      // Flash lights, port 5 (D1)
int waitTime = 2000;                           // Time the vehicle waits before increasing speed again
int ledBlink = LOW;                            // ledState used to set the BlinkLED
int ledFlash = LOW;                            // ledState used to set the FlashLED                    // initial start time
unsigned long previousMillisBrake = 0;         // will store last time Brake was updated
const long brakeInterval = 200;                // Brake interval
unsigned long previousMillisBlink = 0;         // will store last time Blink was updated
const long blinkInterval = 300;                // Blink interval
unsigned long previousMillisAccelerate = 0;    // will store last time Accelerate was updated
const long AccelerateInterval = 200;           // Accelerate interval
unsigned long previousMillisWait = 0;          // will store last time Waittime was updated
const long WaitInterval = 5000;                // Waittime interval
unsigned long previousMillisCruiseSlower = 0;  // will store last time CruiseSlowdown was updated
const long CruiseSlowerInterval = 300;         // CruiseSlower interval
unsigned long previousMillisCruiseFaster = 0;  // will store last time CruiseFaster was updated
const long CruiseFasterInterval = 300;         // Cruise Faster interval
unsigned long previousMillisFlash = 0;         // will store last time Flash lights was updated
const long FlashInterval = 1000;               // FlasLight interval

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  IrReceiver.begin(RECEIVER_PIN);

  pinMode(4, INPUT);    // IR sensor StopPlace bottom vehicle, HIGH = stop
  pinMode(16, INPUT);   // IR sensor "adaptive" cruise control front vehicle, HIGH = stop
  pinMode(14, INPUT);   // IR input Blink
  pinMode(5, OUTPUT);   // Flash
  pinMode(0, OUTPUT);   // speed motor
  pinMode(2, OUTPUT);   // Brake Led
  pinMode(12, OUTPUT);  // blinker Left
  pinMode(13, OUTPUT);  // blinker Right
}

void loop() {

  remote();  //Remote control
  decode();  //extract IR signaal
  // checkPowerSupply();  // brakelights flashing if power supply is 20% down
  // Accelerate();     // car increases speed
  // Brake();          // car detects a stopplace
  // Wait();           // Wait time after detecting stopplace
  // checkDistance();  // car detects another vehicle in front
  blinker();    // car detects another vehicle in front
  FlashLeds();  // blue Flash Leds constantly on
}
void remote() {

  if (IrReceiver.decode()) {
    IrReceiver.resume();
    Serial.print("remote_irResult DEC / HEX: ");
    Serial.print(irResult, DEC);
    Serial.print(" / ");
    Serial.println(IrReceiver.decodedIRData.command, HEX);
    irResult = (IrReceiver.decodedIRData.command);
  }
}

void decode() {
  // Serial.print("irResult. ");
  // Serial.println(irResult, HEX);
  switch (irResult) {

    case 0x1C:  // ok button switched blinkers of
      rightBlink = 0;
      leftBlink = 0;
      break;

    case 0x8:
      //Serial.println("LeftBlink");
      leftBlink = 1;
      break;

    case 0x5A:
      //Serial.println("RightBlink");
      rightBlink = 1;
      break;
  }
}


void checkPowerSupply() {

  float val = ESP.getVcc() / 1024.0;
  if (val < 2) {  // execution of the Sketch stops on battery low, threshold to be determined
    Serial.print("Voltage: ");
    Serial.println(val);
    Serial.println("V");
    Serial.println("Battery low");
    analogWrite(2, brakeOn);
    delay(500);
    analogWrite(2, brakeOff);
    delay(500);
  } else {
    //Serial.println("Battery OK");
  }
}
void Accelerate() {
  stopPlace = digitalRead(4);
  distance = digitalRead(16);
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillisAccelerate >= AccelerateInterval) {
    previousMillisAccelerate = currentMillis;
    if (speed < maxspeed) {
      if (stop == 1 && distance == 1 && wait == 0) {
        analogWrite(2, brakeOff);  //Brake off, this code is needed for Cruise
        speed = speed + 10;
        currentspeed = speed;
        analogWrite(0, speed);
        Serial.print("speedAccelerate :");
        Serial.println(speed);
        Serial.println("");
      }
    }
  }
}

void checkDistance() {

  int Distance = digitalRead(16);
  //Serial.println(Distance);
  if (Distance == 0) {
    //while (Distance ==0)
    //Serial.println("obstacle in front");
    Cruise();
    //Serial.println (Distance);
    //delay(500);
  }
}

void Brake() {
  stopPlace = digitalRead(4);
  if (stopPlace == 0 && distance == 1 && wait == 0) {  // if a stopplace  is passed the car slows down to 0,  Distance should be not active,
                                                       //in active distance state its handled there
    stop = 0;
    analogWrite(2, brakeOn);  //Brake on
  }
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillisBrake >= brakeInterval) {
    previousMillisBrake = currentMillis;
    if (stop == 0) {
      if (speed >= 10) {
        speed = speed - 10;
        analogWrite(0, speed);
        Serial.print("speedBrake: ");
        Serial.println(speed);
        analogWrite(2, brakeOn);  //Brake on
        currentspeed = speed;
      }
    }
    if (speed < 10) {
      stop = 1;  // reset
      wait = 1;
      analogWrite(2, brakeOff);  //Brake off
    }
  }
}
void Cruise() {
  int distance = digitalRead(16);
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillisCruiseSlower >= CruiseSlowerInterval) {
    previousMillisCruiseSlower = currentMillis;
    if (distance == 0) {
      analogWrite(2, brakeOn);  //Brake on
      speed = (speed - 10);
      analogWrite(0, speed);
      Serial.print("speedCruise: ");
      Serial.println(speed);
    }
  }

  if (currentMillis - previousMillisCruiseFaster >= CruiseFasterInterval) {
    previousMillisCruiseFaster = currentMillis;
    if (distance == 1) {
      analogWrite(2, brakeOff);  //Brake off
      Serial.println("Faster");
      speed = (speed + 10);
      analogWrite(0, speed);
    }
  }
}




void blinker() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillisBlink >= blinkInterval && leftBlink == 1) {
    previousMillisBlink = currentMillis;
    analogWrite(12, ledBlink);
    if (ledBlink < blinkOn) {
      ledBlink = blinkOn;
    } else {
      ledBlink = blinkOff;
    }
  }
  if (currentMillis - previousMillisBlink >= blinkInterval && rightBlink == 1) {
    previousMillisBlink = currentMillis;
    analogWrite(13, ledBlink);
    if (ledBlink < blinkOn) {
      ledBlink = blinkOn;
    } else {
      ledBlink = blinkOff;
    }
  }
}

void FlashLeds() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillisFlash >= FlashInterval) {
    previousMillisFlash = currentMillis;
    analogWrite(5, ledFlash);
    if (ledFlash < 50) {
      ledFlash = 400;
    } else {
      ledFlash = 0;
    }
  }
}

void Wait() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillisWait >= WaitInterval) {
    previousMillisWait = currentMillis;
    Serial.println("wait");
    wait = 0;
  }
}

Try a default case for "did not match" that clears irResult (changes irResult from 0x1c, 0x08, 0x5a)

void decode() {
  // Serial.print("irResult. ");
  // Serial.println(irResult, HEX);
  switch (irResult) {

    case 0x1C:  // ok button switched blinkers of
      rightBlink = 0;
      leftBlink = 0;
      break;

    case 0x8:
      //Serial.println("LeftBlink");
      leftBlink = 1;
      break;

    case 0x5A:
      //Serial.println("RightBlink");
      rightBlink = 1;
      break;
  }
}

Thanks, will try that

I still do not get it working, what am I doing wrong.....?

For readability I have got rid of all irrelevant code and did put it into one loop and made it as simple as I could:

//Blink test

#include <IRremote.h>

int leftBlink = 0;                             // on 1 leftblinker is on, IR controlled
int rightBlink = 0;                            // on 1 rightblinker is on, IR controlled
int irResult;                                  //stores IR input
const int RECEIVER_PIN = 14;  
int L_blinkOff = 0;                            //Brake off Led intensity
int L_blinkOn = 200;                           //Brake on Led intensity
int R_blinkOff = 0;                            //Brake off Led intensity
int R_blinkOn = 200;                           //Brake on Led intensity
const int blinkLeft = (12);                    // blinker Left, port 12. (D6)
const int blinkRight = (13);                   // blinker Right, port 13 (D7)
int L_ledBlink = LOW;                          // ledState used to set the BlinkLED
int R_ledBlink = LOW;                          // ledState used to set the BlinkLED
unsigned long previousMillisL_Blink = 0;       // will store last time Blink was updated
const long L_blinkInterval = 200;              // Blink interval
unsigned long previousMillisR_Blink = 0;       // will store last time Blink was updated


void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  IrReceiver.begin(RECEIVER_PIN);

  pinMode(12, OUTPUT);  // blinker Left
  pinMode(13, OUTPUT);  // blinker Right
}

void loop() {

  if (IrReceiver.decode()) {
    IrReceiver.resume();
    Serial.print("remote_irResult DEC / HEX: ");
    Serial.print(irResult, DEC);
    Serial.print(" / ");
    Serial.println(IrReceiver.decodedIRData.command, HEX);
    irResult = (IrReceiver.decodedIRData.command);

    if (irResult == 0x8) {
      leftBlink = 1;
      Serial.println(irResult, DEC);
    }
    else if (irResult == 0x5A) {
      rightBlink = 1;
    } else {
    }
  }
  if (leftBlink == 1) {
    analogWrite(12, L_ledBlink);
    if (L_ledBlink < L_blinkOn) {
      L_ledBlink = L_blinkOn;
      delay(200);
    } else {
      L_ledBlink = L_blinkOff;
      delay(200);
    }
  }
  else if (rightBlink == 1) {
    analogWrite(13, R_ledBlink);
    if (R_ledBlink < R_blinkOn) {
      R_ledBlink = R_blinkOn;
      delay(100);
    } else {
      R_ledBlink = R_blinkOff;
      delay(200);
    }
  }
}

I can press on all remote switches and can read all the belonging codes.
If I press left arrow or right arrow, the left of right led is starting to blink and no input is red anymore until I press the reset button...

If I change the code as follows:

    if (irResult == 0x8) {
      leftBlink = 2;
      Serial.println(irResult, DEC);
    }
    else if (irResult == 0x5A) {
      rightBlink = 2;
    } else {
    }

The obviously the leds do not blink AND all codes a red including 0x8 and 0x5A.

So it seems the loops to control the leds on/off do block reading the IR input?????

The IR part of your sketch seems to work, but this sketch does not make any LEDs turn on.

Without using the IR remote... make your LEDs work by using a push button or a Serial.read();... then use the IR data to make the LEDs light.

sorry but the leds do lid as they should........see my previous post:<[the left of right led is starting to blink]
it's just that I can switch on one of the 2 and then no IR readings anymore

Maybe you are controlling the cathode side... I don't know... you have your devices, drawings and IDE right in front of you... I do not. I see things like this...

... and said something about your code that did not look right. ... and you say...

I know that your code is doing exactly as you have instructed it. Enjoy your project.

Thanks but no answer on why the leds work fine until I implement ir remote….

Say what? You think your code was just fine until what... I started trying to help?

Because you wrote it wrong.

I walk a dog, moved some furniture, cleaned a room or two, made a meal, sat for a few minutes and made this...

[edit]I got left and right backwards[/edit]
[edit] removed hard-coded left/right values... oops[/edit]

#include <IRremote.h>
int irResult;

const int RECEIVER_PIN = 14;
const int ledLeft = 12;
const int ledRight = 13;
const int leftIR  = 0x08;
const int rightIR = 0x5A;

unsigned long previousMillis = 0;
unsigned long currentMillis;
unsigned long interval = 200;

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(RECEIVER_PIN);
  pinMode(ledLeft, OUTPUT);
  pinMode(ledRight, OUTPUT);
}

void loop() {
  if (IrReceiver.decode()) {
    irResult = (IrReceiver.decodedIRData.command);
    if (irResult) {
      IrReceiver.resume();
      Serial.print("irResult: 0x");
      Serial.println(irResult, HEX);
    }
  }
  if (irResult == leftIR || irResult == rightIR) { // << I hard coded this... changed to leftIR/rightIR
    if (irResult == leftIR) {
      digitalWrite(ledRight, LOW);
      blinkLED(ledLeft);
    }
    if (irResult == rightIR) {
      digitalWrite(ledLeft, LOW);
      blinkLED(ledRight);
    }
  } else {
    digitalWrite(ledLeft, LOW);
    digitalWrite(ledRight, LOW);
  }
}

void blinkLED(int whichSide) {
  currentMillis = millis();
  if ((currentMillis - previousMillis) > interval) {
    previousMillis = currentMillis;
    digitalWrite (whichSide, !digitalRead(whichSide));
  }
}

Learn to NOT blame the code.
directionalindicators

2 Likes
  • Make a New Year’s resolution to start commenting your lines of code.
1 Like

Yessir.

That was meant for the OP.

sorry to upset anyone, that was not what I meant to do. I am not native English speaking. I appreciate any help. I will check the code above and let you know, thanks again.

  • Not upset.

  • Commenting your code helps you and helps us communicate.

1 Like

Ah, relieved:)
I just tried the code, the serial monitor shows al the codes I sent, that's nice, and continues to do so if I sent either 0x08 or 0x5A;

But the leds stay OFF. If I change all the LOW to HIGH the Leds stay on, so no flashing on and off.
I have to dive into the code to understand why. Will get back to it

I probably made mistakes and wrote values double, or backwards (to adapt my IR code to your IR code). Here is a simulation... Press << or >> and then any other key turns blinking OFF.

1 Like

great, see it working in your very nice simulation, guess I should be able to get it working too

Try using "functions" to make your code "modular" - this means, when you make a section of code work int "loop()", give that section of code its own function... see how "blinkLED()" is in its own function and "receives" a value for "left" or "right"? After you get your code working, see if you can have a "decode my results" and a "blink this or that LED" as separate functions.

I have done that, see my first post:)
Later I have just picked out the relevant code for the blinkers and IR remote to get a better overview

It works fine now!!!!

Had to change one line:
// if (irResult == 0xE0 || irResult == 0x90) {
into
if (irResult == 0x8 || irResult == 0x5A) {

Thanks a lot for the help, have to implement it now in the other sketch.

1 Like