Complications when adding IR Reciever statements into code

Im very new at coding and working on a project for a controls dynamics lab. Im trying to implement an IR receiver into a simple obstacle avoiding little car. It was all working fine until i tried to use IR signal to stop and start the car. I thought just making two cases would work. One being a stopping case and one being the normal running case. Right now, with this code i am able to start the wheels by double tapping any remote button and stop by holding any button, but for some reason when its running the code isnt processing the IF statement which stops the car if its about to hit something. The serial monitor shows the arduino is still measuring the distances but its just a constant velocity. The car never breaks. Its not a hardware or wiring problem. Im sure its just an arduino syntax issue im not familiar with. Any ideas?

//* Obstacle Avoiding Robot Using Ultrasonic Sensor and Arduino Uno

#include "IRremote.h"

// Motor A connections
int enA = 9;
int in1 = 8; //Foward(high)
int in2 = 7; //Backward(high)
// Motor B connections
int enB = 3;
int in3 = 5; //Foward (high)
int in4 = 4; //Backward (high)
// Ultrasonic Sensors
int trigPin = 10; // trig pin of HC-SR04
int echoPin = 11; // Echo pin of HC-SR04
//led lights
int backuplight = 12;
//IR Reciever
int receiver = 6;
IRrecv irrecv(receiver);
decode_results results;

long duration, distance;
void setup()
{
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(trigPin, OUTPUT); // set trig pin as output
pinMode(echoPin, INPUT); //set echo pin as input to capture reflected waves
pinMode(backuplight,OUTPUT);
irrecv.enableIRIn(); //start reciever
Serial.begin(9600);
// Turn off motors - Initial state
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
digitalWrite(backuplight, LOW);
}

void loop() {
//get the distance reading from the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // send waves for 10 us
delayMicroseconds(10);
duration = pulseIn(echoPin, HIGH); // receive reflected waves
distance = duration / 58.2; // convert to distance
delay(10);
Serial.print(distance);
Serial.print("\t");
Serial.println(results.value,HEX);
analogWrite(enA,255); //set motor 1 voltage(speed)
analogWrite(enB,255); //set motor 2 voltage(speed)

if (irrecv.decode(&results)) //is there a signal?
{
switch(results.value)

{
case 0xFFFFFFFF: //VOL+ pressed
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
digitalWrite(backuplight, LOW);
irrecv.resume();
break;

default:
if (distance > 25)
{
digitalWrite(in1, LOW); // move forward
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
digitalWrite(backuplight,LOW);
irrecv.resume();
}

else
{
digitalWrite(in1, LOW); //Stop
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
digitalWrite(backuplight,HIGH);
delay(500);

digitalWrite(in1, HIGH); //movebackword
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(500);

digitalWrite(in1, LOW); //Stop
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(100);

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(500);
irrecv.resume();
}
break;
irrecv.resume(); //recieve next value
}
}
}

Welcome to the forums. Please read the sticky note at the top of the forum about how to post your code with code tags. It’s helps people help you.

You should really put some Serial.print() staments in your code to see what is going on

Your distance is only acted on when an unused IR code comes in (it's in the 'default' case of the switch statement). You have to move the distance check out of the IR code so it gets checked every time through loop().

chrislee547:
Im very new at coding and working on a project for a controls dynamics lab. Im trying to implement an IR receiver into a simple obstacle avoiding little car. It was all working fine until i tried to use IR signal to stop and start the car. I thought just making two cases would work. One being a stopping case and one being the normal running case. Right now, with this code i am able to start the wheels by double tapping any remote button and stop by holding any button, but for some reason when its running the code isnt processing the IF statement which stops the car if its about to hit something. The serial monitor shows the arduino is still measuring the distances but its just a constant velocity. The car never breaks. Its not a hardware or wiring problem. Im sure its just an arduino syntax issue im not familiar with. Any ideas?

//* Obstacle Avoiding Robot Using Ultrasonic Sensor and Arduino Uno


#include "IRremote.h"


// Motor A connections
int enA = 9;
int in1 = 8;    //Foward(high)
int in2 = 7;    //Backward(high)
// Motor B connections
int enB = 3;
int in3 = 5;    //Foward (high)
int in4 = 4;    //Backward (high)
// Ultrasonic Sensors
int trigPin = 10;      // trig pin of HC-SR04
int echoPin = 11;     // Echo pin of HC-SR04
//led lights
int backuplight = 12;
//IR Reciever
int receiver = 6;
IRrecv irrecv(receiver);
decode_results results;


long duration, distance;


void setup()
{
  Serial.begin(9600);


  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  analogWrite(enA, 255);                 //set motor 1 voltage(speed)
  analogWrite(enB, 255);                 //set motor 2 voltage(speed)


  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  // Turn off motors - Initial state
  Stop();


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


  pinMode(trigPin, OUTPUT);         // set trig pin as output
  pinMode(echoPin, INPUT);          //set echo pin as input to capture reflected waves


  irrecv.enableIRIn();              //start reciever
}


void Stop()
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);


}


void Forward()
{


  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}


void Reverse()
{


  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}


void Turn()
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}


void loop()
{
  //get the distance reading from the sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);           // send waves for 10 us
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH);     // receive reflected waves
  distance = duration / 58.2;            // convert to distance
  delay(10);
  Serial.print(distance);
  Serial.print("\t");


  if (distance < 25)
  {
    Stop();


    // Back up a bit
    digitalWrite(backuplight, HIGH);
    delay(500);
    Reverse();
    delay(500);
    digitalWrite(backuplight, LOW);


    Stop();
    delay(100);


    // Turn a bit
    Turn();
    delay(500);


    Forward();
  }




  if (irrecv.decode(&results))      //is there a signal?
  {
    Serial.println(results.value, HEX);
    if (results.value == 0xFFFFFFFF) // Repeat code means STOP
    {
      Stop();
      digitalWrite(backuplight, LOW);
    }
    else   // Any other code means GO
    {
      Forward();
    }
    
    irrecv.resume();        //recieve next value
  }
}