Level 2 Project Range Device

Dependencies:   N5110 SDFileSystem SRF02 TMP102 mbed

Fork of Ranger by Philip Thompson

Files at this revision

API Documentation at this revision

Comitter:
el15pjt
Date:
Wed May 04 22:16:06 2016 +0000
Parent:
9:bc259fc22fa2
Child:
11:b64d123b9f4f
Commit message:
LEDs control revise and buzzer revised.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed May 04 14:05:07 2016 +0000
+++ b/main.cpp	Wed May 04 22:16:06 2016 +0000
@@ -11,30 +11,16 @@
 int main()
 {
     int d = 0;
-    int myarray[5] {9,9,9,9,9}; /// initilised to any possitive integer to prevent random values at start up triggering an alert
+    int myarray[5] {999,999,999,999,999}; /// initilised to any possitive integer to prevent random values at start up triggering an alert
     setup();
     lcd.init(); // Initiate LCD
-
-    sprintf(buffer,"ELEC 2645");
-    sprintf(buffer1,"Project Ranger");
-    sprintf(buffer2,"Phil Thompson");
-    sprintf(buffer3,"SID 200971914 ");
-    lcd.printString(buffer,0,0);
-    lcd.printString(buffer1,0,1);
-    lcd.printString(buffer2,0,2);
-    lcd.printString(buffer3,0,3);
+    lcd.printString("ELEC 2645",0,0);
+    lcd.printString("Project Ranger",0,1);
+    lcd.printString("Phil Thompson",0,2);
+    lcd.printString("SID 200971914 ",0,3);
     wait (3);
-
-    serial.baud(115200);  // full-speed!
-    serial.printf("#### SD TESTING #####\n");
-    FILE *fp; // this is our file pointer
-    wait(1);
-
-
-
-/////////////////////// Reading from file example ////////////////////////
-
-    // now open file for reading...note the 'r'
+    FILE *fp; // file pointer
+    // Load settings
     fp = fopen("/sd/settings.txt", "r");
     if (fp == NULL) {  // if it can't open the file then print error message
         serial.printf("Error! Unable to open file!\n");
@@ -48,9 +34,7 @@
         fclose(fp);  // ensure you close the file after reading
     }
     lcd.setBrightness(bright);
-    wait (3);
     while(1) {
-
         if (g_sw1_flag) { /// Event triggered inturupt to call the menu and reset page numbers
             g_sw1_flag = 0;
             page = 0;
@@ -58,7 +42,6 @@
             lcd.clear();
             menu();
         }
-
         if (g_timer_flag_srf02) {
             g_timer_flag_srf02 = 0;  // if it has, clear the flag
             if (units == 1) {
@@ -75,27 +58,37 @@
         } else {
             d++;
         }
-
         myarray[d] = distance;
-        // serial.printf("ARRAY ELEMENT = %i", d);
-        // serial.printf("myarray %i", myarray[d]);
         totaldistance =0;
-        for (int i = 0; i < 6; i++) {
+        for (int i = 0; i < 5; i++) {
             totaldistance = totaldistance + myarray[i];
         }
         avgdistance = (totaldistance/5);
 
-        //   serial.printf("TOATAL DISTANCE %i\n", totaldistance);
+        // serial.printf("TOATAL DISTANCE %i\n", totaldistance);
         // serial.printf("DISTANCE %i\n", distance);
-        //   serial.printf("AVERAGE DISTANCE %f\n", avgdistance);
+        // serial.printf("AVERAGE DISTANCE %f\n", avgdistance);
+        // serial.printf("ARRAY ELEMENT = %i\n", d);
+        // serial.printf("myarray %i\n", myarray[d]);
+
+        /* uncomment to use button 2 to toggle thru alert levels for debugging and comment out setalert();
+                if (g_sw2_flag) { /// Event triggered inturupt to call the menu and reset page numbers
+                    g_sw2_flag = 0;
+                    if (alert ==8) {
+                        alert =0;
+                    } else {
+                        alert++;
+                    }
+                    serial.printf("ALERT LEVEL %i\n", alert);
+                }*/
         setalert();
-        //  serial.printf("ALERT LEVEL %i\n", alert);
+        //serial.printf("ALERT LEVEL %i\n", alert);
         lcdoutput();
-        //     if (alert !=0) {
-        //       if (buzz_flag == 0) {
-        //         setbuzzer();
-        //   }
-        // }
+        if (alert !=0) {
+            if (buzz_flag == 0) {
+                setbuzzer();
+            }
+        }
         setleds();
         sleep();
     }
@@ -124,17 +117,14 @@
         switch (standby) {
             case 0:
                 if (check_flag == 1) {
-                    sprintf(buffer5,"COLLISIONCHECK");
+                    lcd.printString("COLLISIONCHECK",0,5);
                 }
-                sprintf(buffer,"**PATH CLEAR**");
-                lcd.printString(buffer,0,5);
-                lcd.printString(buffer,0,0);
+                lcd.printString("**PATH CLEAR**",0,0);
                 lcd.refresh();
                 break;
             case 1:
                 if (check_flag == 1) {
-                    sprintf(buffer5,"COLLISIONCHECK");
-                    lcd.printString(buffer5,0,5);
+                    lcd.printString("COLLISIONCHECK",0,5);
                 }
                 if (units == 0) {
                     T = (Traw*1.8000)+32.00;
@@ -151,8 +141,7 @@
                 break;
             case 2:
                 if (check_flag == 1) {
-                    sprintf(buffer5,"COLLISIONCHECK");
-                    lcd.printString(buffer5,0,5);
+                    lcd.printString("COLLISIONCHECK",0,5);
                 }
                 if (units == 0) {
                     T = (Traw*1.8000)+32.00;
@@ -169,9 +158,8 @@
                 lcd.setBrightness(0);
                 break;
         } //close switch
-
-
-    } else { //If alert isn't 0 then the distance is to be dispayed alonng with the the distance bar
+    } else {
+        //If alert isn't 0 then the distance is to be dispayed alonng with the the distance bar
         lcd.setBrightness(bright);
         standby = 0;
         lcd.clear();
@@ -180,23 +168,20 @@
         } else {
             sprintf(buffer2,"%0.2f In",avgdistance);
         }
-        sprintf(buffer,"****RANGE!****");
-        sprintf(buffer1,"DISTANCE");
-        sprintf(buffer4,"Menu");
         lcd.printString(buffer2,25,2);
-        lcd.printString(buffer,0,0);
-        lcd.printString(buffer1,16,1);
-        lcd.printString(buffer4,0,5);
+        lcd.printString("****RANGE!****",0,0);
+        lcd.printString("DISTANCE",16,1);
+        lcd.printString("Menu",0,5);
         int h;
-        h = (84/r7*c); ///Maps length of bar to largest set range r7
+        h = (r7*c)/84; ///Maps length of bar to largest set range r7
         distbar = (avgdistance*h)-2;
         //drawRect(int x0,int y0,int width,int height,int fill);
         lcd.drawRect(0,29,distbar,7,1); //
-        serial.printf("H = %i, r7 = %i",h,r7);
-        serial.printf("distbar %f",distbar);
+        //serial.printf("H = %i, r7 = %i",h,r7);
+        //serial.printf("distbar %f",distbar);
         lcd.refresh();
     }
-}
+}//close function
 
 void setalert()
 {
@@ -225,31 +210,23 @@
 
 void setleds()
 {
-    if (alert ==7) {
-        if (g_timer_flag_led) {
-            g_timer_flag_led = 0;  // if it has, clear the flag
-            rr_led = !rr_led;
-            gg_led = !gg_led;
-            a_led = !a_led;
-        }
-    } else if(Alertlevel[alert].fa_led == HIGH) {
-        if (g_timer_flag_led) {
-            g_timer_flag_led = 0;  // if it has, clear the flag
-            a_led = !a_led;
-        }
-    } else if (Alertlevel[alert].frr_led == HIGH) {
-        if (g_timer_flag_led) {
-            g_timer_flag_led = 0;  // if it has, clear the flag
-            rr_led = !rr_led;
-        }
-    } else if(Alertlevel[alert].fgg_led == HIGH) {
-        if (g_timer_flag_led) {
-            g_timer_flag_led = 0;  // if it has, clear the flag
-            gg_led = !gg_led;
-        }
+    if (g_timer_flag_led) {
+        g_timer_flag_led = 0;
+        flash = !flash; // if it has, clear the flag
+    }
+    if(Alertlevel[alert].fa_led == HIGH) {
+        a_led = flash;
+    } else {
+        a_led = Alertlevel[alert].sa_led;
+    }
+    if (Alertlevel[alert].frr_led == HIGH) {
+        rr_led = flash;
     } else {
         rr_led = Alertlevel[alert].srr_led;
-        a_led = Alertlevel[alert].sa_led;
+    }
+    if(Alertlevel[alert].fgg_led == HIGH) {
+        gg_led = flash;
+    } else {
         gg_led = Alertlevel[alert].sgg_led;
     }
 }
@@ -263,7 +240,7 @@
     @param Alertlevel[alert].toneon controls how long the tone will last depending on alert
     */
     buzzer.period (1.0/1000.0);
-    buzzer = 0.2;
+    buzzer = 0.5;
     buzzoff.attach(&flip, Alertlevel[alert].toneon);
 }
 
@@ -332,13 +309,11 @@
                     lcd.clear();
                 }
                 int lightbar = bright*84;
-                sprintf(buffer1,"BACKLIGHT");
                 sprintf(buffer2,"%.0f%%",bright*100);
                 lcd.drawRect(0,26,lightbar,7,1);  // move bar up!!!!!!!!!!!!!!!!
-                lcd.printString(buffer1,0,1);
+                lcd.printString("BACKLIGHT",0,1);
                 lcd.printString(buffer2,0,2);
-                sprintf(buffer4,"NEXT       ADJ");
-                lcd.printString(buffer4,0,5);
+                lcd.printString("NEXT       ADJ",0,5);
                 lcd.refresh();
                 break;
             case 1:
@@ -351,31 +326,30 @@
                         offset += 1;
                     }
                 }
