ECE595 - Lab 3 Part 2 - Joystick Program - TA

Revision:
4:da0f4a1c6416
Parent:
3:b7e534ebe607
--- a/main.cpp	Sat Jan 16 00:19:21 2021 +0000
+++ b/main.cpp	Sat Jan 16 00:30:23 2021 +0000
@@ -1,12 +1,25 @@
 #include "mbed.h"
-DigitalOut vibMotor(D6);
+AnalogIn xAxis(A0);
+AnalogIn yAxis(A1);
+
+int x,y,button; // global variables to hold values
+Ticker joystick; // recurring interrupt to get joystick data
 
-int main (void) { 
-    int timer = 1;
-    while(1) {
-        vibMotor = 1;
-        wait(timer);
-        vibMotor = 0;
-        wait(timer);
+void joystick_Int_Handler() {
+    x = xAxis.read() * 1000; // float (0->1) to int (0-1000)
+    y = yAxis.read() * 1000;
+    if ( (x > 900) || (y > 900) )
+        button = 1;
+    else
+        button = 0;
+    }
+
+int main () { 
+    //init interrupt, call every .2s
+    joystick.attach(joystick_Int_Handler,0.2);
+    
+    // Print out the variables
+    while(1){
+        printf("\rX=%3d, Y=%3d, Button=%d\n",x,y,button);
     }
 }
\ No newline at end of file