Hello experts!! I'm new to this hobby and have little experience in C programming. I bought a DFRobotshop Rover w/c is Arduino compatible basic kit (with tracks) and purchase the Sharp IR GP2D12 with servo. I am impressed with the obstacle avoidance robots. I already test the programming of the forward/backward/left/right of the motors but I could not operate it with the IR and servo. My goal is to detect obstacles in the three angles which is left/center/right. I tried the following code:
int irReader = 1; // the analog input pin for the ir reader
int motoPin1 = 8;
int motoPin2 = 7;
int ena1 = 6;
int ena2 = 5;
void setup()
{
Serial.begin(9600);
pinMode(motoPin1, OUTPUT);
pinMode(motoPin2, OUTPUT);
pinMode(ena1, OUTPUT);
pinMode(ena2, OUTPUT);
int analogRead(irReader);
}
void loop()
{
if (analogRead(irReader) > 60)
{
motor_stop();
delay(500);
backward();
motor_stop();
delay(500);
right_turn();
motor_stop();
delay(500);
}
else if (analogRead(irReader) < 60)
{
forward();
}
}
void backward()
{
digitalWrite(motoPin1, HIGH);
digitalWrite(motoPin2, HIGH);
digitalWrite(ena1, HIGH);
digitalWrite(ena2, HIGH);
}
void forward()
{
digitalWrite(motoPin1, LOW);
digitalWrite(motoPin2, LOW);
digitalWrite(ena1, HIGH);
digitalWrite(ena2, HIGH);
}
void right_turn()
{
digitalWrite(motoPin1, HIGH);
digitalWrite(motoPin2, LOW);
digitalWrite(ena1, HIGH);
digitalWrite(ena2, HIGH);
}
void left_turn()
{
digitalWrite(motoPin1, LOW);
digitalWrite(motoPin2, HIGH);
digitalWrite(ena1, HIGH);
digitalWrite(ena2, HIGH);
}
void motor_stop()
{
digitalWrite(ena1, LOW);
digitalWrite(ena2, LOW);
}
I hope somebody can help with this.
Thanks!!!