IR controlled car. (Using forums for first time)

Hey guys, this is my first time using Arduino forums so if I'm doing some mistakes or posting in a wrong section please forgive me. Anyways, I want some help regarding my project. I'm trying to build a car using IR sensor which has a 16*2 LCD display. To test the project I used LEDs since I was not able find geared 5V DC motors, it was working as I wished it to. Later when I found my motors I hooked them up as I hoked up my LEDs previously but once I use my remote to use my car it doesn't work, it is almost like it freezes.

Here is the code:

// Setting up LCD Display Here.

#include<LiquidCrystal.h>

int RS = 12;
int E = 11;
int D4 = 5;
int D5 = 4;
int D6 = 3;
int D7 = 2;

LiquidCrystal lcd(RS,E,D4,D5,D6,D7);

// Setting up IR reciver sensor here.

#include<IRremote.h>

int IR_Reciver_Pin = 9;
IRrecv irrecv(IR_Reciver_Pin);
decode_results results;

// Setting up DC motor pins. 

int MotorOne = 6;
int MotorTwo = 7;

// Setting up other variables required.

String S1 = "Welcome!";
String S2 = "Rollin forward ";
String S3 = "captain!";
String S4 = "Turning towards ";
String S5 = "right.";
String S6 = "left.";
String S7 = "Halting Captain!";

void setup() {

  // Initiating LCD display here.

  lcd.begin(16,2);
  lcd.print(S1);

  // Initiating IR reciver sensor here.

  irrecv.enableIRIn();

  // Initiating DC motors.

  pinMode(MotorOne,OUTPUT);
  pinMode(MotorTwo,OUTPUT);

  //initiating Serial Monitor.

  Serial.begin(9600);

}

void loop() {

  // Reading IR remote value.
  
  if(irrecv.decode(&results)) 
    {
     int value = results.value;
     Serial.print("CODE: ");
     Serial.println(results.value);
     irrecv.resume();              
    }

  // Code for going forward.   

  if(results.value==3772778233)
    {
      lcd.clear();
      lcd.print(S2);
      lcd.setCursor(0,2);
      lcd.print(S3);
      digitalWrite(MotorOne,HIGH);
      digitalWrite(MotorTwo,HIGH);
    }

  // Code for turning right.

  if(results.value==3772794553)
     {
      lcd.clear();
      lcd.print(S4);
      lcd.setCursor(0,2);
      lcd.print(S5);
      digitalWrite(MotorOne,HIGH);
      digitalWrite(MotorTwo,LOW);
     }

  // Code for turning left.  

  if(results.value==3772819033)
     {
      lcd.clear();
      lcd.print(S4);
      lcd.setCursor(0,2);
      lcd.print(S6);
      digitalWrite(MotorOne,LOW);
      digitalWrite(MotorTwo,HIGH);  
     }

  // Code for stoping.

  if(results.value==3772782313)
     {
      lcd.clear();
      lcd.print(S7);
      digitalWrite(MotorOne,LOW);
      digitalWrite(MotorTwo,LOW);   
     }
}

Please help me if you are seeing this, :slightly_smiling_face:

Welcome to the forum and thank you for using code tags when you posted your code

Your post was MOVED to its current location as it is more suitable

How is the car powered and how are the motors connected to the Arduino ?

Are you using a PP3 9V battery by any chance ?

You've managed to make your code less readable, and waste more RAM by doing this.

Bob beat me to the power supply question.

Thank you so much!

Currently I have to buy a battery so I'm powering my project through the USB cable used to upload the code to Arduino. Also, I'm attaching the schematics of my project. If something has to be changed please do tell.
Untitled

I'm not sure if I understand this, will you elaborate so I can understand what I have to fix in my project.

What is driving the motors?
(Correct answers do not include the words "the I/O pins")

Every String consumes at least six bytes of RAM, even if empty.

The label "S4" conveys zero information about what is being printed

So, you're wasting precious RAM, and making your code harder to read.

If you are asking am I using a L293D driving shield or not, that I'm not. Also, in the message after you asked me "What is driving the motors?", do you wanna say that instead of using String variable I should directly write the sentences in the code?

Are you saying the motors are connected directly to the I/O pins?

Absolutely.
Preferably using the F() macro, so the string literals remain in flash, where they belong.

Yes, the motors are connected directly to I/O pins, since, I don't have L293D driving shield.

I'll try this out immediately.

Fix that first (or preferably something less antique), and let's hope you haven't damaged the Uno.

I have previously built an obstacle avoiding car using the same geared 5V DC motor and without using L293D driving shield, that was like a year ago, and I have build several projects after that, the board is working fine till date. Will me not having or using L293D damage my Arduino UNO? I have accommodated your advice regarding the String variables, there seems to be no change.

Here is the new code:

// Setting up LCD Display Here.

#include<LiquidCrystal.h>

int RS = 12;
int E = 11;
int D4 = 5;
int D5 = 4;
int D6 = 3;
int D7 = 2;

LiquidCrystal lcd(RS,E,D4,D5,D6,D7);

// Setting up IR reciver sensor here.

#include<IRremote.h>

int IR_Reciver_Pin = 9;
IRrecv irrecv(IR_Reciver_Pin);
decode_results results;

// Setting up DC motor pins. 

