Bluetooth receiver interfering with IR receiver

Hello. I’v built a toy car that has both an IR receiver (VS1838) and a bluetooth one (HC-05).
I want the car to be able operate with either form of control, the IR or the bluetooth.
So for example, if i use the bluetooth control, then at the same time, i want the IR receiver to not interfere.

Right now , with the code i’v written, the IR control doesn’t work as long as the bluetooth module’s TX pin is physically connected to the arduino’s RX pin. This is due to the stop() function in the bluetooth part of the code (if (Serial.available() > 0){
stops();).

I’m looking for way to keep that stop() function, without it interfering with the operation of the IR part of the code. Any suggestions?

I’m using an IR remote to operate the IR receiver, and a smartphone to operate the bluetooth receiver.

The part of the code with which i need help starts in the loop, all the way to the end of the bluetooth part in the loop. The code does other things as well, feel free to ignore them.

Also, i’m using arduino uno. Thank you in advance.

The full code:

#include <dht.h>
#include <IRremote.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

//temperature sensor’s variables
dht DHT;
#define DHT11_PIN 12
unsigned long previousMillis_temp = 0; // will store last time temperature was updated
long interval_temp = 5000; // interval at which to update temperature

//servo’s variables
Servo myservo;
int increment = 20;
int pos = 90;
unsigned long previousMillis_servo = 0;
int interval_servo = 25;

// ultrasonic variables
const int trigPin = 10;
const int echoPin = 9;
long duration;
int distance;
unsigned long previousMillis_distance = 0;
int interval_distance = 1000;

unsigned long currentMillis = 0;

//IR receiver’s variables
const int RECV_PIN = 8;
IRrecv irrecv(RECV_PIN);
decode_results results;

// Set the pins on the I2C chip used for LCD connections:
// address (of the I2C chip), en,rw,rs,d4,d5,d6,d7
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7);

// forward and rear right motors
const int EN_A = 6;
const int IN_1 = 7;
const int IN_2 = 3;

// forward and rear left motors
const int IN_3 = 4;
const int IN_4 = 2;
const int EN_B = 5;

int pwm_speed = 200;

void setup() {
Serial.begin(9600);
myservo.attach(11); // attaches the servo on pin 11 to the servo object.
lcd.begin(16,2); // Initializes the interface to the LCD screen,
// and specifies the dimensions (width and height) of the display.
lcd.setBacklightPin(3,POSITIVE); // makes the P3 pin control the backlight of the LCD.
lcd.setBacklight(HIGH); // makes the P3 pin go High, which turns on the NPN transistor.
// This provides GND to the LED pin of the LCD as the other LED pin is already connected to Vcc
// through the jumper, and this allows the LCD backlight to glow.

irrecv.enableIRIn(); // Infrared
irrecv.blink13(true);
pinMode(EN_A, OUTPUT);
pinMode(EN_B, OUTPUT);
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(IN_3, OUTPUT);
pinMode(IN_4, OUTPUT);
pinMode(trigPin, OUTPUT); // Ultrasonic
pinMode(echoPin, INPUT);

}

void loop() {

analogWrite(EN_A, pwm_speed);
analogWrite(EN_B, pwm_speed);

if(distance <= 20 && pwm_speed >= 160) {
stops();
pwm_speed = 120;
}

else if (irrecv.decode(&results)){

switch(results.value) {
case 0xFF18E7: // forward - keypad #2
forward();
break;

case 0xFF4AB5: // backward - keypad #8
backward();
break;

case 0xFF10EF: // Left - keypad #4
left();
break;

case 0xFF5AA5: // Right - keypad #6
right();
break;

case 0xFF38C7: // Stop - keypad #5
stops();
break;

case 0xFFA857: // increase speed - keypad (+)
if(pwm_speed < 240)
pwm_speed = pwm_speed + 40;
break;

case 0xFFE01F: // decrease speed - keypad (-)
if(pwm_speed > 160)
pwm_speed = pwm_speed - 40;
break;
}
irrecv.resume();
}

if (Serial.available() > 0){
stops();

switch(Serial.read()) {
case ‘F’: // forward - keypad #2
forward();
break;

case ‘B’: // backward - keypad #8
backward();
break;

case ‘L’: // Left - keypad #4
left();
break;

case ‘R’: // Right - keypad #6
right();
break;
}
}

// to make sure that the trigpin is clear of any signal we set it to LOW for 2(us).
digitalWrite(trigPin,LOW);
delayMicroseconds(2);

digitalWrite(trigPin,HIGH); // telling the Trig pin of the ultrasonic sensor to send a signal.
delayMicroseconds(10);
digitalWrite(trigPin,LOW); // telling the Trig pin of the ultrasonic to stop sending the signal.

duration = pulseIn(echoPin,HIGH);
distance = duration*0.0343/2;

currentMillis = millis();
ultrasonicDistance();
servoSweep();
temperature();
}

//functions for controling the movement of the car

void forward() {
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
}

void backward() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
}

void left() {
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
}

void right() {
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
}

void stops() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, LOW);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, LOW);
}

void servoSweep() {
if(currentMillis - previousMillis_servo >= interval_servo){
previousMillis_servo = currentMillis;
pos += increment;
myservo.write(pos);
if((pos >= 100) || (pos <= 30))
increment = -increment;
}
}

void temperature() {
if(currentMillis - previousMillis_temp >= interval_temp) {
previousMillis_temp = currentMillis;
int chk = DHT.read11(DHT11_PIN);
lcd.setCursor(0,1);
lcd.print("Temp: ");
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print(“C”);
}
}

void ultrasonicDistance() {
if(currentMillis - previousMillis_distance >= interval_distance) {
previousMillis_distance = currentMillis;

lcd.setCursor(0,0); // lcd.setCursor(col, row)
// Sets the location at which subsequent text written to the LCD will be displayed.
lcd.print(“Distance: “);
lcd.print(distance);
lcd.print(” Cm”);
}
}

This is due to the stop() function in the bluetooth part of the code (if (Serial.available() > 0){
stops();).

That is not right.

What is likely happening is that you are sending something like “F”, instead of “F”. So, the ‘F’ arriving causes stops() to be called, and then forward() is called. The ‘’ then causes stops() to be called, but no other function. The ‘’ does the same.

Move the stops() call into the cases for ‘F’, ‘B’, ‘L’, and ‘R’. Then, the and won’t make the robot stop.