First Draft, serial print change based on distance

Revision:
5:98845ccaaacd
Parent:
4:77500a7f951d
Child:
6:18a4dd77057e
--- a/main.cpp	Sat Jan 22 15:28:18 2022 +0000
+++ b/main.cpp	Sat Jan 22 20:06:35 2022 +0000
@@ -1,43 +1,126 @@
 #include "mbed.h"
 #include "ultrasonic.h"
 #include "N5110.h"
-// added R and L buttons presses in so that the user has to press when they are ready to rotate object and in which direction
-DigitalIn L(PTB18);
-DigitalIn R(PTB3);
-
-// LEDs on PCB to be connected (1 to 6) - active-low 0 = on and 1 = off
-BusOut output(PTA1,PTA2,PTC2,PTC3,PTC4,PTD3);
-
-// VCC,SCE,RST,D/C,MOSI,SCLK,LED
-N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // K64F - pwr from 3V3
+#include "Joystick.h"
+#include "main.h"
 
-// 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
-int fsm[6] = {0b111110, 0b111101, 0b111011, 0b110111, 0b101111, 0b011111};
-
-void dist(int distance);
-
-
-ultrasonic mu(PTD0, PTC12, .5, 1, &dist);    //Set the trigger pin to PTD0 and the echo pin to PTC12
-                                        //have updates every .5 seconds and a timeout after 1
-                                        //second, and call dist when the distance changes
 
 int main()
 {
-    // set inital state 
-    int state = 0;
+
     R.mode(PullDown);
     L.mode(PullDown);
+    Start.mode(PullDown);
+    Start.rise(Start_isr);
+    Back.mode(PullDown);
+    Back.rise(Back_isr);
+    
+    joystick.init();
+    init_display();
+    mu.startUpdates();//start mesuring the distance
+    main_menu();
+    
+    while(1)
+    {
+    
+        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
+                                //the class checks if dist needs to be called.
+    }
+}
+
+void init_display(){
+        
     lcd.init();
     lcd.setContrast(0.4);
-    mu.startUpdates();//start mesuring the distance
+    lcd.clear(); /// clear screen (init)  
+        
+    }
+        
+void dist(int distance){
+
+    if (distance > 250){
+    printf("Safe Distance %dmm\r\n", distance);
+    }
+    }
+    
+void main_menu(){
+    
+    lcd.clear();
+    Start_flag = 0;
+    int select = 0;
+    output = 63; //set all LEDs to off
+
+    while (1) {
+        
+    Direction d = joystick.get_direction();   
+    printf("Direction = %i\n",d);
+    
+        switch(select) {
+            case 0:
+                switch(d) {
+                    case 1:
+                        select = 1;
+                    printf("UP");
+                        break;
+                    case 5:
+                        select = 1;
+                    printf("Down");
+                        break;
+}
+                break;
+            case 1:
+                switch(d) {
+                    case 1:
+                        select = 0;
+                    printf("UP");
+                        break;
+                    case 5:
+                        select = 0;
+                    printf("Down");
+                        break;
+}
+                break;
+ 
+        }
+                wait(0.1);
+        if (select == 0) {
+            lcd.clear();
+            lcd.printString("Detect",27,0);
+            lcd.printString("Sense Object",7,2);
+            lcd.printString("Calibration",7,3);
+            lcd.drawCircle(3,19,2,FILL_TRANSPARENT);
+            lcd.refresh();
+            wait(0.15);
+            if (Start_flag == 1) {
+                Start_flag = 0;
+                sense_object();
+            }
+        } else if (select == 1) {
+            lcd.clear();
+            lcd.printString("Detect",27,0);
+            lcd.printString("Sense Object", 7,2);
+            lcd.printString("Calibration",7,3);
+            lcd.drawCircle(3,27,2,FILL_TRANSPARENT);
+            lcd.refresh();
+            wait(0.15);
+            if (Start_flag == 1) {
+                Start_flag = 0;
+            }
+
+        }
+    }
+}
+
+void sense_object(){
+        
+        
+    // set inital state 
+    int state = 0;
+    
     while(1)
     {
     output = fsm[state];  // output current state
-    lcd.clear(); // clear buffer at start of every loop
-// can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display)
-    
-// check which state we are in and see which the next state should be
+        // check which state we are in and see which the next state should be
     switch(state) {
         case 0:
         lcd.clear();
@@ -116,16 +199,17 @@
                 // or could jump to starting state i.e. state = 0
                 break;  
         }
-        ThisThread::sleep_for(100);
+        ThisThread::sleep_for(200);
+        }
+        }
         
-        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
-                                //the class checks if dist needs to be called.
+void Start_isr()
+    {
+    Start_flag = 1;
     }
-}
-    
-void dist(int distance){
 
-    if (distance > 250){
-    printf("Safe Distance %dmm\r\n", distance);
+void Back_isr()
+    {
+    Back_flag = 1;
     }
-    }
\ No newline at end of file
+    
\ No newline at end of file