กลัวหาย (line follower two sensor)


/*
A simple line following program.
You will need 2 light sensors attached to inputs 1 and 3.
Place the bot on the test pad, with the wheels
on the start line facing forward.
Then start the program.
author: Jan-Klaas Kollhof
last changed by: $LastChangedBy$
last changed date: $Date$
revision: $Revision$
*/
#define LightThreshold 50
#define Fast 75
#define Slow 20
task main(){
SetSensorLight(IN_3);
SetSensorLight(IN_1);
//lets turn 90 deg left
RotateMotorEx(OUT_BC, Fast, 180,-100, true, false);
//now follow the line
OnFwd(OUT_BC, Fast);
while(1){
if(SENSOR_3 < LightThreshold){
//sensor 3 just detected a black line
//so lets go a bit slower on it's side
OnFwd(OUT_B, Fast);
OnFwd(OUT_C, Slow);
}else if (SENSOR_1 < LightThreshold){
//sensor 1 just detected a black line
//so lets go a bit slower on it's side
OnFwd(OUT_B, Slow);
OnFwd(OUT_C, Fast);
}else{
//either it is all black or all light
//lets just go forward
OnFwd(OUT_BC, Fast);
}
}
}
———————————————
/*
A simple line following program.
It works with the standard NXT bot.
Place the bot on the test pad, with the wheels
on the start line facing forward.
Then start the program.
author: Jan-Klaas Kollhof
last changed by: $LastChangedBy$
last changed date: $Date$
revision: $Revision$
*/
#define LightThreshold 50
#define Fast 75
#define Slow 30
task main(){
SetSensorLight(IN_3);
//lets turn 90 deg left
RotateMotorEx(OUT_BC, Fast, 180,-100, true, false);
//now follow the line
OnFwd(OUT_BC, Fast);
while(1){
if(Sensor(IN_3) > LightThreshold){
//we just went off of the black line
//so lets slow down on the right
OnFwd(OUT_B, Slow);
}else{
OnFwd(OUT_B, Fast);
}
}
}
———————————————
/*
A simple programm for avoiding
obstacles using the sonic sensor.
It works with the standard NXT bot.
author: Jan-Klaas Kollhof
last changed by: $LastChangedBy$
last changed date: $Date$
revision: $Revision$
*/
#define VeryClose 20
#define Close 40
#define Slow 30
#define Fast 70
task main(){
SetSensorLowspeed(IN_4);
while(1){
TextOut(0, LCD_LINE1, “sonic”, true);
NumOut(70, LCD_LINE1, SensorUS(IN_4));
int dist = SensorUS(IN_4);
if(dist < VeryClose){
//lets spin 45 deg in a random direction
if(Random(2) == 0){
RotateMotorEx(OUT_BC, Fast, 180, -100, true, false);
}else{
RotateMotorEx(OUT_BC, Fast, 180, 100, true, false);
}
}else{
OnFwd(OUT_BC, Fast);
}
Wait(100);
}
}
อ้างถึง http://jan.kollhof.net