Pages: [1]   Go Down
Author Topic: Using IR Proximity Sensor to control motors  (Read 1611 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
}
}
 
 
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 51
Posts: 3435
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This:
Code:
  if (sensorValue = 0)

Should be this:
Code:
  if (sensorValue == 0)
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24470
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void LFORWARD();{
Lose the semicolons.
Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24470
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Pages: [1]   Go Up
Jump to: