Coursework

Revision:
12:ceea378d5691
Parent:
11:1dec05b7d1c1
Child:
13:034ffa96643d
Child:
15:234321e80678
diff -r 1dec05b7d1c1 -r ceea378d5691 main.cpp
--- a/main.cpp	Fri Jan 07 19:51:48 2022 +0000
+++ b/main.cpp	Sat Jan 08 17:35:22 2022 +0000
@@ -4,6 +4,18 @@
 #include "hcsr04.h"
 #include "Piezo.h"
 #include "string"
+// FSM Menu 
+// defines directions as 0/1. Note UPPERCASE
+#define UP 0
+#define DOWN 1
+// set inital state
+  volatile  int state = 0;
+    // set initial direction
+    int direction = UP;
+// array of states in the FSM, each element is the output of the counter
+// set the output in binary to make it easier, 0 is LED on, 1 is LED off
+string fsm[4] = {"buttonA_isr","buttonB_isr","buttonAsss_isr","buttonA_isr"};
+volatile int Press;
 
 // y  x  button
 Joystick joystick(PTB10,PTB11,PTC16);
@@ -103,7 +115,7 @@
 //first need to initialise display
        lcd.init();    
 
-// Button A is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
+    // Button A is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
     buttonA.mode(PullDown);
     // Button B is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
     buttonB.mode(PullDown);
@@ -171,6 +183,18 @@
 //bool button = buttonA.read();
 //printf("%f\n", g_buttonA_flag );
 //Recording button presses
+
+            // swap direction when button has been pressed
+            // (could just use ! but want this to be explicit to aid understanding)
+            if (direction == UP) {
+                direction = DOWN;    
+            }
+            else {
+                direction = UP;
+string output;
+        output = fsm[state];  
+
+
 int result;
  result = cube(5);
     printf("Button A pressed  %f\n", cube(5));
@@ -191,6 +215,14 @@
             }
 if (g_buttonX_flag){
                    g_buttonX_flag = 0;  // if it has, clear the flag
+                               // swap direction when button has been pressed
+            // (could just use ! but want this to be explicit to aid understanding)
+            if (direction == UP) {
+                direction = DOWN;    
+            }
+            else {
+                direction = UP;
+            }
 
             // send message over serial port - can observe in CoolTerm etc.
             printf("Button X pressed\n");
@@ -264,21 +296,24 @@
     
     }  
  }
- 
- 
-  //Button A event-triggered interrupt
+ }
+   //Button A event-triggered interrupt
 void buttonA_isr()
 {
-    g_buttonA_flag = 1;  
-      //  printf("flag is now %f\n", g_buttonA_flag); 
-        lcd.clear(); 
+    
+    Press++;
+    
+    g_buttonA_flag = 1;   // set flag in ISR
+  
+        printf("press count = %i\n",Press);
+            
+      // lcd.clear(); 
         lcd.printString("Button A Pressed",0,0);
+        
          lcd.refresh(); // set flag in ISR
-       
-        };
-    
+}
+ 
 
-//}
   //Button B event-triggered interrupt
 void buttonB_isr()
 {