State machine

Dependencies:   mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter

Revision:
7:e7f808875bc4
Parent:
6:bfc6e68774f5
Child:
8:9090ab7c19a8
--- a/main.cpp	Tue Oct 30 12:19:51 2018 +0000
+++ b/main.cpp	Wed Oct 31 05:58:51 2018 +0000
@@ -11,6 +11,9 @@
 // Main loop wait time per cycle. This does not influence the motor PID or EMG reading frequencies.
 const double main_loop_wait_time = 0.01;
 
+// Time between two button polls. Used to debounce the button presses.
+const double button_poll_interval = 0.1;
+
 
 States current_state;   // Defining the state we are currently in
 States last_state;      // To detect state changes.
@@ -18,9 +21,11 @@
 
 // Order of buttons: up_down, left_right, panic
 // D2, D3, D8
-Button ud_button(PTA4);
+Button ud_button(D2);
 Button lr_button(D3);
-Button p_button(PTC6);
+Button p_button(D8);
+
+Ticker button_ticker;
 
 DigitalOut led_red(LED_RED);
 DigitalOut led_green(LED_GREEN);
@@ -156,6 +161,17 @@
     }
 }
 
+void poll_buttons() {
+    // We need to poll the pins periodically.
+    // Normally one would use rise and fall interrupts, so this wouldn't be
+    // needed. But the buttons we use generate so much chatter that
+    // sometimes a rising or a falling edge doesn't get registered.
+    // With all the confusion that accompanies it.
+    ud_button.poll_pin();
+    lr_button.poll_pin();
+    p_button.poll_pin();
+}
+
 int main()
 {
     led_red = 1;
@@ -165,6 +181,9 @@
     // Pretend we come from the operation state.
     // So that the waiting state knows it just got started.
     last_state = operation;
+    
+    // Start the button polling ticker.
+    button_ticker.attach(&poll_buttons, button_poll_interval);
 
     while (true) {
         main_loop();