static void button_readState() { /* middle button - pause */ int button_press; int button_release; button_press = get_single_debounced_button_press(ANY_BUTTON); button_release = get_single_debounced_button_release(ANY_BUTTON); switch (g_middle_button.state) { case BUTTON_STATE_RELEASED: if (button_press & MIDDLE_BUTTON) { g_middle_button.state = BUTTON_STATE_PRESSED; if (g_motor_state.enabled) { g_motor_state.enabled = false; g_motor_state.direction = DIRECTION_FORWARD; play_note(A(5), 200, 12); } else { g_motor_state.enabled = true; play_note(A(5), 200, 12); } } break; case BUTTON_STATE_PRESSED: if (button_release & MIDDLE_BUTTON) { g_middle_button.state = BUTTON_STATE_RELEASED; } break; } /* bottom button - speed down */ switch (g_bottom_button.state) { case BUTTON_STATE_RELEASED: if (button_press & BOTTOM_BUTTON) { g_bottom_button.state = BUTTON_STATE_PRESSED; g_motor_state.speed = MIN(g_motor_state.speed + 5, 255); } break; case BUTTON_STATE_PRESSED: if (button_release & BOTTOM_BUTTON) { g_bottom_button.state = BUTTON_STATE_RELEASED; } break; } /* top button - speed up */ switch (g_top_button.state) { case BUTTON_STATE_RELEASED: if (button_press & TOP_BUTTON) { g_top_button.state = BUTTON_STATE_PRESSED; g_motor_state.speed = MAX(g_motor_state.speed - 5, 0); } break; case BUTTON_STATE_PRESSED: if (button_release & TOP_BUTTON) { g_top_button.state = BUTTON_STATE_RELEASED; } break; } }
int main() { lcd_init_printf(); clear(); // clear the LCD printf("Time: "); while (1) { unsigned char button = get_single_debounced_button_press(ANY_BUTTON); switch (button) { case BUTTON_A: play_note(A(4), 50, 10); break; case BUTTON_B: play_note(B(4), 50, 10); break; case BUTTON_C: play_note(C(5), 50, 10); } button = get_single_debounced_button_release(ANY_BUTTON); switch (button) { case BUTTON_A: play_note(A(5), 50, 10); break; case BUTTON_B: play_note(B(5), 50, 10); break; case BUTTON_C: play_note(C(6), 50, 10); } unsigned long ms = get_ms(); // get elapsed milliseconds // convert to the current time in minutes, seconds, and hundredths of seconds unsigned char centiseconds = (ms / 10) % 100; unsigned char seconds = (ms / 1000) % 60; unsigned char minutes = (ms / 60000) % 60; lcd_goto_xy(0, 1); // go to the start of the second LCD row // print as [m]m:ss.cc (m = minute, s = second, c = hundredth of second) printf("%2u:%02u.%02u", minutes, seconds, centiseconds); } }