Go Down

Topic: Sharp IR problem (Read 767 times) previous topic - next topic

greasemonkey94

Mar 01, 2011, 01:15 pm Last Edit: Mar 01, 2011, 01:21 pm by greasemonkey94 Reason: 1
Hello,

I just recently finished building my robot, it has a sharp IR gp2d12 sensor on a servo for basic obstacle avoidance,
I'm using an arduino uno and l293d motor driver.
It worked beautifully(relatively, remember this is my first bot!!) for a while and then it seemed to detect obstacles all
the time, it never went straight.
It seems to stop working after the unplug the battery, I have 2 power supplies a 12 900mAh NiCd for the motors and
a 9V battery for the arduino (i haven't forgotten common ground, I checked  ;D)

It seems to work fine for sometime and then goes completely haywire, this has happened 3-4 times, each time i reloaded the code :(

Does anyone know whats wrong or what im doing wrong, tell me if you need picks of my setup.

Is this a hardware or software problem??. It gives weird readings like 30cm,14cm etc (i powered the uno thoguht the usb and saw the readings via serial) even when when i tilt it towards the ceiling and there is no object to be seen .



here is the code I'm using



Basically whats its supposed to do is move straight until it finds an object, then it stops and scans 90 degrees to its left and right and then moves in the direction of greatest object distance.



Code: [Select]
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int motor_left[] = {2, 3};
int motor_right[] = {7, 6};
int IRpin=0 ;
       
void setup()
{
myservo.attach(9);// attaches the servo on pin 9 to the servo object
// Setup motors
int i;
for(i = 0; i < 2; i++)
{
 pinMode(motor_left[i], OUTPUT);
 pinMode(motor_right[i], OUTPUT);  
 }
}


void loop()
{
myservo.write(90);
delay(250);
int sharpir=getsharpdata();
drive_forward();

if(sharpir<20)
{
 
  servo_scan();
}

}

// --------------------------------------------------------------------------- Drive

void motor_stop()
{
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(250);
}

void drive_forward()
{
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void drive_backward()
{
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

void turn_left()
{
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void turn_right()
{
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

float getsharpdata()
{
 float val = analogRead(IRpin);
 float distance = (2914 / (val + 5)) - 1;
 delay(50);
 return distance;
 
}


void servo_scan()
{
motor_stop();
myservo.write(0);
delay(1000);
int left=getsharpdata();
myservo.write(180);
delay(1000);    
int right=getsharpdata();

if(left>right)
{
 turn_left();
 delay(500);
}

else
{
  turn_right();
  delay(500);
}

}



Any and all help appreciated hehehe  ;)

Thanks

Artem_F

what does the voltmeter show for IR sensor output?
It's a "regular" voltage that can be measured by anything.
I mean, it's an analog sensor so you'll easily identify if
it's a sensor that went crazy or it's the board that can't digitize
the voltage properly.

Go Up