/*** get keypad input ****/ void get_keypad_input() { char keypad_key; keypad_enable(); while(training) { keypad_key=getkey(); // get value from keypad wait_keyup(); lcd_init(); set_lcd_addr(0x00); if(keypad_key==1) { // 1 = forward move_forward(); }else if(keypad_key==4) { // 4 = left move_left(); }else if(keypad_key==7) { // 7 = right move_right(); }else if(keypad_key==0) { // 0 = reverse move_reverse(); }else if(keypad_key==6) { // 7 = pause move_pause(); }else if(keypad_key==9) { // break out of training mode training=0; move_stop(); display_movement(&stop_training); break; }else{ display_movement(&error); } ms_delay(50); } }
/*** Playback Robot ***/ void playback_robot(){ while(playback) { int i=0; display_movement(&start_playback); /* pull from array */ for(i=0; i<=array_length; i++) { int direction=array1[i]; if(direction == 1) { move_forward(); }else if(direction == 2) { move_left(); }else if(direction == 3) { move_right(); }else if(direction == 4) { move_reverse(); }else if(direction == 5) { move_pause(); }else if((direction == 0) || (direction == 9)) { playback=0; display_movement(&stop_playback); break; } else { playback=0; break; } if(SW5_down()){ //switch5 stop playback mode playback=0; display_movement(&stop_playback); } } } }
void display_text(void) { //draws logo draw_sprite(buffer, LOGO, 92, 5); //displays whose move it is if(player == 0) draw_sprite(buffer, PL1, 10, 90); else draw_sprite(buffer, PL2, 10, 90); //if valid move displays blue marker if(grid[spotx][spoty] == 'x' || grid[spotx][spoty] == 'o') display_movement(); //displays invalid move else if((mouse_x >= GX_START - 46 && mouse_x <= GX_END) && (mouse_y >= GY_START - 46 && mouse_y <= GY_END)) { if(player == 0) draw_sprite(buffer, INVALID, 29, 110); else draw_sprite(buffer, INVALID, 29, 110); } }
/*** Move pause ***/ void move_pause() { display_movement(&pause); MOTORDDR=0xFF; // enable motor port as output MOTORPORT=0xFF; ms_delay(2000); if(training) { add_to_array(5); // if training mode boolean is 1, add direction to array } move_stop(); // turn off all motor ports }
/*** Move right ***/ void move_right() { int i=0; for(i=0; i!=1; i++) { display_movement(&right); MOTORDDR=0xFF; // enable motor port as output MOTORPORT=0xFB; // turn on motor port(1) ms_delay(850); } if(training) { // if training mode boolean is 1, add direction to array add_to_array(3); } move_stop(); }
/************** MAIN **************/ void main(void) { seg7_disable(); /* Stop Motors on initial load */ MOTORDDR=0xFF; // enable motor port as output MOTORPORT=0xFF; // turn off all motor ports lcd_init(); // initialize lcd set_lcd_addr(0x00); // set starting lcd address /* check training or playback mode */ while(1){ if(SW2_down()){ // switch2 starts training mode playback=0; training=1; display_movement(&start_training); } if(SW3_down()){ // switch3 stops training mode training=0; display_movement(&stop_training); } if(SW4_down()){ // switch4 starts playback mode if(array_length) { training=0; playback=1; } else { display_movement(&array_empty); } } if(SW5_down()){ // switch5 erases array playback=0; array_length=0; // reset array counter memset(array1, 9, sizeof(array1)); // reset array display_movement(&stop_playback); } get_keypad_input(); playback_robot(); } }
/*** Move forward ***/ void move_forward() { int i=0; int sensor_detect=0; for(i=0; i!=8; i++) { // loop 8 times and detect objects every 250 ms display_movement(&forward); /* Detect any objects in the way */ sensor_detect = get_sensor_data(); while(sensor_detect){ MOTORPORT=0xFF; //stop execution of queue ms_delay(250); sensor_detect = get_sensor_data(); } MOTORDDR=0xFF; // enable motor port as output MOTORPORT=0xFA; // turn on motor port(0) ms_delay(250); } if(training) { // if training mode boolean is 1, add direction to array add_to_array(1); } move_stop(); }
/*** Move stop ***/ void move_stop() { display_movement(&stop); MOTORDDR=0xFF; // enable motor port as output MOTORPORT=0xFF; // turn off all motor ports }