Let's Make Robots!

Programming help, sharp IR mounted on servo


I am trying to control my bot using a sharp IR sensor mounted on hitec servo with arduino mega. The pseudocode i have so far is as follows:

1. Set servo at 90deg and read IR sensor value
2. If val > 200, then stop the bot for a second
  a. Turn servo 0 deg completely and read ir val
  b. if val < 200 then turn bot left
  c. else turn servo 180 deg and read ir val
  d. if val < 200 then turn bot right
  e. else if cant turn right or left, reverse a bit and then turn the bot 180 degree and go straight.
3. repeat

What happens now is that the bot goes forward, if the IR reading comes within 200, the bot turns 180 and goes in that new direction. It doesnt pan the servo left or right.  On two occasions it did turn the servo, but that was only to the left (0 deg).

Have i missed something here? Im just learning how to program, so perhaps someone could suggest a far better method?


I know this code is extremely crappy, but what i was hoping, if it works, was to modify it so that the servo pans say in intervals of 30 degrees, takes a reading and stores it. It then calulates based on the readings the best route(ie with least obstacle distance) and moves the bot in that direction. Im not very sure how to implement this either.


Any help would be appreciated. Thank you


#include <MegaServo.h>
#define IR_pin 0

int IR_val = 0;

MegaServo servo1;

void setup() {
  servo1.write(90); //center servo intially


void loop() {
    IR_val = analogRead(IR_pin);

if (IR_val > 200) // obstacle detected
    motor_stop();     // Stop both motors...
    delay(1000);      // ...and pause for a second 
    servo1.write(0);  // Turn servo left completely and scan
    IR_val = analogRead(IR_pin);   
    if (IR_val < 200) // if left seems to be clear
        delay(535);    // Turn till bot has turned 90 deg left
        servo1.write(90); // center the servo
        servo1.write(180); // Turn servo completely right and scan
        IR_val = analogRead(IR_pin);   
       if (IR_val < 200) // if right seems to be clear
           delay(540);    // Turn till bot has turned 90 deg right
           servo1.write(90); // center the servo
      servo1.write(90); // position servo back at center
      delay(500);  // wait for sometime till it reverses
  } // end of routine that checks if IR_val > 200

/* Motor movements...removed to save space!! Btw..all these functions work fine. I've tested them.

void motor_stop()
void go_straight()
void go_reverse()
void turn_bot_right()
void turn_bot_left()
void turn_bot_180()



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Yes, pinmode is for digital output/input. Actually, that line isnt needed there at all! If im not mistaken, servos use PPM?
PPM refers to a radio control radio formating of the signal, compared to PCM. Servos operate off a PWM signal, changing postion based on the width of the incoming pulse.
radio control as in radio transmitters/receivers?
Yes, as in model airplanes and cars/trucks. The receiver gets the PPM or PCM signal from the transmitter and converts it to several PWM signals for the servos.
Obviously, i was mistaken. Thanks for the info.