-                sprintf(buffer1,"OFFSET");
                 sprintf(buffer2,"%i",offset);
-                lcd.printString(buffer1,0,1);
+                lcd.printString("OFFSET",0,1);
                 sprintf(buffer4,"NEXT       ADJ");
                 lcd.printString(buffer4,0,5);
                 break;
             case 2:
-                sprintf(buffer1,"UNITS");
-                lcd.printString(buffer1,0,1);
                 if (g_sw2_flag) {
                     g_sw2_flag = 0;  // if it has, clear the flag
                     if (units == 1) {
                         units = 0;
                         c = 0.3937;
-                        sprintf(buffer2,"IMPERIAL");
-                        lcd.clear();
                     } else {
                         units = 1;
-                        sprintf(buffer2,"METRIC");
                         c = 1;
                         lcd.clear();
                     }
                 }
-                sprintf(buffer4,"NEXT       ADJ");
-                lcd.printString(buffer4,0,5);
+                if (units == 0) {
+                    sprintf(buffer2,"IMPERIAL");
+                } else {
+                    sprintf(buffer2,"METRIC");
+                }
+                lcd.printString("NEXT       ADJ",0,5);
+                lcd.printString("UNITS",0,1);
                 break;
             case 3:
                 if (g_sw2_flag) {
@@ -384,14 +358,12 @@
                     lcd.clear();
                 }
                 if (check_flag == 0) {
-                    sprintf(buffer1,"NO");
                     sprintf(buffer2,"COLLISION");
-                    lcd.printString(buffer1,0,1);
+                    lcd.printString("NO",0,1);
                     sprintf(buffer4,"NEXT     ");
                 } else {
-                    sprintf(buffer1,"CLEAR");
                     sprintf(buffer2,"COLLISION");
-                    lcd.printString(buffer1,0,1);
+                    lcd.printString("CLEAR",0,1);
                     sprintf(buffer4,"NEXT     CLEAR");
                 }
 
