Line follower and Obstacle avoider robot arduino resetting

Hello,
I have made a line follower robot that will stop at obstacles. However, when both my IRs are on, the arduino non-stop resets. If only one IR sensor is on though, it works. I am using a motor shield without power supply but takes power from the vin pin. Also, I am using 6 1.5 volt batteries. A HC SR04 module too.

28 posts and you don't realise how important it is to post code and schematics when asking questions.

Here is my code. I just connected the motors to the motor driver. Also, I connected all my components to the motor driver itself, as it had enough pins.

#include <Ultrasonic.h>

Ultrasonic ultrasonic(9, 11);

const int righto = 8;
const int lefto = 7;
const int motor_rightFront = 2;
const int motor_rightBack = 3;
const int motor_leftFront = 4;
const int motor_leftBack = 5;
int x;
int y;

int readingUltimate2; //This is what I named the distance the ultrasonic sensor reads.
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
for(x = 2; x<6; x++){
  pinMode(x, OUTPUT);
}
for(y=7; y<9 ; y++){
  pinMode(y, INPUT);
}
servo.attach(10);
}

void loop() {
  // put your main code here, to run repeatedly:

  readingUltimate2 = ultrasonic.read(CM);
  Serial.println(readingUltimate2);
  if(readingUltimate2>10){
int right_sensor = digitalRead(righto);
int left_sensor = digitalRead(lefto);
if(right_sensor == HIGH && left_sensor == LOW){
 
  left();
 
  
}
if(right_sensor == LOW && left_sensor == HIGH){
 
  right();
 
  
}
if(right_sensor == LOW && left_sensor == LOW){
  yay();
}
if(right_sensor == HIGH && left_sensor == HIGH){
  forward();
  
}
}
else{
  yay();
}
}

void right(){
  digitalWrite(motor_rightFront, LOW);
  digitalWrite(motor_rightBack, LOW);
  digitalWrite(motor_leftFront, LOW);
  digitalWrite(motor_leftBack, HIGH);
}
void left(){
  digitalWrite(motor_rightFront, LOW);
  digitalWrite(motor_rightBack, HIGH);
  digitalWrite(motor_leftFront, LOW);
  digitalWrite(motor_leftBack, LOW);
}
void forward(){
   digitalWrite(motor_rightFront, LOW);
  digitalWrite(motor_rightBack, HIGH);
  digitalWrite(motor_leftFront, LOW);
  digitalWrite(motor_leftBack, HIGH);
}
void yay(){              //This is to make it stop.
   digitalWrite(motor_rightFront, LOW);
  digitalWrite(motor_rightBack, LOW);
  digitalWrite(motor_leftFront, LOW);
  digitalWrite(motor_leftBack, LOW);
}

Sorry for forgetting about the code.

This sounds like a power supply issue, also please post your schematics

Your code does not compile. Please post some real code that you have compiled and run.

Steve

#include <Ultrasonic.h>

Ultrasonic ultrasonic(12, 11);

const int righto = 8;
const int lefto = 7;
const int motor_rightFront = 2;
const int motor_rightBack = 3;
const int motor_leftFront = 4;
const int motor_leftBack = 5;
int x;
int y;

int readingUltimate2; //This is what I named the distance the ultrasonic sensor reads.
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
for(x = 2; x<6; x++){
  pinMode(x, OUTPUT);
}
for(y=7; y<9 ; y++){
  pinMode(y, INPUT);
}

}

void loop() {
  // put your main code here, to run repeatedly:

  readingUltimate2 = ultrasonic.read(CM);
  Serial.println(readingUltimate2);
  if(readingUltimate2>10){
int right_sensor = digitalRead(righto);
int left_sensor = digitalRead(lefto);
if(right_sensor == HIGH && left_sensor == LOW){
 
  left();
 
  
}
if(right_sensor == LOW && left_sensor == HIGH){
 
  right();
 
  
}
if(right_sensor == LOW && left_sensor == LOW){
  yay();
}
if(right_sensor == HIGH && left_sensor == HIGH){
  forward();
  
}
}
else{
  yay();
}
}

