Hi all, I'm struggling to use the output of my IR sensors(2 sensors) to control 2 motors.
I am using 2 Sharp IR GP2Y0D340K Distance sensor , a Ardumoto to power the motors and a Arduino Uno. When I connect power to the Arduino the motors turn as expected , but only keep turning in one direction. I want the motors to change direction when the IR sensor detects an object, but they do not respond to the sensors. In my code I have assigned the sensors readings( 1 or 0 ) to be sent to the serial monitor just so that I can see if they are working, and they are. Can someone please take a look at my code and why the motors do not respond. Thanks in advance.
int pwm_a = 3; //PWM control for motor outputs 1 and 2 is on digital pin 3
int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_a = 12; //dir control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13; //dir control for motor outputs 3 and 4 is on digital pin 13
void setup()
{
pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
Serial.begin(9600);
analogWrite(pwm_b, 220);
analogWrite(pwm_a, 220);
}
void loop()
{
int sensorValue = digitalRead(A0);
int sensorValue1 = digitalRead(A1);
Serial.println(sensorValue1, DEC);//Just to see if the sensors are working, and they are
if (sensorValue = 0) { //Sensor 1 is asigned to only control the left-side motor
void LFORWARD();
}
else{
void LREVERSE();
}
if(sensorValue1 = 0){ //Sensor 2 is asigned to only control the right-side motor
void RFORWARD();
}
else{
void RREVERSE();
}
void LFORWARD();{
digitalWrite(dir_a,HIGH);//Control for the left-side motor
}
void RFORWARD();{
digitalWrite(dir_b,HIGH);//Control for the right-side motor
}
void LREVERSE();{
digitalWrite(dir_a,LOW);//Control for the left-side motor
}
void RREVERSE();{
digitalWrite(dir_b,LOW);//Control for the right-side motor
}
}