@@ -411,46 +383,34 @@
                     r6 = 65;/// Upper limit of alert 6
                     r7 = 80;/// Upper limit of alert 7
                     lcd.clear();
-                    sprintf(buffer1,"SETTINGS");
-                    sprintf(buffer2,"RESET");
-                    lcd.printString(buffer1,0,1);
-                    lcd.printString(buffer2,0,2);
+                    lcd.printString("SETTINGS",0,1);
+                    lcd.printString("RESET",0,2);
                     wait(1);
                     return;
                 }
-                lcd.clear();
-                sprintf(buffer1,"RESET");
                 sprintf(buffer2,"SETTINGS");
-                lcd.printString(buffer1,0,1);
-                lcd.printString(buffer2,0,2);
-                sprintf(buffer4,"NEXT      RESET");
-                lcd.printString(buffer4,0,5);
-                lcd.refresh();
+                lcd.printString("RESET",0,1);
+                lcd.printString("NEXT     RESET",0,5);
                 break;
             case 5:
                 if (g_sw2_flag) {
                     g_sw2_flag = 0;  // if it has, clear the flag
                     submenu();
                 }
-                sprintf(buffer1,"RANGE");
                 sprintf(buffer2,"PARAMETERS");
-                lcd.printString(buffer1,0,1);
-                sprintf(buffer4,"EXIT       ADJ");
-                lcd.printString(buffer4,0,5);
+                lcd.printString("RANGE",0,1);
+                lcd.printString("EXIT       ADJ",0,5);
                 break;
             default:
                 lcd.clear();
                 save ();
