Problem with IR receiver

Hello guys...I have almost finished my diplomatic project but this just keeps happening...

I am trying to turn on and off a vehicle with an IR remote control. It always turns on but after that it seems like it loses the communication and won't stop or obey any other commands. It looks like it gets stuck somewhere.

{ 
  if (irrecv.decode(&results)) {
    ir=(results.value);
    irrecv.resume(); 
  }
  if (ir==16720605) {
    onoff=1;
    delay(100);
    ir=0;
  }
  
  if (ir==16753245) {
    onoff=0;
    delay(100);
    ir=0;
  }
 if (onoff==1) { do this }
 if (onoff==0) {do that}

Although it always turns on (therefore onoff is 1 when I press the button) it then sticks to that and won't go back to 0 so the whole vehicle goes nuts.

Any opinions? Thanks...

Any opinions?

Sure. Snippets-R-us is down the road a ways. We want to see ALL of your code.

It's kinda long that's why I didn't post it all at once...just the IR parts are bothering me everything else works fine...this is it:

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

//DHLWSH METAVLHTWN

Ultrasonic ultrasonic( 12, 13 );
int x,s1,s2,s3,s4,g1,g2,onoff,dist1,dist2,k,x5144,x4539; //x:distance,s1:speed,g1+g2:apenergopoiites,dist:epithimiti apostasi
  const int motor1Pin1 = 9;    // H-bridge leg 1 (pin 2, 1A)
  const int motor1Pin2 = 10;    // H-bridge leg 2 (pin 7, 2A)
  const int motor2Pin1 = 5;
  const int motor2Pin2 = 6;
  const int enablePin = 8;  // H-bridge enable pin
unsigned long t1,t2,t3,t4,tt,p1,p2,ir; //t1,t2:xronoi gia freno,p1,p2:xronoi allagis s1,ir:received button

int right_ldr = 1;//analog pin to which LDR is connected
int left_ldr = 2; 
int right_value = 0;        //variable to store LDR values
int left_value = 0;
int x4439;
int x5145;

int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;

//TELOS DHLWSHS METAVLHTWN

void setup()
{
  Serial.begin( 9600 );
    pinMode(motor1Pin1, OUTPUT); 
    pinMode(motor1Pin2, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT); 
    pinMode(enablePin, OUTPUT);
    digitalWrite(enablePin, HIGH); 
    s1=0;
    g1=0;
    g2=0;
    irrecv.enableIRIn(); // Start the receiver
    ir=0;
    onoff=0;
    dist1=41;
    dist2=30;
    x4539=0;
    x5144=0;
}

void loop()
{ // RECEIVED IR
  if (irrecv.decode(&results)) {
    ir=(results.value);
    irrecv.resume(); // Receive the next value
  }
  // EPEKSERGASIA IR
  if (ir==16720605) {
    onoff=1;
    s1=85;
    s2=86;
    delay(100);
    ir=0;
  }
  
  if (ir==16753245) {
    onoff=0;
    s1=0;
    delay(100);
    ir=0;
  }
  
  if (ir==16761405) {
    ++dist1;
    ++dist2;
    delay(100);
    ir=0;
  }
  
  if (ir==16712445) {
    --dist1;
    --dist2;
    delay(100);
    ir=0;
  }
  
  if (ir==16748655) {
    ++s2;
    ++s2;
    delay(100);
    ir=0;
  }
  
  if (ir==16754775) {
    --s2;
    --s2;
    if (s1>s2) {
      --s1;
      --s1;
    }
    delay(100);
    ir=0;
  }
  //TELOS IR
  

      
  //ULTRASONIC
  
  x=ultrasonic.Ranging(CM);

  //TELOS ULTRASONIC
  
  right_value = analogRead(right_ldr);
  left_value = analogRead(left_ldr);  

  if (onoff==1){
    
    if (right_value<220){
      if (left_value<500){
        s3=0;
        s4=0;
      }
      else if (left_value>500) {
        s3=0;
        s4=200;
      }
    }
    else if (right_value>220) {
      s3=200;
      s4=0;
    }
  
    
  
      // FRENO
  if (x>40) {
     k=0;
     x5144=0;
     x4439=0;

  }
  if (x<41) {
  
  if (x<41&&x>30){
    if (x5144==0){
     t3=millis();
     ++x5144;
    }
  }
  if (x<31&&x>20){
    if (x4539==0) {
     t4=millis();
     ++x4539;
    }
  }
  if (x<20){
    x5144=0;
    x4539=0;
    tt=(t4-t3);
    
  if (tt<1000) {
    if (k==0) {
     analogWrite(motor1Pin1, 0);
     analogWrite(motor1Pin2, 0);
     analogWrite(motor2Pin1, s3);
     analogWrite(motor2Pin2, s4);
     delay(200);
     
     analogWrite(motor1Pin1, 0);
     analogWrite(motor1Pin2, 80);
     analogWrite(motor2Pin1, s3);
     analogWrite(motor2Pin2, s4);
     delay(1000);
     
     analogWrite(motor1Pin1, 0);
     analogWrite(motor1Pin2, 0);
     analogWrite(motor2Pin1, s3);
     analogWrite(motor2Pin2, s4);
     
     ++k;
     onoff=0;
     
  }
  }       
}
} // TELOS FRENOU
  
  // AYKSOMEIWSH APOSTASHS
  if (x>dist1) {
   if (s1<s2) {
   if (g1==0){
      t1=millis();
      ++g1;
      }
      p1=(millis()-t1);
      if (p1>200) {
        ++s1;
        g1=0;
      }  
} 

     analogWrite(motor1Pin1, s1);
     analogWrite(motor1Pin2, 0);
     analogWrite(motor2Pin1, s3);
     analogWrite(motor2Pin2, s4);
  }
  else if (x<dist1&&x>dist2){
     analogWrite(motor1Pin1, s1);
     analogWrite(motor1Pin2, 0);
     analogWrite(motor2Pin1, s3);
     analogWrite(motor2Pin2, s4);
  }
  else if (x<dist2){
    if (s1>30){
    if (g2==0){
      t2=millis();
      ++g2;
      }
      p2=(millis()-t2);
      if (p2>200) {
        --s1;
        g2=0;
      }
      
     analogWrite(motor1Pin1, s1);
     analogWrite(motor1Pin2, 0);
     analogWrite(motor2Pin1, s3);
     analogWrite(motor2Pin2, s4);
  }}}
  // TELOS AYKSOMEIWSIS
  
  // APENERGOPOIHSH
  if (onoff==0){
     s1=0;
     analogWrite(motor1Pin1, 0);
     analogWrite(motor1Pin2, 0);
     analogWrite(motor2Pin1, s3);
     analogWrite(motor2Pin2, s4);
  }
}

You really need to force yourself to use #defines then you wont write code that looks like this -

if (ir==16720605) {
    onoff=1;
    s1=85;
    s2=86;
    delay(100);
    ir=0;

why 16720605 what is it ?

Also if you are using the IR Receiver anywhere where sunlight can get at it you need to build an non reflective enclosure, for my lap timer I had to spray the inside of the enclosure with ultra flat camo paint to stop the Dubai sunshine from triggering it.

Fast moving IR in the Dubai sunshine -

Duane B

rcarduino.blogspot.com

There's no sunshine where I tested it...

As for 16720605 it's the received IR value stored in results.value (it shows which button I pressed). The only way I managed to make use of that was to set it in another variable (ir) and use the new variable. I couldn't use results.value in any other way :frowning:

I couldn't use results.value in any other way

Why not? I did that in my remote code.

Of course, you can only use the value in results.value up to the point where you do the irrecv.resume(), which you do right away.

You should learn to develop functions. Your loop() function is WAY too long. Learning to use local variables would be good, too.

So would using the space bar. Putting things in logical order would be useful, too.

  if (x<41&&x>30){

really would look at lot better, and be far easier to read as

  if(x > 30 && x < 41)
  {
if (ir==16720605) {
    onoff=1;
    s1=85;
    s2=86;
    delay(100);
    ir=0;

Why is the delay() there? Putting it after you use s1 and s2 might make sense. Putting it here does not.

I know it's a mess it's just that I started it for fun and then decided to make a school project...if it works I will make it better looking!

I will try to change the way I get the results.value now that you mentioned that I can use it up until irrecv.resume().

The delay is there because I figured when you press a button it takes some milliseconds to let it go again and I only want to use it once, not consecutive times.

if it works I will make it better looking!

You've got that backwards. You need to make it better looking so that it will work and be easier to understand and modify and maintain (and explain to your teachers. Don't expect them to be impressed by that code).

The delay is there because I figured when you press a button it takes some milliseconds to let it go again and I only want to use it once, not consecutive times.

Does your IR send the same code over and over? Most do not. They send the value of the button when first pressed, and then send a repeat code (that is not the same as the button code) when the button is held down.

It only sends it once...

I just noticed that if I press "ON" and then "OFF" after a few seconds it works every time (it stops as commanded). If I let it some seconds more it stops listening..don't know if this tells you anything.

I'm sorry for triple-posting but it seems that the problem is the battery. When the vehicle is connected to my pc it works fine but when the battery comes in it crashes.

I use a single 9V battery, is that not enough? From the arduino 5V pin I power one ultrasonic sensor (hc-sr04), the IR receiver and a couple of leds.

Also, sometimes after a while the L led on the arduino stops blinking and that's when the vehicle goes out of control...

Hi,

I once had a similar problem, I was doing something daft like powering an LED between the IR Detector Vcc and Vout and I had forgotten the resistor or hadnt cut the track under the resistor anyway the thing worked just not very well and not all of the time.

Other people will tell you that using a 9 volt battery is daft and they are generally right, its ok for powering the Arduino, but isnt up to powering whatever additional hardware you have. A common low cost solution is to use AA Batteries for additional components.

Duane B

rcarduino.blogspot.com

I see....I'll try to fit some AA's to power the components and see what happens. Thanks!

Dont forget a common ground between them and the Arduino if you do

Duane B