I am trying to create a line following car with L293D, 4 Motors, 11-12V of external power supply and Arduino UNO. However while everything is working fine it is only working when USB is connected to my UNO. Even IR sensors are turning on ONLY WHEN USB is attached. However at the same time when USB is attached to Arduino motors are not running unless I switch on the power supply (3 cells each approx 3.7-4V).
My conclusion is that it’s because cells are not providing enough power to board and that is why it is not turning on. But I am no expert I’m just doing it for University project my domain lies on software side, so this all is very confusing to me.
#include <AFMotor.h>
//defining pins and variables
#define lefts A0
#define rights A1
//defining motors
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
void setup() {
//Setting the motor speed
motor1.setSpeed(100);
motor2.setSpeed(100);
motor3.setSpeed(100);
motor4.setSpeed(100);
//Declaring PIN input types
pinMode(lefts,INPUT);
pinMode(rights,INPUT);
//Begin serial communication
Serial.begin(9600);
}
void loop(){
//Printing values of the sensors to the serial monitor
Serial.println(analogRead(lefts));
Serial.println(analogRead(rights));
//line detected by both
if(analogRead(lefts)<=350 && analogRead(rights)<=350){
//Forward
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
//line detected by left sensor
else if(analogRead(lefts)<=350 && !analogRead(rights)<=350){
//turn left
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
//line detected by right sensor
else if(!analogRead(lefts)<=350 && analogRead(rights)<=350){
//turn right
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
//line detected by none
else if(!analogRead(lefts)<=350 && !analogRead(rights)<=350){
//stop
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
}