目次
ライントレース
半分アメリカ人
コースをライントレースして走り、コース上にある障害物の空き缶二つを撤去して一周する。右回り、左回りどちらもできるようにする。
#define VERY_WHITE THRESHOLD+7 #define WHITE THRESHOLD+3 #define THRESHOLD 53 #define BLACK THRESHOLD-2 #define VERY_BLACK THRESHOLD-8 #define SPEED_H 35 #define SPEED_L 25 #define OnRL(speedR,speedL) OnFwd(OUT_A,speedR);OnFwd(OUT_C,speedL); #define go_foward OnRL(SPEED_H,SPEED_H); #define spin_left OnRL(SPEED_L+10,-SPEED_L); #define turn_left OnRL(SPEED_L,0); #define turn_right OnRL(0,SPEED_L); #define spin_right OnRL(-SPEED_L,SPEED_L+10); #define STEP 1 #define nMAX 240 #define short_break Off(OUT_AC);Wait(100); #define CROSS_TIME 800 #define cross_line OnRL(SPEED_L,SPEED_L);Wait(CROSS_TIME);short_break; #define stuck_time 3000 #define DO 523 #define RE 587 #define MI 659 #define FA 698 #define curve_time 29000 #define start_time 20000 #define swiching_time 5000 #define after_cross 7000
task main() {//1 SetSensorLight(S3); int RUN=0;//main while() program int nOnline=0;//Black counter int fOnline=0; int CrossCount=0;//Counts of crossed lines int FinalSwitch=0; long t0,t1,t2,t3 = CurrentTick(); t1=CurrentTick();//Set t1 while(CurrentTick()-t1<start_time){//2 //While start_time if(SENSOR_3<VERY_BLACK){//1' spin_right; }//1' else {//2' if(SENSOR_3<BLACK){//3' turn_right; }//3' else if(SENSOR_3<WHITE){//4' go_foward; }//4' else if(SENSOR_3<VERY_WHITE){//5' turn_left; }//5' else {//6' spin_left; }//6' }//2' }//2 //End of "While start_time"
turn_left;Wait(800); while(RUN==0){//3 //First Cross Detect while(nOnline<nMAX){//4 //Basic cross finding if(SENSOR_3<VERY_BLACK){//5 spin_left; nOnline++; }//5 else {//6 if(SENSOR_3<BLACK){//7 turn_left; }//7 else if(SENSOR_3<WHITE){//8 go_foward; }//8 else if(SENSOR_3<VERY_WHITE){//9 turn_right; }//9 else {//10 spin_right; }//10 nOnline=0; }//6 }//4 //End of First Cross Detect short_break;PlaySound(SOUND_CLICK); spin_right;Wait(nMAX*STEP/2); cross_line; PlaySound(SOUND_UP); CrossCount +=1; nOnline=0;
while(CrossCount==2){//1'' Hit second cross line turn_left;Wait(1000); while(nOnline<nMAX){//4 //Backwards cross finding if(SENSOR_3<VERY_BLACK){//5 spin_right; nOnline++; }//5 else {//6 if(SENSOR_3<BLACK){//7 turn_right; }//7 else if(SENSOR_3<WHITE){//8 go_foward; }//8 else if(SENSOR_3<VERY_WHITE){//9 turn_left; }//9 else {//10 spin_left; }//10 nOnline=0; }//6 }//4 //End of Backwards cross finding short_break;PlaySound(SOUND_CLICK); spin_right;Wait(nMAX*STEP/2); cross_line; PlaySound(SOUND_UP); CrossCount +=1; nOnline=0; }//1'' end of Hit second cross line while(CrossCount==3){//1''' Hit third cross line turn_left;;Wait(1000); while(nOnline<nMAX){//4 //Backwards cross finding if(SENSOR_3<VERY_BLACK){//5 spin_right; nOnline++; }//5 else {//6 if(SENSOR_3<BLACK){//7 turn_right; }//7 else if(SENSOR_3<WHITE){//8 go_foward; }//8 else if(SENSOR_3<VERY_WHITE){//9 turn_left; }//9 else {//10 spin_left; }//10 nOnline=0; }//6 }//4 //End of Backwards cross finding short_break;PlaySound(SOUND_CLICK); spin_right;Wait(nMAX*STEP/2); cross_line; PlaySound(SOUND_UP); CrossCount +=5; FinalSwitch +=1; nOnline=0; }//1''' end of Hit third cross line }//3 End of Main Program
while(FinalSwitch==1){//1'''' Hits final line PlaySound(SOUND_UP); spin_right;Wait(400); nOnline=0; t3=CurrentTick(); while(CurrentTick()-t3<after_cross){//2 //While after_cross if(SENSOR_3<VERY_BLACK){//1' spin_right; }//1' else {//2' if(SENSOR_3<BLACK){//3' turn_right; }//3' else if(SENSOR_3<WHITE){//4' go_foward; }//4' else if(SENSOR_3<VERY_WHITE){//5' turn_left; }else {//6' spin_left; }//6' }//2' }//2 //End of "While after_cross" while(fOnline<nMAX){//4 //Final cross finding if(SENSOR_3<VERY_BLACK){//5 spin_left; fOnline++; }//5 else {//6 if(SENSOR_3<BLACK){//7 turn_left; }//7 else if(SENSOR_3<WHITE){//8 go_foward; }//8 else if(SENSOR_3<VERY_WHITE){//9 turn_right; }//9 else {//10 spin_right; }//10 fOnline=0; }//6 }//4 //End of Final cross finding PlaySound(SOUND_UP); FinalSwitch +=1; }//1'''' End of Hits final line
go_foward;PlayTone(RE,1);Wait(400); spin_left;Wait(400); go_foward;Wait(400); spin_left;Wait(3000); PlaySound(SOUND_DOWN); Off(OUT_AC); }//1
一進一退を繰り返していたが、根気よくプログラミングできなかった。結果的に、十分に時間をかけて調整しなかったため、1周することなく終わってしまった。