Hello,
I'm going to make an arduino based robot that will avoid object in front of it.
The sensor will be a SharpIr GP2D120.
I'll use 2 HT-311 servo modified by myself.
While I'm waiting for the hardware, I start working on the code.
Could you check if there's any big mistake ( I tried keep it simple so I can easily figure what going wrong but would like your thinks).
Later I'll add more thing ( like scanning with a servo+ sharpIr so I can set direction and angle to turn)
Ty
// * Robot avoiding objects V1.0 4JUN10 * //
#include <Servo.h>
const int sharpPin=4; // set n° of the pin
Servo leftservo;
Servo rightservo;
Servo scanservo;
int distance=0;
int threshold=0;
int leftspeed=0;
int rightspeed=0;
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
void setup() {
pinMode(sharpPin, INPUT); //set the pin of sharpIR and servo NB: n° of the pin are
leftservo.attach(9); // fictive - waiting for the board.
rightservo.attach(8);
scanservo.attach(7);
setThreshold ;
}
void setThreshold() { //set the threshold value doing a mean of 5 distance value
a=analogRead(sharpPin);
delay(40);
b=analogRead(sharpPin);
delay(40);
c=analogRead(sharpPin);
delay(40);
d=analogRead(sharpPin);
delay(40);
e=analogRead(sharpPin);
threshold=(a+b+c+d+e)/5;
}
void loop()
{
distance=analogRead(sharpPin);
if (distance > threshold) { // If no object in the way
leftspeed=180;
rightspeed=180;
leftservo.write(leftspeed); // go forward
rightservo.write(rightspeed);
}
else {
leftspeed=135;
rightspeed=45;
leftservo.write(leftspeed); // else turn right
rightservo.write(rightspeed);
}
}