-                sprintf(buffer3,"    SAVING    ");
-                lcd.printString(buffer3,0,2);
-                sprintf(buffer4,"   SETTINGS   ");
-                lcd.printString(buffer4,0,3);
+                lcd.printString("    SAVING    ",0,2);
+                lcd.printString("   SETTINGS   ",0,3);
                 wait (1);
                 return;
         }// switch bracket
-        sprintf(buffer,"*****MENU*****");
         lcd.printString(buffer2,0,2);
-        lcd.printString(buffer,0,0);
+        lcd.printString("*****MENU*****",0,0);
         lcd.refresh();
     }//while braket
 }//functon bracket
@@ -488,16 +448,12 @@
                         r2 = r2+1;
                     }
                 }
-                sprintf(buffer1,"*****MENU*****");
-                sprintf(buffer2,"RANGE");
-                sprintf(buffer3,"PARAMETERS");
                 sprintf(buffer4,"1Cm to %iCm",r2);
-                sprintf(buffer,"NEXT       ADJ");
-                lcd.printString(buffer1,0,0);
-                lcd.printString(buffer2,0,1);
-                lcd.printString(buffer3,0,2);
+                lcd.printString("*****MENU*****",0,0);
+                lcd.printString("RANGE",0,1);
+                lcd.printString("PARAMETERS",0,2);
                 lcd.printString(buffer4,0,3);
-                lcd.printString(buffer,0,5);
+                lcd.printString("NEXT       ADJ",0,5);
                 break;
             case 1:
                 if (g_sw2_flag) {
@@ -508,16 +464,12 @@
                         r3 += 1;
                     }
                 }
-                sprintf(buffer1,"*****MENU*****");
-                sprintf(buffer2,"RANGE");
-                sprintf(buffer3,"PARAMETERS");
                 sprintf(buffer4,"%iCm to %iCm",r2,r3);
-                sprintf(buffer,"NEXT       ADJ");
-                lcd.printString(buffer1,0,0);
-                lcd.printString(buffer2,0,1);
-                lcd.printString(buffer3,0,2);
+                lcd.printString("*****MENU*****",0,0);
+                lcd.printString("RANGE",0,1);
+                lcd.printString("PARAMETERS",0,2);
                 lcd.printString(buffer4,0,3);
-                lcd.printString(buffer,0,5);
+                lcd.printString("NEXT       ADJ",0,5);
                 break;
             case 2:
                 if (g_sw2_flag) {
@@ -528,16 +480,12 @@
                         r4 += 1;
                     }
                 }
-                sprintf(buffer1,"*****MENU*****");
-                sprintf(buffer2,"RANGE");
-                sprintf(buffer3,"PARAMETERS");
                 sprintf(buffer4,"%iCm to %iCm",r3,r4);
-                sprintf(buffer,"NEXT       ADJ");
-                lcd.printString(buffer1,0,0);
-                lcd.printString(buffer2,0,1);
-                lcd.printString(buffer3,0,2);
+                lcd.printString("*****MENU*****",0,0);
+                lcd.printString("RANGE",0,1);
+                lcd.printString("PARAMETERS",0,2);
                 lcd.printString(buffer4,0,3);
-                lcd.printString(buffer,0,5);
+                lcd.printString("NEXT       ADJ",0,5);
                 break;
             case 3:
                 if (g_sw2_flag) {
@@ -548,16 +496,12 @@
                         r5 += 1;
                     }
                 }
-                sprintf(buffer1,"*****MENU*****");
-                sprintf(buffer2,"RANGE");
-                sprintf(buffer3,"PARAMETERS");
-                sprintf(buffer4,"%iCm to %iCm",r4,r5);
-                sprintf(buffer,"NEXT       ADJ");
-                lcd.printString(buffer1,0,0);
-                lcd.printString(buffer2,0,1);
-                lcd.printString(buffer3,0,2);
+                sprintf(buffer4,"%iCm to %iCm",r4,r5);  
+                lcd.printString("*****MENU*****",0,0);
+                lcd.printString("RANGE",0,1);
+                lcd.printString("PARAMETERS",0,2);
                 lcd.printString(buffer4,0,3);
