#define SENSOR 2 #define LEFT_MOTOR 3 #define RIGHT_MOTOR 1
#define DIR 1 #define ESQ 2 #define RETO 3
persistent int LEFT_POWER; persistent int RIGHT_POWER;
persistent int perto; persistent int longe;
persistent int dados_sensor[300];
void Wall(){
int pid1; // Espera precionar start antes de comecar sleep(0.5); printf ("\nSeguir Parede"); while(!start_button()); sleep(0.5); pid1 = start_process(WallFollow()); while(!stop_button()); kill_process(pid1); ao();
}
void WallFollow(){
int sensor; int estado = ESQ; LEFT_POWER = 20; RIGHT_POWER = 20; // Define o estado inicial VirarEsq(); estado = ESQ; while (1){ sensor = analog(SENSOR); if (sensor < perto){ VirarDir(); } else{ if (sensor < longe){ AndarReto(); } else{ VirarEsq(); } } }
}
void CalibrarParede(){
int i; printf ("\nPerto"); while (!start_button()); printf ("\nCalibrando.."); perto = 0; for (i=0; i<50; i++){ perto = perto + analog(SENSOR); sleep(0.05); } perto = (int) perto / 50; beep(); printf ("\nPerto: %d", perto); sleep(1.5); printf ("\nLonge"); while (!start_button()); printf ("\nCalibrando.."); longe = 0; for (i=0; i<50; i++){ longe = longe + analog(SENSOR); sleep(0.05); } longe = (int) longe / 50; beep(); printf ("\nLonge: %d", longe); sleep(1.5);
}
Vira para direita void VirarDir(){ motor (LEFT_MOTOR, (int)1); motor (RIGHT_MOTOR, 0); } Vira para esquerda void VirarEsq(){
motor (LEFT_MOTOR, 0); motor (RIGHT_MOTOR, (int)((float)RIGHT_POWER*0.9));
}
Anda Retor void AndarReto(){ motor (LEFT_MOTOR, LEFT_POWER); motor (RIGHT_MOTOR, RIGHT_POWER); }