int main() { // Initialize the encoders and specify the four input pins. encoders_init(IO_C2, IO_C3, IO_C4, IO_C5); while(1) { // Read the counts for motor 1 and print to LCD. lcd_goto_xy(0,0); print_long(encoders_get_counts_m1()); print(" "); // Read the counts for motor 2 and print to LCD. lcd_goto_xy(4,0); print_long(encoders_get_counts_m2()); print(" "); // Print encoder errors, if there are any. if(encoders_check_error_m1()) { lcd_goto_xy(0,1); print("Error 1"); } if(encoders_check_error_m2()) { lcd_goto_xy(0,1); print("Error 2"); } delay_ms(50); } }
int main() { // Initialize the encoders encoders_init(IO_D2, IO_D3, IO_A3, IO_A2); float count = 0; char forward = TRUE; int motor_speed = 100; //set_motors(100, 255); while(1) { clear(); lcd_goto_xy(0,0); //Calculate the count count = encoders_get_counts_m1() / ENCODER_COUNT_PER_REVOLUTION; // Increment and decrement speed if top or bottom buttons were pressed. unsigned char button = get_single_debounced_button_press(ANY_BUTTON); if ((button & TOP_BUTTON) && motor_speed < 250) // if top button pressed motor_speed = motor_speed + 5; if ((button & BOTTOM_BUTTON) && motor_speed > 80) // if bottom button pressed motor_speed = motor_speed - 5; //If the user is holding the middle button we want to stop the motor. if(button_is_pressed(MIDDLE_BUTTON) & MIDDLE_BUTTON) { print("Paused "); set_motors(0,0); } else { // Set motor speed dependent on direction. if(forward) { set_motors(motor_speed, 0); print("Forward "); } else { set_motors(-motor_speed, 0); print("Reverse "); } // At 0 and 2 we need to switch directions. if(count >= 2 && forward) forward = FALSE; if(count <= 0 && !forward) forward = TRUE; } // Print the motor speed and revolutions print_long(motor_speed); print_count(count); //Delay so LCD doesn't flicker too much delay_ms(50); } }