-                lcd.printString(buffer,0,5);
+                lcd.printString("NEXT       ADJ",0,5);
                 break;
             case 4:
                 if (g_sw2_flag) {
@@ -568,16 +512,12 @@
                         r6 += 1;
                     }
                 }
-                sprintf(buffer1,"*****MENU*****");
-                sprintf(buffer2,"RANGE");
-                sprintf(buffer3,"PARAMETERS");
                 sprintf(buffer4,"%iCm to %iCm",r5,r6);
-                sprintf(buffer,"NEXT       ADJ");
-                lcd.printString(buffer1,0,0);
-                lcd.printString(buffer2,0,1);
-                lcd.printString(buffer3,0,2);
+                lcd.printString("*****MENU*****",0,0);
+                lcd.printString("RANGE",0,1);
+                lcd.printString("PARAMETERS",0,2);
                 lcd.printString(buffer4,0,3);
-                lcd.printString(buffer,0,5);
+                lcd.printString("NEXT       ADJ",0,5);
                 break;
             case 5:
                 if (g_sw2_flag) {
@@ -588,16 +528,12 @@
                         r7 += 1;
                     }
                 }
-                sprintf(buffer1,"*****MENU*****");
-                sprintf(buffer2,"RANGE");
-                sprintf(buffer3,"PARAMETERS");
-                sprintf(buffer,"EXIT       ADJ");
                 sprintf(buffer4,"%iCm to %iCm",r6,r7);
-                lcd.printString(buffer1,0,0);
-                lcd.printString(buffer2,0,1);
-                lcd.printString(buffer3,0,2);
+                lcd.printString("*****MENU*****",0,0);
+                lcd.printString("RANGE",0,1);
+                lcd.printString("PARAMETERS",0,2);
                 lcd.printString(buffer4,0,3);
-                lcd.printString(buffer,0,5);
+                lcd.printString("EXIT       ADJ",0,5);
                 break;
             default:
                 lcd.clear();
@@ -608,6 +544,7 @@
 
 void setup()
 {
+    serial.baud(115200);  // full-speed!
     ticker.attach(&timer_isr_led,0.35); /// Attach the ticker for the flashig LEDs
     ticker_srf02.attach(&timer_isr_srf02,0.1);/// Attach the ticker for collecting a range reading
     ticker_standby.attach(&timer_isr_standby,5.0);
--- a/main.h	Wed May 04 14:05:07 2016 +0000
+++ b/main.h	Wed May 04 22:16:06 2016 +0000
@@ -22,8 +22,8 @@
 
 #define LOW 0 /// No output
 #define HIGH 1 /// High output
-#define METRIC 1
-#define IMPERIAL 0
+
+
 
 
 
@@ -125,7 +125,7 @@
 float c =1; /// convertion factor from Cm to inch 1 = metric no convertion
 int check_flag = 0; ///???????????
 float distbar;
-
+int flash = 0;
 struct Alertlevel {
     char srr_led;  /// stead RED LED state
     char sa_led;    /// stead AMBER LED state
@@ -140,12 +140,12 @@
 
 STyp Alertlevel[8] = {
     {LOW,LOW,LOW,LOW,LOW,LOW,0,1}, // no output
-    {LOW,LOW,LOW,LOW,LOW,HIGH,0.1,0.9}, //flash green
+    {LOW,LOW,LOW,LOW,LOW,HIGH,0.1,1.0}, //flash green
     {LOW,LOW,HIGH,LOW,LOW,LOW,0.1,0.5}, //steady green
-    {LOW,LOW,HIGH,LOW,HIGH,LOW,0.1,0.3}, //flash amber
-    {LOW,HIGH,HIGH,LOW,LOW,LOW,0.2,0.2}, //steady amber
+    {LOW,LOW,HIGH,LOW,HIGH,LOW,0.1,0.25}, //flash amber
+    {LOW,HIGH,HIGH,LOW,LOW,LOW,0.1,0.1}, //steady amber
     {LOW,HIGH,HIGH,HIGH,LOW,LOW,0.2,0.1}, //flash red
-    {HIGH,HIGH,HIGH,LOW,LOW,LOW,0.1,0.1},// steady red
+    {HIGH,HIGH,HIGH,LOW,LOW,LOW,0.1,0.05},// steady red
     {LOW,LOW,LOW,HIGH,HIGH,HIGH,1,0} // all flash
 };/*!< Array contaning structures for diffent outputs */