/* code for a walking robot Motor A is walk forward/backward Sensor A is on when Legs up Motor C turns the robot left/right Sensor C detects collisions, and triggers song Run motor B left/right randomly Sensor B is light - randomly blink it */ #include #include #include #include #include #include #include // define to use light #define LIGHTSENS SENSOR_2 void PutText(void) { // display a random text char * texts[] = { "Tests","Chris","12345","Eatit","Spank", 0}; int count = 0; while (texts[count] != 0) count++; if (count > 0) cputs(texts[random()%count]); } // PutText void PlaySong(void) { static const note_t song[] = { { PITCH_C4, 1 } , { PITCH_D4, 1 } , { PITCH_E4, 1 }, { PITCH_F4, 1 } , { PITCH_G4, 1 } , { PITCH_A4, 1 }, { PITCH_B4, 1 } , { PITCH_C5, 1 } , { PITCH_B4, 1 }, { PITCH_A4, 1 } , { PITCH_G4, 1 } , { PITCH_F4, 1 }, { PITCH_E4, 1 } , { PITCH_D4, 1 } , { PITCH_C4, 3 }, { PITCH_END, 0 } }; dsound_play(song); } // PlaySong wakeup_t press_A(wakeup_t data) { return TOUCH_1; } void Turn(int dir, int time) { // backup till sensor A hit, then motor C left(dir=0) or right(dir=1) time msecs motor_a_dir(off); msleep(100); motor_a_speed(MAX_SPEED); motor_a_dir(rev); wait_event(press_A,0); motor_a_dir(brake); motor_c_speed(MAX_SPEED); if (dir == 0) motor_c_dir(fwd); else motor_c_dir(rev); msleep(time); motor_c_dir(brake); msleep(100); motor_c_dir(off); motor_a_dir(off); // to avoid power drain } // Turn int main(int argc, char *argv[]) { int light = 0; // light off int b_dir = 0; // to toggle directions srandom(1234); // randomize the system // start walking, and motor B motor_a_speed(MAX_SPEED); motor_a_dir(fwd); motor_b_speed(MAX_SPEED); motor_b_dir(fwd); while (1) { // start it up if (TOUCH_3) { cputs("Turn"); PlaySong(); motor_a_speed(MAX_SPEED); motor_a_dir(rev); msleep(1000); Turn(random()&1,2000); // walk again motor_a_speed(MAX_SPEED); motor_a_dir(fwd); } // random action if ((random()%2000) < 30) { // reverse B if (b_dir == 0) { b_dir = 1; motor_b_dir(rev); } else { b_dir = 0; motor_b_dir(fwd); } } if ((random()%2000) < 30) { // reverse light, draw text PutText(); if (light == 0) { light = 1; ds_passive(&LIGHTSENS); } else { light = 0; ds_active(&LIGHTSENS); } } } // infinite loop return 0; } // main // end - WalkerI.c