int main(void){ //SYSTEMConfigPerformance(10000000); int i; state = forward; float Volts = 0.0; float VoltNew = 0.0; init_timer_1(); enableInterrupts(); init_lcd(); clear_lcd(); initPWM(); char voltage[8]; initADC(); initMotor(); while(1){ while(IFS0bits.AD1IF == 0) { } val = ADC1BUF0; IFS0bits.AD1IF = 0; VoltNew = (float)val*5.0/(1023.0); Volts = VoltNew; sprintf(voltage, "%.2f",Volts); move_cursor_lcd(0,1); print_string_lcd(voltage); switch(state) { case forward: idle1 = 0; OC1RS = 0; OC2RS = 0; if (Volts > 2.5) {; OC3RS = 1023; OC4RS = (1- (Volts - 2.5)/2.5)*1023; } else if (Volts < 2.5) { OC3RS = (1- (2.5 - Volts)/2.5)*1023; OC4RS = 1023; } else { OC3RS = 1023; OC4RS = 1023; } stateNext = back; break; case back: idle1 = 0; OC3RS = 0; OC4RS = 0; if (Volts > 2.5) {; OC1RS = 1023; OC2RS = (1- (Volts - 2.5)/2.5)*1023; } else if (Volts < 2.5) { OC1RS = (1- (2.5 - Volts)/2.5)*1023; OC2RS = 1023; } else { OC1RS = 1023; OC2RS = 1023; } stateNext = forward; break; case idle: OC1RS = 0; OC2RS = 0; OC3RS = 0; OC4RS = 0; idle1 = 1; break; case debounce1: delay_ms(40); break; case debounce2: delay_ms(40); if(idle1 == 1) state = stateNext; else if (idle1 == 0) state = idle; break; } } return 0; }
int main(void) { // open the Character LCD port char_lcd_dev = alt_up_character_lcd_open_dev ("/dev/LCD"); /* Initialize the character display */ alt_up_character_lcd_init(char_lcd_dev); // Initially writes the start time of timer to lcd write_time_to_buffer(top_row, seconds, minutes, hours, am_pm_mode); hex_write_date(month, day, year); // Initialize the switches int * sw_ptr = (int *) SW_BASE; int sw_values; int oldvalue = 0x00000000; // Masks for individual switches int MASK_17 = 0x00020000; int MASK_16 = 0x00010000; int MASK_1 = 0x00000002; int MASK_0 = 0x00000001; int is_fast = 0; //use to tell other function if sped up, 0 = slow, 1 = fast int clk_modify = 0; //if 0, clock isn't being changed, if 1 clock is being changed int alarm_modify = 0; //if 0 alarm isn't being changed, if 1, alarm is being changed // Initialize the Timers init_timer_0(&tenths); // Tracker to see when the time changes int old_tenths = 0; // Initialize the KEY port init_button_pio(); // continually while(1) { // check the state of the context integer updated by various ISR functions // Act accordingly, which means // Update the switch_values sw_values = *(sw_ptr); //check if sw17 is up and if it is, then speed up the timer if((sw_values & MASK_17) == 0x00020000 && oldvalue == 0x00000000){ speed_up(); oldvalue = sw_values & MASK_17; is_fast = 1; } //check if sw17 is down and if it is then slow down the timer else if ((sw_values & MASK_17) == 0x00000000 && oldvalue == 0x00020000) { slow_down(); oldvalue = sw_values & MASK_17; is_fast = 0; } // Allow user to change the time if SW0 is up if((sw_values & MASK_0) == 0x00000001){ clk_modify = 1; } else{ clk_modify = 0; } // Buttons increment the hours, minutes, and seconds, respectively to Key3, Key2, and Key1 if(clk_modify == 1 && alarm_modify == 0 && alarm == 0){ // Handle if a key was pressed if (edge_capture) { handle_key_press_time(); } } // Allow user to change the alarm if SW1 is up if((sw_values & MASK_1) == 0x00000002){ alarm_modify = 1; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); alt_up_character_lcd_string(char_lcd_dev, bot_row); } else{ alarm_modify = 0; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); alt_up_character_lcd_string(char_lcd_dev, " "); } // Buttons increment the hours, minutes, and seconds, respectively to Key3, Key2, and Key1 if(alarm_modify == 1 && clk_modify == 0 && alarm == 0){ // Handle if a key was pressed if (edge_capture) { handle_key_press_alarm_set(); } } // Check if alarm should go off yet if(hours == alarm_hours && minutes == alarm_minutes && seconds == 0){ alarm = 1; init_timer_1(&half_second); } // While alarm is going off if( alarm == 1 ){ if (half_second % 2) { // Turn hex on hex_on(); } else { // Turn hex off hex_off(); } if( edge_capture) { handle_key_press_alarm(); } } else { stop_timer_1(); } // Check SW16 for "AM_PM" enable or "24" mode enable // If the switch is enabled, then we turn on 24 hour mode // Else we turn on AM / PM Mode // TODO: Optimize so that it doesn't assign something every loop cycle. Maybe we could slim it down if((sw_values & MASK_16) == MASK_16 ) { am_pm_mode = 0; } else { am_pm_mode = 1; } // Update the clock if (tenths != old_tenths) { // Call the util.h function to update the time update_time(top_row, &old_tenths, &tenths, &seconds, &minutes, &hours, &day, &month, &year, am_pm_mode, 0); // Write the updated time to the display alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 0); alt_up_character_lcd_string(char_lcd_dev, top_row); } } return 0; }
int main(void){ //SYSTEMConfigPerformance(10000000); int i = 0; int j = 0; int onLine = 0; float Volts = 0.0; init_timer_1(); enableInterrupts(); init_lcd(); clear_lcd(); initPWM(); char voltage[8]; initSW1(); initADC(); initMotor(); move_cursor_lcd(0,1); print_string_lcd("lLLLLL"); PrintValue(); lineL = valL + 50;//780; lineR = valR + 50;//805; lineM = valM - 60;//840; while(1){ switch(state) { case Forward: OC1RS = 0; OC2RS = 0; OC3RS = 200; OC4RS = 200; onLine = 0; PrintValue(); if(valR > lineR && valL > lineL && valM > lineM) { state = Double; } else if (valL > lineL) { onLine = 0; state = TurnLeft; if (valM > lineM + 50/*&& counter == 0*/) state = Forward; } else if (valR > lineR) { onLine = 0; state = TurnRight; if (valM > lineM /*&& counter == 0*/) state = Forward; //else state = TurnRight; } else onLine = 0; sprintf(stateDisp, "For"); break; //All sensors are over a line case Double: OC1RS = 0; OC2RS = 0; OC3RS = 200; OC4RS = 200; if (onLine == 0) { counter += 1; onLine = 1; } sprintf(stateDisp,"Doub"); PrintValue(); // delay_ms(100); //if(!(valR > (lineR + Thresh) && valL > (lineL + Thresh) && valM > (lineM + Thresh))) state = Forward; if (counter >= 1) state = TurnAround; //if (counter > 5) state = TurnLeft; if (counter == 12) state = Idle; break; case TurnAround: sprintf(stateDisp,"Tur"); OC1RS = 100; OC2RS = 0; OC3RS = 100; OC4RS = 0; onLine = 0; PrintValue(); stateNext = TurnAround; delay_ms(1); if (valR > lineR /*&& valR < lineR*/ && i == 1) { j = 1; } if(valR < lineR) i = 1; if (j == 1 && valM > lineM + Thresh) { stateNext = Forward; i = 0; j = 0; } //else if(valR < lineR) state = Forward; state = stateNext; break; case TurnLeft: OC1RS = 0; OC2RS = 0; OC3RS = 0; OC4RS = 300; onLine = 0; sprintf(stateDisp, "Lef"); PrintValue(); if(valL < lineL) state = Forward; //else if (valR > lineR && valM > lineM) state = Double; break; case TurnRight: OC1RS = 0; OC2RS = 0; OC3RS = 300; OC4RS = 0; onLine = 0; sprintf(stateDisp, "Rig"); PrintValue(); if(valR < lineR) state = Forward; //if(valL > lineL) state = TurnLeft if(valL > lineL) state = TurnLeft; if (valR > (lineR + Thresh) && valL > (lineL + Thresh) && valM > (lineM + Thresh)) state = Double; //else if(counter < 1) state = Forward; //else state = TurnRight; break; case Idle: idle1 = 1; OC1RS = 0; OC2RS = 0; OC3RS = 0; OC4RS = 0; sprintf(stateDisp, "Idl"); PrintValue(); break; } } return 0; }
int main(void){ int j = 0; int onLine = 0; init_timer_1(); enableInterrupts(); init_lcd(); clear_lcd(); initPWM(); initADC(); initMotor(); move_cursor_lcd(0,1); print_string_lcd("XXXX"); //Though the reason is unclear, the display does not function properly without some initial string being printed on it PrintValue(); lineL = valL + lThresh; lineR = valR + rThresh; lineM = valM - mThresh; while(1){ switch(state) { //Default state. Proceed forward, while ready to change into any other state case Forward: OC1RS = 0; OC2RS = 0; OC3RS = fSpeed; OC4RS = fSpeed; onLine = 0; PrintValue(); if(valR > lineR + Thresh && valL > lineL + Thresh && valM > lineM + Thresh) { stateNext = Triple; state = Delay; } else { if (valL > lineL + Thresh && valM > lineM + Thresh) { Full = 1; i = 0; stateNext = SharpLeft; state = Delay; } else if (valR > lineR + Thresh && valM > lineM + Thresh) { Full = 1; i = 0; stateNext = SharpRight; state = Delay; } else if (valR > lineR) { state = TurnRight; } else if (valL > lineL) { state = TurnLeft; } } sprintf(stateDisp, "For"); break; //All sensors are over a line case Triple: OC1RS = 0; OC2RS = 0; OC3RS = fSpeed; OC4RS = fSpeed; if (onLine == 0) { counter += 1; onLine = 1; } sprintf(stateDisp,"Trip"); PrintValue(); if (!(valR > lineR + Thresh && valL > lineL + Thresh && valM > lineM + Thresh)) { if (counter < 3 && counter > 1) { i = 0; Full = 1; stateNext = SharpRight; state = Delay; } else if (counter == 3) state = TurnAround; else if (counter > 3) state = Idle; } break; //Make 180 degree turn case TurnAround: sprintf(stateDisp,"Tur"); OC1RS = tSpeed; OC2RS = 0; OC3RS = tSpeed; OC4RS = 0; onLine = 0; PrintValue(); delay_ms(1); if (valR > lineR /*&& valR < lineR*/ && i == 1) { j = 1; } if(valR < lineR) i = 1; if (j == 1 && valM > lineM + Thresh) { i = 0; j = 0; state = Forward; } break; //Turn left until the left sensor no longer detects the line case TurnLeft: OC1RS = 0; OC2RS = 0; OC3RS = 0; OC4RS = tSpeed; onLine = 0; sprintf(stateDisp, "Lef"); PrintValue(); if(valL < lineL) state = Forward; else if (valR > (lineR + Thresh) && valL > (lineL + Thresh) && valM > (lineM + Thresh)) { state = Triple; } else if (valL > lineL + Thresh && valM > lineM + Thresh) { i = 0; Full = 1; stateNext = SharpLeft; state = Delay; } break; //Turn right until the right sensor no longer detects the line case TurnRight: OC1RS = 0; OC2RS = 0; OC3RS = tSpeed; OC4RS = 0; onLine = 0; sprintf(stateDisp, "Rig"); PrintValue(); if(valR < lineR) state = Forward; else if (valR > (lineR + Thresh) && valL > (lineL + Thresh) && valM > (lineM + Thresh)) { state = Triple; } else if (valR > lineR + Thresh && valM > lineM + Thresh) { i = 0; Full = 1; stateNext = SharpRight; state = Delay; } break; //Turn right until both the right and middle sensors have passed over a new line case SharpRight: OC1RS = 0; OC2RS = 0; OC3RS = tSpeed / 2; OC4RS = 0; onLine = 0; sprintf(stateDisp, "sRi"); PrintValue(); if (valM > lineM) state = Forward; if (valR > lineR && i == 1) { j = 1; } if(valR < lineR) i = 1; if (j == 1 && valM > lineM + Thresh) { i = 0; j = 0; state = Forward; } break; //Turn left until both the left and middle sensors have passed over a new line case SharpLeft: OC1RS = 0; OC2RS = 0; OC3RS = 0; OC4RS = tSpeed; onLine = 0; sprintf(stateDisp, "sLe"); PrintValue(); if (valM > lineM) state = Forward; if (valL > lineL && i == 1) { j = 1; } if(valR < lineR) i = 1; if (j == 1 && valM > lineM + Thresh) { i = 0; j = 0; state = Forward; } break; //Halt robot. Primarily for debugging case Idle: idle1 = 1; OC1RS = 0; OC2RS = 0; OC3RS = fSpeed; OC4RS = fSpeed; sprintf(stateDisp, "Idl"); PrintValue(); break; //Continue forward until the robot has passed a 90 degree turn or perpendicular line, then take the appropriate action case Delay: OC1RS = 0; OC2RS = 0; OC3RS = fSpeed; OC4RS = fSpeed; PrintValue(); sprintf(stateDisp, "Del"); if (!(valR > lineR + Thresh || valL > lineL + Thresh)) state = stateNext; break; } } return 0; }