I've modified the 50$ with sharp ir source code but because i don't have much experience in c, and i don't have robot to test it on yet can someone check my code to see if there are errors?
the robot should avoid obstacles. when it's close to an object and the sensor is right turn right when sensor is left turn left and when sensor is straight turn around
else if object is away from object go straight.
//SoR Include
#include "SoR_Utils.h" //includes all the technical stuff
//global variables
int sharp_IR_reading=0;
int scan_thresh=0;//threshold value of scanning sensor
int scan_angle=30;//position of scanner, in units of servo command
int max_scan_angle=56;//maximum position scanner can rotate to (57)
//this function causes scanning servo to center on edge of object
void scan(void)
{
//lower (-) goes right
//higher (+) goes left
//30 far right, 50 straight, 56 far left (until it jams)
sharp_IR_reading=a2dConvert8bit(3);
if (sharp_IR_reading > scan_thresh)//object detected
{
if (scan_angle>41) //overflow protection
scan_angle-=2;//scan right
}
else //object not detected
{
if (scan_angle<=max_scan_angle) //maximum servo angle
scan_angle+=2; //scan left
else //if scanned all the way, this forces it to start over
scan_angle=30;
}
//servo scan code
servo_scan(scan_angle);
}
//automatically calculates threshold value before run
void autocalibrate(void)
{
scan_thresh=a2dConvert8bit(3);//sensor reading
}
int main(void)
{
//LED_on();
initialize();
delay_cycles(42000);//two second wait delay
/*********ADD YOUR CODE BELOW THIS LINE **********/
//find thresholds
autocalibrate();
//LED_off();
while(1)
{
scan();
if (sharp_IR_reading < scan_thresh && scan_angle =< 42)
robot_turn_right();
else if (sharp_IR_reading < scan_thresh && scan_angle => 55)
robot_turn_left();
else if (sharp_IR_reading => scan_thresh)
robot_go_straight();
else if (sharp_IR_reading < scan_thresh && scan_angle >42 && >55)
robot_turn_right();
robot_turn_right();
delay_cycles(400);//a small delay to prevent crazy oscillations
}
/*********ADD YOUR CODE ABOVE THIS LINE **********/
return 0;
}