void right(){
  digitalWrite(motor_rightFront, LOW);
  digitalWrite(motor_rightBack, LOW);
  digitalWrite(motor_leftFront, LOW);
  digitalWrite(motor_leftBack, HIGH);
}
void left(){
  digitalWrite(motor_rightFront, LOW);
  digitalWrite(motor_rightBack, HIGH);
  digitalWrite(motor_leftFront, LOW);
  digitalWrite(motor_leftBack, LOW);
}
void forward(){
   digitalWrite(motor_rightFront, LOW);
  digitalWrite(motor_rightBack, HIGH);
  digitalWrite(motor_leftFront, LOW);
  digitalWrite(motor_leftBack, HIGH);
}
void yay(){              //This is to make it stop.
   digitalWrite(motor_rightFront, LOW);
  digitalWrite(motor_rightBack, LOW);
  digitalWrite(motor_leftFront, LOW);
  digitalWrite(motor_leftBack, LOW);
}

I am using 6 1.5 volt batteries for power supply. Also, I connected all components to the motor shield.

What Arduino? What motor shield? What IR sensors? How EXACTLY are the 6 x 1.5V batteries connected?

All the motor shields I know expect separate power input for the motors. Taking an OUTPUT from from the Arduino Vin is a bad idea. The in that pins name means you put power IN there not take it OUT)

Steve

However, if I power it with laptop through USB printer cable, it works perfectly

I can't see how you are powering it.
Please give us a wiring diagram.
Pen, paper and a camera are great.

Also, while you have the camera out, take a few photos of your project and post them here. It will help us help you.

hello,
Here is a photo of my robot. (It is quite basic as this is my first try)

If you don't answer the questions you are asked and you won't provide the information requested you're going to have real trouble getting any help here.

Steve

I am using an arduino uno board. I have connected all six batteries in a case, and connected the case to a DC Jack on the arduino board.
Also, I am using the motor shield from here, along with all of the other materials:

I am also using the IR sensors from there.

I am not so sure exactly how to give schematics.

Is it something wrong with my code?

MukeshArvindh:
Is it something wrong with my code?

Does it do what you want and expect?

If not, then yes, there probably is something wrong with your code.

i mean, it does if I plug it into the computer via USB printer cable, but does not if I plug it to the 6 AA batteries

Gents - working off his sig, Mukesh is a 14y.o. in yr11, so clearly capable..

Go gentle... ... ...

Mukesh - Do you have a way to check if there is 9V coming out of the battery pack? For things like this, I power the Arduino off it too through the Vin pin - that way, you know when you have a power supply issue.

I can drive one servo motor easily off an arduino, but four servos need a battery pack for the extra current needed. Sounds as though this might be the issue as others are hinting at, but stranger things have happened.

A

I do not really have a voltmeter, so i can't measure.
However, in general, can the arduino supply enough current for a HC SR04, 2 IR sensors, and 2 BO motors?

Hi Mukesh:

Maximum supply current of an arduino is (from memory) 40mA per pin and 200mA in total.

Individual currents.

HC-SR04 working current is 15 mA.
Source -> https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf

Those yellow motors - 170-200mA each = 350 mA, probably 1A on stall/startup
Source -> https://create.arduino.cc/projecthub/electropeak/the-beginner-s-guide-to-control-motors-by-arduino-and-l293d-139307

See the GIF in the middle of this page.^^

IR line followers - 20 mA each = 40mA would be a tiny current - I can't find it quickly, will edit when I find it.
Source -> IR Sensor Module Pinout, Features & Datasheet

found this when I was looking for the current draw of the line followers.

^^^ Here's a working line following robot and schematics to get you going. The code is not arduino compliant and will need some massaging.

A