[[2005/B7]] 目次 #contents * 最初のプログラム [#qf51e1e9] // 最初のプログラム // // 作成者:かずま // 作成日:2005-10-14 task main() { OnFwd(OUT_A+OUT_C); // AとCのモータを正回転 Wait(300); // プログラムの実行を3秒待つ Off(OUT_A+OUT_C); // AとCのモーターを止める } #comment * 正方形に沿って動くロボット [#g57bcd35] // 正方形に沿って動くロボット // // 作成者:かずま // 作成日:2005-10-21 task main() { repeat(4) { OnFwd(OUT_A+OUT_C); // AとCのモータを正回転 Wait(200); // プログラムの実行を2秒待つ Off(OUT_A+OUT_C); // AとCのモータを止める OnFwd(OUT_A); // Aのモーターを正回転 OnRev(OUT_C); // Cのモーターを逆回転 Wait(100); // プログラムの実行を1秒待つ Off(OUT_A+OUT_C); // AとCのモーターを逆回転 } } #comment *光を追いかけるプログラム [#n14ec8fa] #define AROUND_TIME 500 #define gosa 12 #define search_frequency 20 int light=0; int turn; void turn_right(int t) { OnFwd(OUT_A); OnRev(OUT_C); Wait(t); Off(OUT_A+OUT_C); } void turn_left(int t) { OnRev(OUT_A); OnFwd(OUT_C); Wait(t); Off(OUT_A+OUT_C); } sub search() { turn_right(turn + search_frequency); turn=turn + search_frequency; turn_left(turn + search_frequency); turn=turn + search_frequency; } task main() { SetSensor(SENSOR_1, SENSOR_LIGHT); ClearTimer(0); OnFwd(OUT_A); OnRev(OUT_C); while(Timer(0) <= AROUND_TIME/10) { if(SENSOR_1 > light) { light=SENSOR_1; ClearTimer(0); } if(Timer(0) == AROUND_TIME/10) { Off(OUT_A+OUT_C); } } while(Timer(0) > AROUND_TIME/10) { if(light - gosa <= SENSOR_1 <= light + gosa) { turn=0; OnFwd(OUT_A+OUT_C); } if((SENSOR_1 < light - gosa) || (SENSOR_1 > light + gosa)) { search(); } } } *光を追いかけるプログラム2 [#tff3accb] #define AROUND_TIME 500 #define gosa 12 #define search_frequency 20 int light=0; int time=0; int turn; void turn_right(int t) { OnFwd(OUT_A); OnRev(OUT_C); Wait(t); Off(OUT_A+OUT_C); } void turn_left(int t) { OnRev(OUT_A); OnFwd(OUT_C); Wait(t); Off(OUT_A+OUT_C); } sub search() { turn_right(turn + search_frequency); turn=turn + search_frequency; turn_left(turn + search_frequency); turn=turn + search_frequency; } task main() { SetSensor(SENSOR_1, SENSOR_LIGHT); ClearTimer(0); OnFwd(OUT_A); OnRev(OUT_C); while(Timer(0) <= AROUND_TIME/10) { if(SENSOR_1 > light) { light=SENSOR_1; time=Timer(0); } if(Timer(0) == AROUND_TIME/10) { Off(OUT_A+OUT_C); } while(AROUND_TIME/10 < Timer(0) <= AROUND_TIME/10 + time) { if(time <= AROUND_TIME/20) { turn_right(time); Off(OUT_A+OUT_C); } if(time > AROUND_TIME/20) { turn_left(AROUND_TIME/10 - time); Off(OUT_A+OUT_C); } } while(Timer(0) > AROUND_TIME/10 + time) { if(light - gosa <= SENSOR_1 <= light + gosa) { turn=0; OnFwd(OUT_A+OUT_C); } if((SENSOR_1 < light - gosa) || (SENSOR_1 > light + gosa)) { search(); } } }