Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15287

IR remote control

$
0
0

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;
  }
}

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 15287

Trending Articles