robot with sharp IR

hello everybody,
I’m building a robot with an arduino, two servos (continuous rotation) for the two wheels, one servo 0–>180 for the head scanning, two leds for the eyes and a speaker to make weird noises :wink:
For the moment, it goes forward and when it detects an obstacle, it turns until te way is clear… I’m already happy because I’m beginner, but I’d like to improve it: I’d like it to execute a sequence…like an animation, for example: when it detects an obstacle, I’d like it to stop for two seconds,than head scanning to the right and to the left and and then turn to the clearest way.
I paste the code I use
Thanx
#include <Servo.h>

#define IR_pin 0

int ledPinR = 11;
int ledPinL = 12;
int IR_val = 0;
Servo myservo_tete;
Servo myservo_gauche;
Servo myservo_droite;

int speakerPin = 9;

void setup()

{
Serial.begin(9600);
pinMode(speakerPin, OUTPUT);

myservo_tete.attach(2);
myservo_gauche.attach(3);
myservo_droite.attach(4);

pinMode(ledPinR, OUTPUT);
pinMode(ledPinL, OUTPUT);
}

void loop() {
IR_val = analogRead(IR_pin);
Serial.println(IR_val);

if (IR_val < 250){
myservo_droite.write(50);
myservo_gauche.write(130);
digitalWrite(ledPinR, HIGH);
digitalWrite(ledPinL, HIGH);
digitalWrite(speakerPin, LOW);
}
else {
myservo_droite.write(100);
myservo_gauche.write(80);

digitalWrite(speakerPin, HIGH);

digitalWrite(ledPinR, HIGH);
digitalWrite(ledPinL, HIGH);
delay(IR_val);
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinL, LOW);
delay(IR_val);
digitalWrite(speakerPin, LOW);

}
}

I built a "sumo robot" the other year with an AVR for uni, and it is amazing how quickly you can build up a robot with a few good base functions.

What you can do to make it a bit better is create subroutines: MoveForward, MoveBack, TurnLeft, TurnRight, Wait... These should move robot in a direction for around about 500mS.

After that use these subroutines to create event reactions: WallDivert, FoundLine,... Which contain a few of these subroutines.

What is important is that you have interupts with your sensors, so that if you are doing an event reaction you can stop that and attend to other problems (like a wall).

Some cool stuff you can add are - Line sensors - Bump sensors - Sonar sensor

Check out www.sparkfun.com http://www.societyofrobots.com/ http://www.pololu.com/

For a good second project, see if you can create a robot which uses line sensors to stay in a circle of masking tape. To make it tricky, have it seek out an object, such as a box and push it out of the "arena".

See if you can make one which can get through a maze drawn on the ground with the same masking tape.

:)

Manut, I’m doing very much the same thing, but with an R/C car and its ESC. I’m using the Sharp IR sensor (GP2Y0D810Z0F) for now since they’re cheap enough to experiment with and simple in design. the range is pretty low on them though.

Here’s me code so far…can I get a sanity check??

/* Convert an R/C vehicle to an autonomous robot.  2 IR sensors at the front of the vehicle will detect obstacles & signal the vehicle to stop, backup, turn 

and move forward again.  Speed is set very slow for the first test run. */

#include <Servo.h> 

Servo SteeringServo;
Servo ElecSpeedCntl;

int DriveMotorPin = 0;
int FrLeftSensorPin = 1;
int FrRightSensorPin = 2;
int SteeringServoPin = 3;
int FrLeftSensorValue = 1;
int FrRightSensorValue = 1;
int x;

void arm() 
{
      setSpeed (50);      // arm the ESC
      delay(1000);      // delay for 1 sec
}

void setSpeed (int speed)
{
       // speed is from -100 to 100 where 0 is off and 100 is max speed
       
        int angle = map(speed, 0, 100, 0, 180);      // maps speed values of 0 to 100 to angles from 0 to 180
        ElecSpeedCntl.write(angle);    
}

void setup ()
{ 
      pinmode (DriveMotorPin, OUTPUT);      // sets pin to drive motors as output
      pinmode (FrLeftSensorPin, INPUT);      // sets pin for IR sensor as input
      pinmode (FrRightSensorPin, INPUT);      // sets pin for IR sensor as input
      pinmode (SteeringServoPin, OUTPUT);      // sets pin to drive servos as output

      SteeringServo.attach(SteeringServoPin);      // sets pin for steering servo control
      ElecSpeedCntl.attach(DriveMotorPin);      // sets pin for electronic speed control
      arm();
}

void loop ()
{
      setSpeed(60);      // start at low speed

      x = 0;
      while (x < 1) {
      
            FrLeftSensorValue = FrLeftSensorPin;      // get left sensor value
            FrRightSensorValue = FrRightSensorPin;      // get right sensor value
      
            If (FrLeftSensorValue = LOW) {
                  setSpeed(50);                        // stop
                  delay (2000);                        // delay 2 seconds
                  setSpeed(70);                        // Drive in reverse
                  delay (2000);                        // delay 2 seconds
                  setSpeed(50);                        // stop
                  SteeringServo.write(112);            // turn right
                  setSpeed(60);                        // Drive fwd
                  delay (2000);                        // delay 2 seconds
                  setSpeed(50);                        // stop
                  SteeringServo.write(90);            // bring servo back to center
                  delay(2000);                        // delay 2 seconds
                  break;
            }
            else if (FrRightSensorValue = LOW)  {
                  setSpeed(50);                        // stop
                  delay (2000);                        // delay 2 seconds
                  setSpeed(70);                        // Drive in reverse
                  delay (2000);                        // delay 2 seconds
                  setSpeed(50);                        // stop
                  SteeringServo.write(67);            // turn left
                  setSpeed(60);                        // Drive fwd
                  delay (2000);                        // delay 2 seconds
                  setSpeed(50);                        // stop
                  SteeringServo.write(90);            // bring servo back to center
                  delay(2000);                        // delay 2 seconds
                  break;
            }

      }
}