ok, so i got V1.1 of the code for PRObot (not the one in my sig, it is now a black painted sigar box), with no OS, for my second robot body,mainly because i have plans to use it for other things.
also, i am now using the servo libary instead of my own for ease of use reasons, although this may be the cause of the problem.
my robot drives, and when it senses a waal, it looks right, then left(recording data from the rangefinder) but then the servo is meant to go back to middle posision, the robot starts slowly turning right and the servo twitches. even stranger, my stop button doesnt work(although i programed it to complete a cycle before listening to it) saying that there is either a fatal error or an infinite loop. this is all the code:
robot1.1:
/* ///////////////
//PRObot 1.1 code/
//by Laurens Weyn/
//for PRObot,also/
//built by /
//Laurens Weyn /
*/ ///////////////
#include <Servo.h>
#define switchpin 12
#define IRPin 0
Servo Lmotor;
Servo Rmotor;
Servo sensor; // create servo object to control a servo
void setup()
{
attatchServos();//attatch / initialize servos
attatchSensors();//set pin modes for sensors
}
void loop()
{
sensor.write(80);//mid point for some reason.(80 degrees,not 90)
if(digitalRead(switchpin)==true)
{
if(analogRead(IRPin)>200)//wall nearby
{
//driveStop(0);//stop the car
if(scan()==true)
{
turnLeft(2000);//turn 90 degrees
}else
{
turnRight(2000);//turn 90 degrees
}
}else {driveForward();delay(100);}//else, continue driving //end wall nearby
}
else driveStop(0);//do not drive, here to prevent robot from driving away randomly(when there is an error in code).
}
sensor:
void attatchSensors()
{
pinMode(switchpin, INPUT);
}
boolean scan()
{
int leftVal;
int rightVal;
sensor.write(0);
delay(300);
leftVal=analogRead(IRPin);
sensor.write(180);
delay(300);
rightVal=analogRead(IRPin);
sensor.write(80);//mid point for some reason.(80 degrees)
if(leftVal>rightVal)
{return true;}else{return false;}
}
servo:
void attatchServos()
{
sensor.attach(2);
Lmotor.attach(8);
Rmotor.attach(9);
}
void turnLeft(int time)
{
Lmotor.write(180);
Rmotor.write(180);
driveStop(time);
}
void turnRight(int time)
{
Lmotor.write(0);
Rmotor.write(0);
driveStop(time);
}
void driveForward()
{
Lmotor.write(0); //drive
Rmotor.write(180);//forwards
}
void driveBackward()
{
Rmotor.write(0); //drive
Lmotor.write(180);//bakwards
}
void driveStop(int time)
{
delay(time);
Lmotor.write(90);//stop
Rmotor.write(90);//stop
}
I don't remember PRECISELY when this crash happens, but it causes a slow right turn on the contunus servos.