Aurdino with Sharp IR Proximity Sensor.

Hello

Myself and a few friends have been trying to build a robot for our university assignment which is due in about a weeks time. We were just having a few problems and were hoping we could get some help.

We require this robot to use sensors to drive thought a course avoid a few simple obstacles and place a softball in a bucket.

We currently have an audrino UNO SMD edition attached to a Dagu 4 Channel DC Motor Controller in the following configuration.

Channel One
PWM pin 3~
DIR pin 4

Channel Two
PWM pin 5~
DIR pin 7

Channel 3
PWM pin 10~
DIR pin 8

Channel 4
PWM pin 11~
DIR pin 12

We do not have any of the encoders or the current sensors attached yet we are just trying to take all of this one step at a time since it is our first robot We dont really plan on using the encoders.

We have 6 AA batteries to a breadboard which powers the audrino and the motor controller. We use the 5V ouput on the audrino to power the Logic part of the Dagu motor controller. We have not yet attached any sensors to the robot because as i said we are just trying to build up to getting this thing ready.

We have attached a Short range proximity sensor, i have read the datasheet but i am still having problems.

The main function of this sensor is to tell the audrino to tell the motor controller when to turn. It is connected to the A0 Analog pin. I have the sensor coming off a 9V supply passing through a voltage regulator. I have tried incorporating capacitors into this design but it hasn't helped much so i could be doing this wrong ethier that or it is something else i have done. I was planning on using the 5V output on the audrino board for powering the sensor however i am using that to power the logic for the motor contoller would it be possible for using it for both? If not would anyone know a better approach to setting this up.

Our main problem is we are trying to make the robot advoid obstacles by using this sensor. But at the moment it ethier turns way to early at around 60 cm or it does not turn at all.

At the moment this is the code we are currently running on the aurdino which one of my group members wrote.

int PinPWM_b1 = 3; //ch1
int PinPWM_b2 = 5; //ch2
int PinPWM_y1 = 10; //ch3
int PinPWM_y2 = 11; //ch4

int PinRot_g1 = 4; //ch1
int PinRot_g2 = 7; //ch2
int PinRot_r1 = 8; //ch3
int PinRot_r2 = 12; //ch4

int sensorPin=0; // The signal pin for the sensor
//int prox_min_short = // about 4cm
//int prox_max_short = // about 30cm
//int prox_min_mid = // about 5cm
//int prow_max_mid = // about 80cm

#define HIGH 0x1
#define LOW 0x0

// 2 is right motors 1 is left motors

void setup()
{
Serial.begin(9600);
pinMode(PinPWM_b1,OUTPUT); // back left motor pwm
pinMode(PinPWM_b2,OUTPUT); // back right motor pwm
pinMode(PinPWM_y1,OUTPUT); // front left motor pwm
pinMode(PinPWM_y2,OUTPUT); // front right motor pwm

pinMode(PinRot_g1,OUTPUT); // back left motor dir
pinMode(PinRot_g2,OUTPUT); // back right motor dir
pinMode(PinRot_r1,OUTPUT); // front left motor dir
pinMode(PinRot_r2,OUTPUT); // front right motor dir
}

