Go Down

Topic: Using IR Proximity Sensor to control motors (Read 2198 times) previous topic - next topic

Silent Siren

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.


Code: [Select]
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
}
}
 
 

wildbill

This:
Code: [Select]
  if (sensorValue = 0)

Should be this:
Code: [Select]
  if (sensorValue == 0)

Silent Siren

Thanks wildbill but the change didn't work. In the place of the motors I plased an LED to check if they would work, and they did work but the light emitted by them was really dim.

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Silent Siren

Thanks AWOL but by removing the semicolons it doesn't want to compile, sorry, what do you think is wrong?

AWOL

Quote
what do you think is wrong?

I think that this version of C doesn't like nested function declarations.
Move the functions out of loop, and LOSE THE SEMICOLONS.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up