Hi I am new to arduino wonder if somebody could help me with my code for my light following robot, I am having issues with my robot it just isn't working and I was wondering if I had done something wrong in the code? The robot is using two motors and an L293D motor chip. Thanks for any responses.
int motor1Enable1 = 0; // pin 1 on L293D
int motor1Input1 = 1; // pin 2 on L293D
int motor1Input2 = 2; // pin 7 on L293D
int motor2Enable2 = 3; // pin 9 on L293D
int motor2Input3 = 4; // pin 10 on L293D
int motor2Input4 = 5; // pin 15 on L293D
int rightLightSensor = A0;
int leftLightSensor = A1;
int pushButton = 6;
// Variables to store ADC Value:
int sensorValueRight = 0;
int sensorValueLeft = 0;
void setup ()
{
// Set sensors and button as inputs.
pinMode(rightLightSensor, INPUT);
pinMode(leftLightSensor, INPUT);
pinMode(pushButton, INPUT);
// Set all motor pins as outputs.
pinMode(motor1Enable1, OUTPUT);
pinMode(motor1Input1, OUTPUT);
pinMode(motor1Input2, OUTPUT);
pinMode(motor2Enable2, OUTPUT);
pinMode(motor2Input3, OUTPUT);
pinMode(motor2Input4, OUTPUT);
// Turn motors off.
digitalWrite(motor1Enable1, HIGH);
digitalWrite(motor1Input1, LOW);
digitalWrite(motor1Input2, LOW);
digitalWrite(motor2Enable2, HIGH);
digitalWrite(motor2Input3, LOW);
digitalWrite(motor2Input4, LOW);
}
void loop ()
{
// read the value from the right sensor:
sensorValueRight = analogRead(rightLightSensor);
// read the value from the left sensor:
sensorValueLeft = analogRead(leftLightSensor);
// Compares sensor values if one is great different sub routine occurs
if (sensorValueRight >= sensorValueLeft)
{
turningRight();
}
else
{
turningLeft();
}
}
void turningRight ()
{
// The integer "difference" stores the value obtained by the subtraction of analog read Right and Left.
int difference = sensorValueRight - sensorValueLeft;
if (difference > 50)
{
turnRight();
}
if (50 > difference > 25)
{
driveForward();
}
if (difference < 25)
{
stopMoving();
}
}
void turningLeft ()
{
// The integer "difference" stores the value obtained by the subtraction of analog read Left and Right.
int difference = sensorValueLeft - sensorValueRight;
if (difference > 50)
{
turnLeft();
}
if (50 > difference > 25)
{
driveForward();
}
if (difference < 25)
{
stopMoving();
}
}
void turnRight ()
{
digitalWrite (motor1Enable1, HIGH);
digitalWrite (motor1Input1, LOW);
digitalWrite (motor1Input2, HIGH);
digitalWrite (motor2Enable2, HIGH);
digitalWrite (motor2Input3, LOW);
digitalWrite (motor2Input4, LOW);
sensorValueRight = analogRead(rightLightSensor);
sensorValueLeft = analogRead(leftLightSensor);
int difference = sensorValueRight - sensorValueLeft;
if (50 > difference > 25)
{
driveForward();
}
if (difference < 25)
{
stopMoving();
}
}
void turnLeft ()
{
digitalWrite (motor1Enable1, HIGH);
digitalWrite (motor1Input1, LOW);
digitalWrite (motor1Input2,LOW);
digitalWrite (motor2Enable2, HIGH);
digitalWrite (motor2Input3, LOW);
digitalWrite (motor2Input4, HIGH);
sensorValueRight = analogRead(rightLightSensor);
sensorValueLeft = analogRead(leftLightSensor);
int difference = sensorValueLeft - sensorValueRight;
if (50 > difference > 25)
{
driveForward();
}
if (difference < 25)
{
stopMoving();
}
}
void driveForward ()
{
digitalWrite (motor1Enable1, HIGH);
digitalWrite (motor1Input1, LOW);
digitalWrite (motor1Input2,HIGH);
digitalWrite (motor2Enable2, HIGH);
digitalWrite (motor2Input3, LOW);
digitalWrite (motor2Input4, HIGH);
sensorValueRight = analogRead(rightLightSensor);
sensorValueLeft = analogRead(leftLightSensor);
int difference = sensorValueLeft - sensorValueRight;
if (sensorValueRight >= sensorValueLeft)
{
turningRight();
}
else
{
turningLeft();
}
}
void stopMoving ()
{
digitalWrite (motor1Enable1, HIGH);
digitalWrite (motor1Input1, LOW);
digitalWrite (motor1Input2,LOW);
digitalWrite (motor2Enable2, HIGH);
digitalWrite (motor2Input3, LOW);
digitalWrite (motor2Input4, LOW);
sensorValueRight = analogRead(rightLightSensor);
sensorValueLeft = analogRead(leftLightSensor);
if (sensorValueRight >= sensorValueLeft)
{
turningRight();
}
else
{
turningLeft();
}
}