int MotorOne = 6;
int MotorTwo = 7;

void setup() {

  // Initiating LCD display here.

  lcd.begin(16,2);
  lcd.print("Welcome!");

  // Initiating IR reciver sensor here.

  irrecv.enableIRIn();

  // Initiating DC motors.

  pinMode(MotorOne,OUTPUT);
  pinMode(MotorTwo,OUTPUT);
  
}

void loop() {

  // Reading IR remote value.
  
  if(irrecv.decode(&results)) 
    {
     int value = results.value;
     irrecv.resume();              
    }

  // Code for going forward.   

  if(results.value==3772778233)
    {
      lcd.clear();
      lcd.print("Rolling forward");
      lcd.setCursor(0,2);
      lcd.print("captain!");
      digitalWrite(MotorOne,HIGH);
      digitalWrite(MotorTwo,HIGH);
    }

  // Code for turning right.

  if(results.value==3772794553)
     {
      lcd.clear();
      lcd.print("Turning towards");
      lcd.setCursor(0,2);
      lcd.print("right.");
      digitalWrite(MotorOne,HIGH);
      digitalWrite(MotorTwo,LOW);
     }

  // Code for turning left.  

  if(results.value==3772819033)
     {
      lcd.clear();
      lcd.print("Turning towards");
      lcd.setCursor(0,2);
      lcd.print("left.");
      digitalWrite(MotorOne,LOW);
      digitalWrite(MotorTwo,HIGH);  
     }

  // Code for stoping.

  if(results.value==3772782313)
     {
      lcd.clear();
      lcd.print("Halting captain!");
      digitalWrite(MotorOne,LOW);
      digitalWrite(MotorTwo,LOW);   
     }
     
}

It's easier to see what is going-on.
But you forgot the F() macros.

Don't connect motors to I/O pins; they're not designed for it.

I have never used this before, can you tell me how and in which part of my code (before void setup or in void setup or loop) shall I use it.

Since I haven't got the L293D driving shield, I guess I'll have to dump this project. :smiling_face_with_tear:

lcd.print(F("Rolling forward"));

Like that.

I would, before you damage any more of your hardware.

Anecdote: A few years ago, I was working for a mobile phone manufacurer.
I had on of those teeny, tiny ERM motors that makes your phone vibrate. It was maybe 5mm or so square, and 15 - 20 mm long, and in an idle moment, I hooked it to a bench supply at its rated voltage.
It drew two and a half times the maximum current for an AVR pin.

Yep, I guess it's better than not having my board for any more projects that I want to do in future.

Woah, I guess I should be more careful before using something with my Arduino.

Thanks for helping me out! Have a nice day ahead!

Need your help friend...

I somehow got access to L293D Driver IC and hooked it up with the motors according to the schematics given below. Also, I connected the 16*2 LCD and VS1388B IR receiver sensor in a new way then the previous diagram I had shared.

Here is the new code:

// Setting up LCD Display Here.

#include<LiquidCrystal.h>

int RS = 13;
int E = 12;
int D4 = 11;
int D5 = 10;
int D6 = 6;
int D7 = 2;

LiquidCrystal lcd(RS,E,D4,D5,D6,D7);

// Setting up IR reciver sensor here.

#include<IRremote.h>

int IR_Reciver_Pin = A5;
IRrecv irrecv(IR_Reciver_Pin);
decode_results results;

// Setting up DC motor pins. 

/* Motor A connections */

int enA = 9;
int in1 = 8;
int in2 = 7;

/* Motor B connections */

int enB = 3;
int in3 = 5;
int in4 = 4;

void setup() {

  // Initiating LCD display here.

  lcd.begin(16,2);
  lcd.print("Welcome!");

  // Initiating IR reciver sensor here.

  irrecv.enableIRIn();

  // Initiating DC motors.

  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Turning motors off - Initial state
  
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  
}

void loop() {

  // Reading IR remote value.
  
  if(irrecv.decode(&results)) 
    {
     int value = results.value;
     irrecv.resume();              
    }

  // Code for..... well, something.

  analogWrite(enA, 255);
  analogWrite(enB, 255);

  // Code for going forward.   

  if(results.value==3772778233)
    {
      lcd.clear();
      lcd.print("Rolling forward");
      lcd.setCursor(0,2);
      lcd.print("captain!");
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
      digitalWrite(in3, HIGH);
      digitalWrite(in4, LOW);
    }

  // Code for turning right.

  if(results.value==3772794553)
     {
      lcd.clear();
      lcd.print("Turning towards");
      lcd.setCursor(0,2);
      lcd.print("right.");
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
      digitalWrite(in3, LOW);
      digitalWrite(in4, LOW);
     }

  // Code for turning left.  

  if(results.value==3772819033)
     {
      lcd.clear();
      lcd.print("Turning towards");
      lcd.setCursor(0,2);
      lcd.print("left.");
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
      digitalWrite(in3, HIGH);
      digitalWrite(in4, LOW); 
     }

  // Code for stoping.

  if(results.value==3772782313)
     {
      lcd.clear();
      lcd.print("Halting captain!");
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
      digitalWrite(in3, LOW);
      digitalWrite(in4, LOW); 
     }
     
}

What am I doing wrong? Everything seems about right as it can be. Please help and sorry if I'm troubling yaa.