about my code

Hello everyone,

I am new to programming and i was trying to control the direction of two motors with light. However my code is not working. Could you guys please see what the problem could be?

// motor One
int enA = 9;
int in1 = 8;
int in2 = 7;
// motor Two
int enB = 3;
int in3 = 5;
int in4 = 4;

int sensorPinRight = 2; // Right sensor pin
int sensorPinLeft = 3; // Left sensor pin
int sensorPinForward = 0; // Front sensor pin
int sensorPinBackward = 1; // Back sensor Pin

int lightReadingRight;
int lightToleranceRight = 500; // Sensor reading needed to trigger right turn

int lightReadingLeft;
int lightToleranceLeft = 500; // Sensor reading needed to trigger left turn

int lightReadingForward;
int lightToleranceForward = 500; // Sensor reading needed to trigger front turn

int lightReadingBackward;
int lightToleranceBackward = 500; // Sensor reading needed to trigger back turn

int readingDelay = 100; // Delay between readings

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

//Uncomment serial feed for calibration purposes

Serial.begin(9600);
}

void loop()
{
// Uncomment the serial print lines for calibration purposes

// Take reading from right sensor
lightReadingRight = analogRead(sensorPinRight);
Serial.print("Right Sensor Reading :");
Serial.println(lightReadingRight);

// Take reading from left sensor
lightReadingLeft = analogRead(sensorPinLeft);
Serial.print("Left Sensor Reading :");
Serial.println(lightReadingLeft);

// Take reading from front sensor
lightReadingForward = analogRead(sensorPinForward);
Serial.print("Front Sensor Reading :");
Serial.println(lightReadingForward);

//Take reading from back sensor
lightReadingBackward = analogRead(sensorPinBackward);
Serial.print("Back Sensor Reading :");
Serial.println(lightReadingBackward);

// Move forward if enought light is detected from front sensor
if (lightReadingForward > lightToleranceForward)
{
// motor A
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
analogWrite(enA,200);

// motor B

digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
analogWrite(enB,200);
delay(readingDelay);

// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

}

// Turn right if enough light is detected from right sensor
if (lightReadingRight > lightToleranceRight)
{
digitalWrite(in1,LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(readingDelay);
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}

// Turn left if enough light is detected from left sensor
if (lightReadingLeft > lightToleranceLeft)
{
digitalWrite(in1,HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3,HIGH);
digitalWrite(in4, LOW);
delay(readingDelay);
// Stop
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}

//Turn back if enough light is detected from the backward direction

digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
delay(readingDelay);
// Stop
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

}

Your code is working; possibly not as intended but that's something else. Please explain what you expect it to do and what it is actually doing.

Your are aware that the last part of loop (after // turn back ...) is always executed?

And please edit your post and add code tags around the code
Type
** **[code]** **
before the code
Type
** **[/code]** **
after the code