Robot C-program for Group C-3 (light), E5 Fall 2002
Yavor Georgiev
Tal Levy
Shalaj Tater
int abs(int x)
{
if (x < 0) x = -1 * x;
return x;
}
void turnaround(void) {
motor(2,-100);
motor(3,-100);
sleep(3.0);
motor(3,100);
sleep(2.5);
}
void turnleft(void){
motor (2, 100);
motor (3, 0);
}
void turnright(void){
motor (2, 0);
motor (3, 100);
}
void show_values(void){
printf ("\na2: %d a3: %d", analog(2), analog(3));
}
void movefwd(void){
motor (2, 100);
motor (3, 100);
}
int max = 0;
void calibrate(void){
while (start_button()==0){
show_values();
}
max = analog(2);
printf("\n10 sec to start");
sleep(10.0);
}
void choose(void){
if ((analog(2)-analog(3))>5){
turnleft();
}
if ((analog(3)-analog(2))>5){
turnright();
}
if ((abs(analog(2)-analog(3)))<=5){
movefwd();
}
}
int done=0;
void win(void){
if ((analog(2) >= (max-5)) || (analog(3) >= (max-10))){
ao();
tone(500.0, 1.0);
sleep(1.0);
tone(500.0, 1.0);
sleep(1.0);
tone(500.0, 1.0);
sleep(1.0);
done=1;
}
}
int screwedup=0;
void screwup(void){
if(seconds() >= 40.0){
reset_system_time();
tone(500.0, 1.0);
screwedup=1;
}
if((screwedup==1)&&(seconds() <= 15.0)){
movefwd();
} else {
screwedup=0;
}
}
void main(void){
int x, y, z;
calibrate();
motor(2,100);
motor(3,100);
reset_system_time();
while(!stop_button()){
while(x=digital(7) && y=digital(8) && z=!stop_button()&&done==0){
show_values();
choose();
screwup();
win();
}
if (!z || done==1){
ao();
} else {
turnaround();
}
}
ao();
}