目次
// 最初のプログラム // // 作成者:かずま // 作成日:2005-10-14 task main() { OnFwd(OUT_A+OUT_C); // AとCのモータを正回転 Wait(300); // プログラムの実行を3秒待つ Off(OUT_A+OUT_C); // AとCのモーターを止める }
// 正方形に沿って動くロボット // // 作成者:かずま // 作成日: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のモーターを逆回転 } }
#define AROUND_TIME 500 // ロボが一周するのにかかる時間を定義 #define gosa 15 // 光センサの反応誤差を考慮 #define search_frequency 20 // サーチの範囲を定義 int light=0; int turn=0; 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=turn + search_frequency; turn_right(turn); turn=turn + search_frequency; turn_left(turn); } 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) { while(light - gosa <= SENSOR_1 <= light + gosa) { turn=0; OnFwd(OUT_A+OUT_C); } while((SENSOR_1 < light - gosa) || (SENSOR_1 > light + gosa)) { search(); } } }
・なんでかうまくいかない…orz
#define AROUND_TIME 500 #define error 12 #define search_range 20 int light=0; int time=0; int move_time=0; int turn=0; void turn_right(int t) { OnFwd(OUT_A); OnRev(OUT_C); Wait(t); } void turn_left(int t) { OnRev(OUT_A); OnFwd(OUT_C); Wait(t); } sub search() { turn=turn + search_range; turn_right(turn); turn=turn + search_range; turn_left(turn); } 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(time <= AROUND_TIME/20) { move_time=time; } if(time > AROUND_TIME/20) { move_time=AROUND_TIME/10 - time; } } if(Timer(0) == AROUND_TIME/10) { Off(OUT_A+OUT_C); } } while(AROUND_TIME/10 < Timer(0) <= AROUND_TIME/10 + move_time) { if(time <= AROUND_TIME/20) { turn_right(move_time); Off(OUT_A+OUT_C); } if(time > AROUND_TIME/20) { turn_left(move_time); Off(OUT_A+OUT_C); } } while(Timer(0) > AROUND_TIME/10 + move_time) { while(light - error <= SENSOR_1 <= light + error) { turn=0; OnFwd(OUT_A+OUT_C); } while((SENSOR_1 < light - error) || (SENSOR_1 > light + error)) { search(); } } }
・上のがうまくいかないのに発展型を作ってしもた。当然うまくいかない…orzなぜじゃぁぁぁぁぁぁぁ!!!
#define Do 523 #define Re 587 #define Mi 659 #define Fa 698 #define So 784 #define Ra 880 #define Si 988 #define DO 1047 #define RE 1175 #define MI 1319 #define FA 1397 #define X1 10 #define x1 13 #define X2 20 #define x2 25 #define X3 30 #define x3 38 #define X4 40 #define x4 50 #define X6 60 #define x6 75 #define X8 80 #define x8 100 #define X16 160 #define x16 200 task play_music() { while (true) { PlayTone(Fa,X2); Wait(x2); PlayTone(Ra,X2); Wait(x2); PlayTone(DO,X2); Wait(x2); PlayTone(RE,X6); Wait(x6); PlayTone(RE,X2); Wait(x2); PlayTone(MI,X2); Wait(x2); PlayTone(RE,X2); Wait(x2); PlayTone(MI,X2); Wait(x2); PlayTone(FA,X8); Wait(x8); } } task main() { start play_music; }