void loop()
{

delay(2000);
digitalWrite(PinRot_g1,LOW); // turn all motors forward
digitalWrite(PinRot_g2,LOW);
digitalWrite(PinRot_r1,HIGH);
digitalWrite(PinRot_r2,HIGH);
analogWrite(PinPWM_b1,200); // provide power to all motors
analogWrite(PinPWM_b2,200); // test using HIGH or 255/200/etc HIGH is full power.
analogWrite(PinPWM_y1,200);
analogWrite(PinPWM_y2,200);
delay(1000);
digitalWrite(PinRot_g1,LOW); // turn all motors forward
digitalWrite(PinRot_g2,LOW);
digitalWrite(PinRot_r1,HIGH);
digitalWrite(PinRot_r2,HIGH);
analogWrite(PinPWM_b1,200); // provide power to all motors
analogWrite(PinPWM_b2,200); // test using HIGH or 255/200/etc HIGH is full power.
analogWrite(PinPWM_y1,200);
analogWrite(PinPWM_y2,200);
delay(1000);

float volts = analogRead(sensorPin)0.00322265624; // Reads the signal from the sensor, returning a number. The number is higher the closer an object gets.
float distance = 12.21
pow(volts,-1.15);
Serial.println(distance);
delay(1000);

if (distance > 200 ) // we have tried all sorts of values for this
{
digitalWrite(PinPWM_b1,LOW); // stop motors
digitalWrite(PinPWM_b2,LOW);
digitalWrite(PinPWM_y1,LOW);
digitalWrite(PinPWM_y2,LOW);
delay(1000);
digitalWrite(PinRot_r2,HIGH); // turn right motors
digitalWrite(PinRot_g2,LOW);
digitalWrite(PinRot_r1,LOW); // turn left motors
digitalWrite(PinRot_g1,HIGH);
analogWrite(PinPWM_y2,200); // provide power to right motors
analogWrite(PinPWM_b2,200);
analogWrite(PinPWM_y1,200); // provide power to left motors
analogWrite(PinPWM_b1,200);
delay(200);
digitalWrite(PinRot_r2,HIGH); // turn right motors
digitalWrite(PinRot_g2,LOW);
digitalWrite(PinRot_r1,LOW); // turn left motors
digitalWrite(PinRot_g1,HIGH);
analogWrite(PinPWM_y2,200); // provide power to right motors
analogWrite(PinPWM_b2,200);
analogWrite(PinPWM_y1,200); // provide power to left motors
analogWrite(PinPWM_b1,200);
delay(200);
digitalWrite(PinRot_r2,HIGH); // turn right motors
digitalWrite(PinRot_g2,LOW);
digitalWrite(PinRot_r1,LOW); // turn left motors
digitalWrite(PinRot_g1,HIGH);
analogWrite(PinPWM_y2,200); // provide power to right motors
analogWrite(PinPWM_b2,200);
analogWrite(PinPWM_y1,200); // provide power to left motors
analogWrite(PinPWM_b1,200);
delay(1000);
}
else
{
digitalWrite(PinRot_g1,LOW); // turn all motors forward
digitalWrite(PinRot_g2,LOW);
digitalWrite(PinRot_r1,HIGH);
digitalWrite(PinRot_r2,HIGH);
analogWrite(PinPWM_b1,200); // provide power to all motors
analogWrite(PinPWM_b2,200); // test using HIGH or 255/200/etc HIGH is full power.
analogWrite(PinPWM_y1,200);
analogWrite(PinPWM_y2,200);
delay(1000);
digitalWrite(PinRot_g1,LOW); // turn all motors forward
digitalWrite(PinRot_g2,LOW);
digitalWrite(PinRot_r1,HIGH);
digitalWrite(PinRot_r2,HIGH);
analogWrite(PinPWM_b1,200); // provide power to all motors
analogWrite(PinPWM_b2,200); // test using HIGH or 255/200/etc HIGH is full power.
analogWrite(PinPWM_y1,200);
analogWrite(PinPWM_y2,200);
delay(1000);
}

//digitalWrite(PinPWM_b1,LOW); // stop motors
//digitalWrite(PinPWM_b2,LOW);
//digitalWrite(PinPWM_y1,LOW);
//digitalWrite(PinPWM_y2,LOW);
//delay(1000);
//digitalWrite(PinRot_g1,HIGH); // turn all motors backwards
//digitalWrite(PinRot_g2,HIGH);
//digitalWrite(PinRot_r1,LOW);
//digitalWrite(PinRot_r2,LOW);
//analogWrite(PinPWM_b1,200); // provide power to all motors
//analogWrite(PinPWM_b2,200);
//analogWrite(PinPWM_y1,200);
//analogWrite(PinPWM_y2,200);
//delay(1000);
//digitalWrite(PinRot_g1,HIGH); // turn all motors backwards
//digitalWrite(PinRot_g2,HIGH);
//digitalWrite(PinRot_r1,LOW);
//digitalWrite(PinRot_r2,LOW);
//analogWrite(PinPWM_b1,200); // provide power to all motors
//analogWrite(PinPWM_b2,200);
//analogWrite(PinPWM_y1,200);
//analogWrite(PinPWM_y2,200);
//delay(1000);
//digitalWrite(PinPWM_b1,LOW); // stop motors
//digitalWrite(PinPWM_b2,LOW);
//digitalWrite(PinPWM_y1,LOW);
//digitalWrite(PinPWM_y2,LOW);
//delay(1000);
//digitalWrite(PinRot_r2,HIGH); // turn right motors
//digitalWrite(PinRot_g2,LOW);
//digitalWrite(PinRot_r1,LOW); // turn left motors
//digitalWrite(PinRot_g1,HIGH);
//analogWrite(PinPWM_y2,200); // provide power to right motors
//analogWrite(PinPWM_b2,200);
//analogWrite(PinPWM_y1,200); // provide power to left motors
//analogWrite(PinPWM_b1,200);
//delay(1000);
//digitalWrite(PinRot_r2,HIGH); // turn right motors
//digitalWrite(PinRot_g2,LOW);
//digitalWrite(PinRot_r1,LOW); // turn left motors
//digitalWrite(PinRot_g1,HIGH);
//analogWrite(PinPWM_y2,200); // provide power to right motors
//analogWrite(PinPWM_b2,200);
//analogWrite(PinPWM_y1,200); // provide power to left motors
//analogWrite(PinPWM_b1,200);
//delay(1000);
//digitalWrite(PinRot_r2,HIGH); // turn right motors
//digitalWrite(PinRot_g2,LOW);
//digitalWrite(PinRot_r1,LOW); // turn left motors
//digitalWrite(PinRot_g1,HIGH);
//analogWrite(PinPWM_y2,200); // provide power to right motors
//analogWrite(PinPWM_b2,200);
//analogWrite(PinPWM_y1,200); // provide power to left motors
//analogWrite(PinPWM_b1,200);
//delay(1000);
//digitalWrite(PinPWM_b1,LOW); // stop motors
//digitalWrite(PinPWM_b2,LOW);
//digitalWrite(PinPWM_y1,LOW);
//digitalWrite(PinPWM_y2,LOW);
//delay(1000);
}

I will upload some pictures as soon as i get them from a fellow team member. Thankyou for looking any help would be greatly appreciated.

Any help will be much appreciated.

Thanks from Luke

you should learn about functions to make your code more readable e.g.

void step( int b1, int b2, int y1, int y2)
{
  analogWrite(PinPWM_y2, y2); // provide power to right motors
  analogWrite(PinPWM_b2, b2);
  analogWrite(PinPWM_y1, y1); // provide power to left motors
  analogWrite(PinPWM_b1, b1); 
}

Furthermore you should learn to get rid of the delays - google for the blink without delay example -