Level 2 Project Range Device

Dependencies:   N5110 SDFileSystem SRF02 TMP102 mbed

Files at this revision

API Documentation at this revision

Comitter:
el15pjt
Date:
Thu May 05 10:51:19 2016 +0000
Parent:
10:c9f3c22368f1
Child:
12:0171e8723d9d
Commit message:
Final commit.... Maybe

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 22:16:06 2016 +0000
+++ b/main.cpp	Thu May 05 10:51:19 2016 +0000
@@ -5,22 +5,16 @@
 */
 
 #include "main.h"
-#include "SDFileSystem.h"
-Serial serial(USBTX, USBRX);  // for PC debug
 
 int main()
 {
-    int d = 0;
-    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
-    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);
+    int d = 0;
+    int myarray[5] {999,999,999,999,999}; /// initilised to any possitive integer to prevent random values at start up triggering an alert
+
+    // Load settings from SD Card\\\\\\\\\\
     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");
@@ -33,7 +27,19 @@
         serial.printf("Done.\n");
         fclose(fp);  // ensure you close the file after reading
     }
-    lcd.setBrightness(bright);
+    //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
+
+    //Startup Screen\\\\\\\\\\\\\\\\\\\\
+    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);
+    //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
+
+    lcd.setBrightness(bright); //Set brightness of screen on startup from loaded value from SD Card
+
+    // Main while loop of the Ranger
     while(1) {
         if (g_sw1_flag) { /// Event triggered inturupt to call the menu and reset page numbers
             g_sw1_flag = 0;
@@ -44,15 +50,18 @@
         }
         if (g_timer_flag_srf02) {
             g_timer_flag_srf02 = 0;  // if it has, clear the flag
-            if (units == 1) {
+            if (units == 1) { // if units = 1 metric measurment are retrived from SFR02
                 distance = srf02.getDistanceCm();
                 distance = distance-(offset*c);
-            } else {
+            } else { //If units = 0 imperial measurments retrived from SFR02
                 distance = srf02.getDistannceInch();
-                distance = distance -(offset*c); //MAYBE MOVE C TO OFSET SETTING
-            }// close else breaket
+                distance = distance -(offset*c);
+            } // close else breaket
         } // close if inteurrupt bracket
 
+
+//Function to average last 5 readings\\\\\\\\\\\\\\\\
+
         if (d == 4) {
             d = 0;
         } else {
@@ -65,44 +74,65 @@
         }
         avgdistance = (totaldistance/5);
 
-        // serial.printf("TOATAL DISTANCE %i\n", totaldistance);
-        // serial.printf("DISTANCE %i\n", distance);
-        // serial.printf("AVERAGE DISTANCE %f\n", avgdistance);
-        // serial.printf("ARRAY ELEMENT = %i\n", d);
-        // serial.printf("myarray %i\n", myarray[d]);
+/*
+        Debugging\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
+        Uncomment to use button 2 to toggle thru alert levels for debugging and comment out setalert();
+        serial.printf("TOATAL DISTANCE %i\n", totaldistance);
+        serial.printf("DISTANCE %i\n", distance);
+        serial.printf("AVERAGE DISTANCE %f\n", avgdistance);
+        serial.printf("ARRAY ELEMENT = %i\n", d);
+        serial.printf("myarray %i\n", myarray[d]);
+        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);
+        }
+*/
 
-        /* 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);
         lcdoutput();
-        if (alert !=0) {
-            if (buzz_flag == 0) {
+
+        if (alert !=0) { // no buzzer needed at alert == 0 path clear
+            if (buzz_flag == 0) {// flag keeps buzz from being called durig it's off period
                 setbuzzer();
             }
         }
-        setleds();
-        sleep();
+        setleds(); // Function called to set LED outputs
+        sleep(); // Sleep till nest interupt
     }
 }
 
-void lcdoutput()
+void setalert()
 {
-    /**
-    Function called to print to the LCD display
-    @param i used to scale the max distaance to the width of the screen
-    @param distance Distance read from sensor to be dispayed
-    @param distbar used to adjust how far along the screen the bar is to go acorrding to distance
-    */
-
+    if (avgdistance >= (r6*c) && avgdistance < (r7*c)) {  // r6 150 and r7 200
+        alert = 1; /// alert 1 avgdistance between preset 150Cm to 200Cm
+    } else if (avgdistance >= (r5*c) && avgdistance  < (r6*c)) {
+        alert = 2; /// alert 2 when between preset 90Cm to 150Cm
+    } else if (avgdistance >= (r4*c) && avgdistance < (r5*c)) {
+        alert = 3; /// alert 3 when avgdistance between 60Cm to 90Cm
+    } else if (avgdistance >= (r3*c) && avgdistance < (r4*c)) {
+        alert = 4; /// alert 4 when avgdistance between 40Cm and 60Cm
+    } else if (avgdistance >= (r2*c) && avgdistance < (r3*c)) {
+        alert = 5; ///alert 5 when avgdistance between 20Cm and 40m
+    } else if (avgdistance >= (r1*c) && avgdistance <= (r2*c)) { //r1 3 and r2 20
+        alert = 6; ///alert 6 when avgdistance between 1 and 20
+    } else if (avgdistance <=(r1*c)) {
+        alert = 7; ///alert 7 when avgdistance below 1Cm
+        if (check_flag ==0) {
+            save();
+        }
+        check_flag = 1;
+    } else {
+        alert = 0; /// alert 0 all else
+    }
+}
+void lcdoutput()
+{   
     // if alert == 0 no need to display range path is clear
     if (alert == 0) {
         lcd.clear();
@@ -183,33 +213,10 @@
     }
 }//close function
 
-void setalert()
-{
-    if (avgdistance >= (r6*c) && avgdistance < (r7*c)) {  // r6 150 and r7 200
-        alert = 1; /// alert 1 avgdistance between preset 150Cm to 200Cm
-    } else if (avgdistance >= (r5*c) && avgdistance  < (r6*c)) {
-        alert = 2; /// alert 2 when between preset 90Cm to 150Cm
-    } else if (avgdistance >= (r4*c) && avgdistance < (r5*c)) {
-        alert = 3; /// alert 3 when avgdistance between 60Cm to 90Cm
-    } else if (avgdistance >= (r3*c) && avgdistance < (r4*c)) {
-        alert = 4; /// alert 4 when avgdistance between 40Cm and 60Cm
-    } else if (avgdistance >= (r2*c) && avgdistance < (r3*c)) {
-        alert = 5; ///alert 5 when avgdistance between 20Cm and 40m
-    } else if (avgdistance >= (r1*c) && avgdistance <= (r2*c)) { //r1 3 and r2 20
-        alert = 6; ///alert 6 when avgdistance between 1 and 20
-    } else if (avgdistance <=(r1*c)) {
-        alert = 7; ///alert 7 when avgdistance below 1Cm
-        if (check_flag ==0) {
-            save();
-        }
-        check_flag = 1;
-    } else {
-        alert = 0; /// alert 0 all else
-    }
-}
 
 void setleds()
 {
+    int flash = 0; ///Variable to toggle LEDs high low
     if (g_timer_flag_led) {
         g_timer_flag_led = 0;
         flash = !flash; // if it has, clear the flag
@@ -267,38 +274,12 @@
     lcd.setBrightness(bright);
 }
 
-void sw2_isr()
-{
-    g_sw2_flag = 1;   /** set flag in ISR by button 2 @param g_sw2_flag 0 or 1*/
-}
-
-void sw1_isr()
-{
-    g_sw1_flag = 1;   /** set flag in ISR by button 2 @param g_sw1_flag 0 or 1*/
-}
-
-void timer_isr_led()
-{
-    g_timer_flag_led = 1;   /** set flag in ISR by timer_isr_led @param g_timer_flag_led 0 or 1 */
-}
-
-void timer_isr_srf02()
-{
-    g_timer_flag_srf02 = 1;   /** set flag in ISR by ticker_srf02 @param g_timer_flag_srf02 0 or 1 */
-}
-
-void timer_isr_standby()
-{
-    g_timer_flag_standby = 1; /** set flag in ISR by ticker_tone @param g_timer_flag_tone 0 or 1 */
-}
-
-
 void menu()
 {
     while(1) {
         if (g_sw1_flag) {
             g_sw1_flag = 0;
-            page++; /// Moves page
+            page++; // Moves page
             lcd.clear();
         }
         switch (page) {
@@ -375,13 +356,13 @@
                     bright = 1.0;
                     offset = 0;
                     units = 1;
-                    r1 = 03;/// Upper limit of alert 1
-                    r2 = 10;/// Upper limit of alert 2
-                    r3 = 20;/// Upper limit of alert 3
-                    r4 = 35;/// Upper limit of alert 4
-                    r5 = 50;/// Upper limit of alert 5
-                    r6 = 65;/// Upper limit of alert 6
-                    r7 = 80;/// Upper limit of alert 7
+                    r1 = 03;// Upper limit of alert 1
+                    r2 = 10;// Upper limit of alert 2
+                    r3 = 20;// Upper limit of alert 3
+                    r4 = 30;// Upper limit of alert 4
+                    r5 = 50;// Upper limit of alert 5
+                    r6 = 60;// Upper limit of alert 6
+                    r7 = 80;// Upper limit of alert 7
                     lcd.clear();
                     lcd.printString("SETTINGS",0,1);
                     lcd.printString("RESET",0,2);
@@ -415,21 +396,6 @@
     }//while braket
 }//functon bracket
 
-
-void save()
-{
-    fp = fopen("/sd/settings.txt", "w");
-    if (fp == NULL) {  // if it can't open the file then print error message
-        serial.printf("Error! Unable to open file!\n");
-    } else {  // opened file so can write
-        serial.printf("Writing to file....");
-        fprintf(fp, "%f,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i",bright,units,offset,check_flag,r1,r2,r3,r4,r5,r6,r7); // ensure data type matches
-        serial.printf("Done.\n");
-        fclose(fp);  // ensure you close the file after writing
-    }
-}
-
-
 void submenu()
 {
     while(1) {
@@ -443,7 +409,7 @@
                 if (g_sw2_flag) {
                     g_sw2_flag = 0;           // if it has, clear the flag
                     if (r2 == r3) {
-                        r2 = 1;
+                        r2 = 3;
                     } else {
                         r2 = r2+1;
                     }
@@ -496,7 +462,7 @@
                         r5 += 1;
                     }
                 }
-                sprintf(buffer4,"%iCm to %iCm",r4,r5);  
+                sprintf(buffer4,"%iCm to %iCm",r4,r5);
                 lcd.printString("*****MENU*****",0,0);
                 lcd.printString("RANGE",0,1);
                 lcd.printString("PARAMETERS",0,2);
@@ -542,11 +508,24 @@
     }//while bracket
 }//function bracket
 
+void save()
+{
+    fp = fopen("/sd/settings.txt", "w");
+    if (fp == NULL) {  // if it can't open the file then print error message
+        serial.printf("Error! Unable to open file!\n");
+    } else {  // opened file so can write
+        serial.printf("Writing to file....");
+        fprintf(fp, "%f,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i",bright,units,offset,check_flag,r1,r2,r3,r4,r5,r6,r7); // ensure data type matches
+        serial.printf("Done.\n");
+        fclose(fp);  // ensure you close the file after writing
+    }
+}
+
 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_srf02.attach(&timer_isr_srf02,0.2);/// Attach the ticker for collecting a range reading
     ticker_standby.attach(&timer_isr_standby,5.0);
     sw1.rise(&sw1_isr); /// sw1_isr called when button presed on the rising edge
     sw2.rise(&sw2_isr); /// sw2_isr called when button presed on the rising edge
@@ -558,4 +537,29 @@
     gg_led = 0; //PCB LEDS
     sw2.mode(PullDown); //Turns on use of the pulldown resistors for use with the PCB buttons
     sw1.mode(PullDown); //Turns on use of the pulldown resistors for use with the PCB buttons
-}
\ No newline at end of file
+}
+
+void sw2_isr()
+{
+    g_sw2_flag = 1;   /** set flag in ISR by button 2 @param g_sw2_flag 0 or 1*/
+}
+
+void sw1_isr()
+{
+    g_sw1_flag = 1;   /** set flag in ISR by button 2 @param g_sw1_flag 0 or 1*/
+}
+
+void timer_isr_led()
+{
+    g_timer_flag_led = 1;   /** set flag in ISR by timer_isr_led @param g_timer_flag_led 0 or 1 */
+}
+
+void timer_isr_srf02()
+{
+    g_timer_flag_srf02 = 1;   /** set flag in ISR by ticker_srf02 @param g_timer_flag_srf02 0 or 1 */
+}
+
+void timer_isr_standby()
+{
+    g_timer_flag_standby = 1; /** set flag in ISR by ticker_tone @param g_timer_flag_tone 0 or 1 */
+}
--- a/main.h	Wed May 04 22:16:06 2016 +0000
+++ b/main.h	Thu May 05 10:51:19 2016 +0000
@@ -2,7 +2,7 @@
 @file main.h
 @brief Header file containing functions prototypes, defines and global variables.
 @brief Ranger Project
-@brief Revision 1.0.
+@brief Revision 1.3.
 @author Philip Thompson
 @date   March 2016
 @brief The following code has been writen for the University of Leeds ELEC264501 embedded system project and is intended to
@@ -24,7 +24,8 @@
 #define HIGH 1 /// High output
 
 
-
+/// for PC debug
+Serial serial(USBTX, USBRX);
 
 
 /**
@@ -60,11 +61,11 @@
 @namespace  Timers
 @brief Tickers and Timeouts
 */
-Ticker ticker;
-Ticker ticker_srf02;
-Ticker ticker_standby;
-Timeout buzzoff;
-Timeout buzzon;
+Ticker ticker; // Ticker to control LED flash
+Ticker ticker_srf02; //Ticker to get distance reading
+Ticker ticker_standby; //Ticker to control standby
+Timeout buzzoff; //Buzzer off duratuion
+Timeout buzzon; // buzzer on duration
 
 // Create TMP102 object
 TMP102 tmp102(I2C_SDA,I2C_SCL);
@@ -84,6 +85,9 @@
 /// Connections to SD card holder on K64F (SPI interface)
 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
 FILE *fp;
+
+//FLAGS\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
+
 /**
 @namespace  TimerFlags
 @brief Flags for use with timed interupts
@@ -97,35 +101,58 @@
 */
 volatile int g_sw1_flag = 0, g_sw2_flag = 0;
 
-/**
-@namespace  RangePresets
-@brief Preset range of the differant alert levels
-*/
-float Traw;
+
+
+//VERIABLES\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
+/// Upper limit of alert 1
+int r1 = 03;
+/// Upper limit of alert 2
+int r2 = 10;
+/// Upper limit of alert 3
+int r3 = 20;
+/// Upper limit of alert 4
+int r4 = 30;
+/// Upper limit of alert 5
+int r5 = 40;
+/// Upper limit of alert 6
+int r6 = 50;
+/// Upper limit of alert 7
+int r7 = 70;
+/// Veriable to hold sumation of distance readings for mean vale calculation
 int totaldistance;
-int r1 = 03;/// Upper limit of alert 1
-int r2 = 10;/// Upper limit of alert 2
-int r3 = 20;/// Upper limit of alert 3
-int r4 = 35;/// Upper limit of alert 4
-int r5 = 50;/// Upper limit of alert 5
-int r6 = 65;/// Upper limit of alert 6
-int r7 = 80;/// Upper limit of alert 7
-float avgdistance =0; // Averaged Distance vale from ten previous readings
-int subpage; ///< veriable to hold the page being viewed with in the submenu
-int page;/// veriable to hold the page being viewed with in the menu
-int offset = 0;/// veriable to hold the offset and adjust the 0 Range point
-int alert;/// veriable to hold the current alert level
-int distance;/// veriable to hold the distance read from the srf02 sensor
-float bright = 1.0;/// veriable to hold the current LED backlight of the 5110 LCD
-int units = 1;/// veriable to hold the current unit type set to METRIC as default
-//int length, length1, length2, length3, length4; //
-char buffer[14], buffer1[14], buffer2[14], buffer3[14], buffer4[14],buffer5[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) not ended globale but reused frquently throuh out
-float T; /// Temperature value retured from TMP102 sensor
-int standby = 0; ///?????????????????????
-float c =1; /// convertion factor from Cm to inch 1 = metric no convertion
-int check_flag = 0; ///???????????
+///< veriable to hold the page being viewed with in the submenu
+int subpage;
+/// veriable to hold the page being viewed with in the menu
+int page;
+/// veriable to hold the offset and adjust the 0 Range point
+int offset = 0;
+/// veriable to hold the current alert level
+int alert;
+/// veriable to hold the distance read from the srf02 sensor
+int distance;
+/// veriable to hold the current unit type set to 1 = METRIC as default
+int units = 1;
+///standby state 1. path clear 2. Temp 3.Temp no backlight
+int standby = 0;
+///flag raised is there is a collision
+int check_flag = 0;
+/// Temp reading return from TMP120 sensor
+float Traw;
+/// veriable to hold the current LED backlight of the 5110 LCD
+float bright = 1.0;
+/// Temperature value retured from TMP102 sensor
+float T;
+/// Veriable to hold the distance across the screen the distance bar should go
 float distbar;
-int flash = 0;
+/// Averaged Distance vale from ten previous readings
+float avgdistance =0;
+/// convertion factor from Cm to inch 1 = metric no convertion
+float c =1;
+// each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) not needed to be globle but reused frquently throuhout
+char buffer[14], buffer1[14], buffer2[14], buffer3[14], buffer4[14],buffer5[14];
+
+
+/*!< Stucture to hold all outputs. Steady LEDs, Flashing LEDs, Tone on, Tonne off*/
 struct Alertlevel {
     char srr_led;  /// stead RED LED state
     char sa_led;    /// stead AMBER LED state
@@ -133,11 +160,12 @@
     char frr_led;///FLASHING RED LED state
     char fa_led; ///FLASHING AMBER LED state
     char fgg_led; ///FLASHING GREEN LED state
-    float toneon;
-    float toneoff;
-}; /*!< Stucture to hold all outputs. Steady LEDs, Flashing LEDs, Tone on, Tonne off*/
+    float toneon; ///Tone on time
+    float toneoff; ///Tone off time
+};
 typedef const struct Alertlevel STyp;
 
+/*!< Array contaning structures for diffent outputs */
 STyp Alertlevel[8] = {
     {LOW,LOW,LOW,LOW,LOW,LOW,0,1}, // no output
     {LOW,LOW,LOW,LOW,LOW,HIGH,0.1,1.0}, //flash green
@@ -149,14 +177,42 @@
     {LOW,LOW,LOW,HIGH,HIGH,HIGH,1,0} // all flash
 };/*!< Array contaning structures for diffent outputs */
 
-void flip();
-void buzzflag();
+
 
 
-/** Controls the LCD while not in a Menu  Function called to print to the LCD display
-    @param i used to scale the max distaance to the width of the screen
-    @param distance Distance read from sensor to be dispayed
-    @param distbar used to adjust how far along the screen the bar is to go acorrding to distance
+//FUNCTIONS/////////////////////////////////////////////
+
+/**
+Function called to stop buzzer at end of on period and then make buzzer avilable again after off period
+@param buzz_flag 1 or 0
+@param buzzer = 0.0 buzzer off
+@code
+{
+    buzz_flag = 1;
+    buzzer = 0.0;
+    buzzon.attach(&buzzflag,Alertlevel[alert].toneoff);
+}
+@endcode
+*/
+void flip();
+
+/** Flag used it indicate if buzzer is avilable or on a off period
+@param buzz_flag zero
+@code
+{
+    buzz_flag = 0;
+    return;
+}
+@endcode
+*/
+void buzzflag();
+
+/** Controls the LCD while not in a Menu
+
+@param i used to scale the max distaance to the width of the screen
+@param distance Distance read from sensor to be dispayed
+@param distbar used to adjust how far along the screen the bar is to go acorrding to distance
+    
 @code
 if (alert == 0) {
 if (g_timer_flag_standby) {
@@ -238,10 +294,6 @@
 @endcode
 */
 void lcdoutput();
-void save();
-void timer_isr_led();
-void timer_isr_srf02();
-void timer_isr_standby();
 
 /** Called to increment to brightness by 0.2 each time when at 1 resets back to 0.0
 @param bright 0.0-1
@@ -256,8 +308,28 @@
 @endcode
 */
 void backlight();
-void sw2_isr();
-void sw1_isr();
+
+/**
+Sets up and initalizies switches, LEDs, Tickers and serial connection
+@code
+{
+    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.2);/// Attach the ticker for collecting a range reading
+    ticker_standby.attach(&timer_isr_standby,5.0);
+    sw1.rise(&sw1_isr); /// sw1_isr called when button presed on the rising edge
+    sw2.rise(&sw2_isr); /// sw2_isr called when button presed on the rising edge
+    r_led = 1; //Onboard leds
+    b_led = 1; //Onboard leds
+    g_led = 1; //Onboard leds
+    rr_led = 0; //PCB LEDS
+    a_led = 0; //PCB LEDS
+    gg_led = 0; //PCB LEDS
+    sw2.mode(PullDown); //Turns on use of the pulldown resistors for use with the PCB buttons
+    sw1.mode(PullDown); //Turns on use of the pulldown resistors for use with the PCB buttons
+}
+@endcode
+*/
 void setup();
 
 /** A fuction used to determin the alert level given a range with the use of IF statments
@@ -286,54 +358,62 @@
 @endcode
 */
 void setalert();
-/** If statments to determine the output of each LED by inspecting the struct in the relevent element of the alertlevel array
 
+/** Function for controlling the LED outputs
+@para alert changes to element array and so output controlls
 @code
-   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;
-       }
-   } else {
-       rr_led = Alertlevel[alert].srr_led;
-       a_led = Alertlevel[alert].sa_led;
-       gg_led = Alertlevel[alert].sgg_led;
-   }
+{
+    int flash = 0; ///Variable to toggle LEDs high low
+    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;
+    }
+    if(Alertlevel[alert].fgg_led == HIGH) {
+        gg_led = flash;
+    } else {
+        gg_led = Alertlevel[alert].sgg_led;
+    }
+}
+@endcode
+*/
+void setleds();
 
+ /**
+ @code
+{
+   
+    control the PWM to drive the buzzer
+    @param buzzer.period frequncy 1KHz
+    @param buzzer duty cycle equal on/off max volume
+    @param Alertlevel[alert].toneon controls how long the tone will last depending on alert
+    */
+    buzzer.period (1.0/1000.0);
+    buzzer = 0.5;
+    buzzoff.attach(&flip, Alertlevel[alert].toneon);
 }
-@endcode*/
-void setleds();
+@endcode
+*/
 void setbuzzer();
-void menu();
-/**
-{
-     Function called to invoke a menu within a while loop with button interupts used to move pages and adjust within a switch statment
-    @param g_sw1_flag 0 or 1 used to advance page
-    @param g_sw2_flag 0 or 1 used to adjust current item
-    @code
-    while(1) {
 
 
+/**
+Function used to call and navigate Main menu and change settings
+@code
+{
+    while(1) {
         if (g_sw1_flag) {
             g_sw1_flag = 0;
-            page++; /// Moves page
+            page++; // Moves page
             lcd.clear();
         }
         switch (page) {
@@ -342,232 +422,312 @@
                     g_sw2_flag = 0;  // if it has, clear the flag
                     backlight();
                     lcd.clear();
-                } else {
-                    int lightbar = bright*84;
-                    sprintf(buffer2,"*****MENU*****");
-                    sprintf(buffer1,"BACKLIGHT");
-                    sprintf(buffer3,"%.0f%%",bright*100);
-                    lcd.printString(buffer2,0,0);
-                    lcd.printString(buffer1,0,1);
-                    lcd.printString(buffer3,0,2);
-                    lcd.drawRect(0,38,lightbar,7,1);
-                    lcd.refresh();
-
                 }
+                int lightbar = bright*84;
+                sprintf(buffer2,"%.0f%%",bright*100);
+                lcd.drawRect(0,26,lightbar,7,1);  // move bar up!!!!!!!!!!!!!!!!
+                lcd.printString("BACKLIGHT",0,1);
+                lcd.printString(buffer2,0,2);
+                lcd.printString("NEXT       ADJ",0,5);
+                lcd.refresh();
                 break;
-
             case 1:
                 if (g_sw2_flag) {
                     g_sw2_flag = 0;  // if it has, clear the flag
                     if (offset == 20) {
                         offset = 0;
+                        lcd.clear();
                     } else {
                         offset += 1;
                     }
-                } else {
-                    length2 = sprintf(buffer2,"*****MENU*****");
-                    length1 = sprintf(buffer1,"OFFSET");
-                    length3 = sprintf(buffer3,"%i",offset);
-                    lcd.printString(buffer2,0,0);
-                    lcd.printString(buffer1,0,1);
-                    lcd.printString(buffer3,0,2);
-                    lcd.refresh();
                 }
-
+                sprintf(buffer2,"%i",offset);
+                lcd.printString("OFFSET",0,1);
+                sprintf(buffer4,"NEXT       ADJ");
+                lcd.printString(buffer4,0,5);
                 break;
-
             case 2:
                 if (g_sw2_flag) {
                     g_sw2_flag = 0;  // if it has, clear the flag
                     if (units == 1) {
                         units = 0;
+                        c = 0.3937;
                     } else {
                         units = 1;
+                        c = 1;
+                        lcd.clear();
                     }
                 }
-                if (units == 1) {
-                    sprintf(buffer3,"METRIC");
-                    lcd.printString(buffer3,0,2);
+                if (units == 0) {
+                    sprintf(buffer2,"IMPERIAL");
                 } else {
-                    sprintf(buffer3,"IMPERIAL");
-                    lcd.printString(buffer3,0,2);
+                    sprintf(buffer2,"METRIC");
+                }
+                lcd.printString("NEXT       ADJ",0,5);
+                lcd.printString("UNITS",0,1);
+                break;
+            case 3:
+                if (g_sw2_flag) {
+                    g_sw2_flag = 0;  // if it has, clear the flag
+                    check_flag =0;
+                    lcd.clear();
+                }
+                if (check_flag == 0) {
+                    sprintf(buffer2,"COLLISION");
+                    lcd.printString("NO",0,1);
+                    sprintf(buffer4,"NEXT     ");
+                } else {
+                    sprintf(buffer2,"COLLISION");
+                    lcd.printString("CLEAR",0,1);
+                    sprintf(buffer4,"NEXT     CLEAR");
                 }
 
-                sprintf(buffer2,"*****MENU*****");
-                sprintf(buffer1,"UNITS");
-                lcd.printString(buffer2,0,0);
-                lcd.printString(buffer1,0,1);
-                lcd.refresh();
-
+                lcd.printString(buffer4,0,5);
                 break;
-
-            case 3:
-
+            case 4:
+                if (g_sw2_flag) {
+                    g_sw2_flag = 0;  // if it has, clear the flag
+                    bright = 1.0;
+                    offset = 0;
+                    units = 1;
+                    r1 = 03;// Upper limit of alert 1
+                    r2 = 10;// Upper limit of alert 2
+                    r3 = 20;// Upper limit of alert 3
+                    r4 = 30;// Upper limit of alert 4
+                    r5 = 50;// Upper limit of alert 5
+                    r6 = 60;// Upper limit of alert 6
+                    r7 = 80;// Upper limit of alert 7
+                    lcd.clear();
+                    lcd.printString("SETTINGS",0,1);
+                    lcd.printString("RESET",0,2);
+                    wait(1);
+                    return;
+                }
+                sprintf(buffer2,"SETTINGS");
+                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();
                 }
-                length2 = sprintf(buffer3,"*****MENU*****");
-                length3 = sprintf(buffer1,"RANGE");
-                length3 = sprintf(buffer2,"PARAMETERS");
-                lcd.printString(buffer3,0,0);
-                lcd.printString(buffer1,0,1);
-                lcd.printString(buffer2,0,2);
+                sprintf(buffer2,"PARAMETERS");
+                lcd.printString("RANGE",0,1);
+                lcd.printString("EXIT       ADJ",0,5);
+                break;
+            default:
+                lcd.clear();
+                save ();
+                lcd.printString("    SAVING    ",0,2);
+                lcd.printString("   SETTINGS   ",0,3);
+                wait (1);
+                return;
+        }// switch bracket
+        lcd.printString(buffer2,0,2);
+        lcd.printString("*****MENU*****",0,0);
+        lcd.refresh();
+    }//while braket
+}//functon bracket
+@endcode
+*/
+void menu();
 
 
+/**
+Function for submenu Controlling the range peramiters
+@code
+{
+    while(1) {
+        /// interupt used to shift page
+        if (g_sw1_flag) {
+            g_sw1_flag = 0;
+            subpage++;
+        }
+        switch (subpage) {  ///interupt used to adjust range
+            case 0:
+                if (g_sw2_flag) {
+                    g_sw2_flag = 0;           // if it has, clear the flag
+                    if (r2 == r3) {
+                        r2 = 3;
+                    } else {
+                        r2 = r2+1;
+                    }
+                }
+                sprintf(buffer4,"1Cm to %iCm",r2);
+                lcd.printString("*****MENU*****",0,0);
+                lcd.printString("RANGE",0,1);
+                lcd.printString("PARAMETERS",0,2);
+                lcd.printString(buffer4,0,3);
+                lcd.printString("NEXT       ADJ",0,5);
                 break;
-
+            case 1:
+                if (g_sw2_flag) {
+                    g_sw2_flag = 0;           // if it has, clear the flag
+                    if (r3 == r4) {
+                        r3 = r2;
+                    } else {
+                        r3 += 1;
+                    }
+                }
+                sprintf(buffer4,"%iCm to %iCm",r2,r3);
+                lcd.printString("*****MENU*****",0,0);
+                lcd.printString("RANGE",0,1);
+                lcd.printString("PARAMETERS",0,2);
+                lcd.printString(buffer4,0,3);
+                lcd.printString("NEXT       ADJ",0,5);
+                break;
+            case 2:
+                if (g_sw2_flag) {
+                    g_sw2_flag = 0;           // if it has, clear the flag
+                    if (r4 == r5) {
+                        r4 = r3;
+                    } else {
+                        r4 += 1;
+                    }
+                }
+                sprintf(buffer4,"%iCm to %iCm",r3,r4);
+                lcd.printString("*****MENU*****",0,0);
+                lcd.printString("RANGE",0,1);
+                lcd.printString("PARAMETERS",0,2);
+                lcd.printString(buffer4,0,3);
+                lcd.printString("NEXT       ADJ",0,5);
+                break;
+            case 3:
+                if (g_sw2_flag) {
+                    g_sw2_flag = 0;           // if it has, clear the flag
+                    if (r5 == r6) {
+                        r5 = r4;
+                    } else {
+                        r5 += 1;
+                    }
+                }
+                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("NEXT       ADJ",0,5);
+                break;
+            case 4:
+                if (g_sw2_flag) {
+                    g_sw2_flag = 0;           // if it has, clear the flag
+                    if (r6 == r7) {
+                        r6 = r5;
+                    } else {
+                        r6 += 1;
+                    }
+                }
+                sprintf(buffer4,"%iCm to %iCm",r5,r6);
+                lcd.printString("*****MENU*****",0,0);
+                lcd.printString("RANGE",0,1);
+                lcd.printString("PARAMETERS",0,2);
+                lcd.printString(buffer4,0,3);
+                lcd.printString("NEXT       ADJ",0,5);
+                break;
+            case 5:
+                if (g_sw2_flag) {
+                    g_sw2_flag = 0;           // if it has, clear the flag
+                    if (r7 == 300) {
+                        r7 = r6;
+                    } else {
+                        r7 += 1;
+                    }
+                }
+                sprintf(buffer4,"%iCm to %iCm",r6,r7);
+                lcd.printString("*****MENU*****",0,0);
+                lcd.printString("RANGE",0,1);
+                lcd.printString("PARAMETERS",0,2);
+                lcd.printString(buffer4,0,3);
+                lcd.printString("EXIT       ADJ",0,5);
+                break;
             default:
                 lcd.clear();
                 return;
-        }
-    }
-    @endcode
-    */
-void menu();
-/**
-@code
-   while(1) {
-    /// innterupt used to shift page
-    if (g_sw1_flag) {
-        g_sw1_flag = 0;
-        subpage++;
-    }
-    switch (subpage) {
-            ///interupt used to adjust range
-        case 0:
-            if (g_sw2_flag) {
-                g_sw2_flag = 0;           // if it has, clear the flag
-                if (r2 == r3) {
-                    r2 = 1;
-                } else {
-                    r2 = r2+1;
-                }
-            }
-            length2 = sprintf(buffer1,"*****MENU*****");
-            length3 = sprintf(buffer2,"RANGE");
-            length3 = sprintf(buffer3,"PARAMETERS");
-            length4 = sprintf(buffer4,"1Cm  to  %iCm",r2);
-            lcd.printString(buffer1,0,0);
-            lcd.printString(buffer2,0,1);
-            lcd.printString(buffer3,0,2);
-            lcd.printString(buffer4,0,3);
-            break;
-
-        case 1:
-
-            if (g_sw2_flag) {
-                g_sw2_flag = 0;           // if it has, clear the flag
-                if (r3 == r4) {
-                    r3 = r2;
-                } else {
-                    r3 += 1;
-                }
-            }
-
-            sprintf(buffer1,"*****MENU*****");
-            sprintf(buffer2,"RANGE");
-            sprintf(buffer3,"PARAMETERS");
-            sprintf(buffer4,"%iCm  to  %iCm",r2,r3);
-            lcd.printString(buffer1,0,0);
-            lcd.printString(buffer2,0,1);
-            lcd.printString(buffer3,0,2);
-            lcd.printString(buffer4,0,3);
-            break;
-
-        case 2:
-
-            if (g_sw2_flag) {
-                g_sw2_flag = 0;           // if it has, clear the flag
-                if (r4 == r5) {
-                    r4 = r3;
-                } else {
-                    r4 += 1;
-                }
-            }
+        }//switch breaket
+    }//while bracket
+}//function bracket
+@endcode
+*/
+void submenu();
 
-            sprintf(buffer1,"*****MENU*****");
-            sprintf(buffer2,"RANGE");
-            sprintf(buffer3,"PARAMETERS");
-            sprintf(buffer4,"%iCm  to  %iCm",r3,r4);
-            lcd.printString(buffer1,0,0);
-            lcd.printString(buffer2,0,1);
-            lcd.printString(buffer3,0,2);
-            lcd.printString(buffer4,0,3);
-            break;
-
-        case 3:
-
-            if (g_sw2_flag) {
-                g_sw2_flag = 0;           // if it has, clear the flag
-                if (r5 == r6) {
-                    r5 = r4;
-                } else {
-                    r5 += 1;
-                }
-            }
-
-            sprintf(buffer1,"*****MENU*****");
-            sprintf(buffer2,"RANGE");
-            sprintf(buffer3,"PARAMETERS");
-            sprintf(buffer4,"%iCm to %iCm",r4,r5);
-            lcd.printString(buffer1,0,0);
-            lcd.printString(buffer2,0,1);
-            lcd.printString(buffer3,0,2);
-            lcd.printString(buffer4,0,3);
-            break;
-        case 4:
-
-            if (g_sw2_flag) {
-                g_sw2_flag = 0;           // if it has, clear the flag
-                if (r6 == r7) {
-                    r6 = r5;
-                } else {
-                    r6 += 1;
-                }
-            }
-
-            length2 = sprintf(buffer1,"*****MENU*****");
-            length3 = sprintf(buffer2,"RANGE");
-            length3 = sprintf(buffer3,"PARAMETERS");
-            length3 = sprintf(buffer4,"%iCm to %iCm",r5,r6);
-            lcd.printString(buffer1,0,0);
-            lcd.printString(buffer2,0,1);
-            lcd.printString(buffer3,0,2);
-            lcd.printString(buffer4,0,3);
-            break;
-        case 5:
-
-            if (g_sw2_flag) {
-                g_sw2_flag = 0;           // if it has, clear the flag
-                if (r7 == 300) {
-                    r7 = r6;
-                } else {
-                    r7 += 1;
-                }
-            }
-
-            length2 = sprintf(buffer1,"*****MENU*****");
-            length3 = sprintf(buffer2,"RANGE");
-            length3 = sprintf(buffer3,"PARAMETERS");
-            length3 = sprintf(buffer4,"%iCm to %iCm",r6,r7);
-            lcd.printString(buffer1,0,0);
-            lcd.printString(buffer2,0,1);
-            lcd.printString(buffer3,0,2);
-            lcd.printString(buffer4,0,3);
-            break;
-
-        default:
-            lcd.clear();
-            //  save();
-            return;
+/**
+Save function saving settings
+@param bright - Backlight setting
+@param units - Metric or Imperial settings
+@param offset - offest distance setting
+@param check_flag - Saves a collision alert
+@param r1 - upper limit for alert 1
+@param r2 - upper limit for alert 2
+@param r3 - upper limit for alert 3
+@param r4 - upper limit for alert 4
+@param r5 - upper limit for alert 5
+@param r6 - upper limit for alert 6
+@param r7 - upper limit for alert 7
+@code
+{
+    fp = fopen("/sd/settings.txt", "w");
+    if (fp == NULL) {  // if it can't open the file then print error message
+        serial.printf("Error! Unable to open file!\n");
+    } else {  // opened file so can write
+        serial.printf("Writing to file....");
+        fprintf(fp, "%f,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i",bright,units,offset,check_flag,r1,r2,r3,r4,r5,r6,r7); // ensure data type matches
+        serial.printf("Done.\n");
+        fclose(fp);  // ensure you close the file after writing
     }
 }
+@endcode
+*/
+void save();
+
+/**
+Inturupt flag for button 2
+@code
+{
+    g_sw2_flag = 1;   //set flag in ISR by button 2 @param g_sw2_flag 0 or 1
 }
 @endcode
 */
-void submenu();
-void save();
-void load();
+void sw2_isr();
+
+/**
+Inturupt flag for button 1
+@code
+{
+    g_sw1_flag = 1;   //set flag in ISR by button 2 @param g_sw1_flag 0 or 1
+}
+@endcode
+*/
+void sw1_isr();
+
+/**
+Flag used with ticker for flashing of LEDs
+@code
+{
+    g_timer_flag_led = 1;   // set flag in ISR by timer_isr_led @param g_timer_flag_led 0 or 1 
+}
+@endcode
+*/
+void timer_isr_led();
+
+/**
+Flag used for Ticker controlling SRF02 sensor reading
+@code
+{
+    g_timer_flag_srf02 = 1;   // set flag in ISR by ticker_srf02 @param g_timer_flag_srf02 0 or 1 
+}
+@endcode
+*/
+void timer_isr_srf02();
+
+/**
+Flag raised for incrementing standby level
+@code
+{
+    g_timer_flag_standby = 1; // set flag in ISR by ticker_tone @param g_timer_flag_tone 0 or 1
+}#
+@endcode
+*/
+void timer_isr_standby();
 
 #endif
\ No newline at end of file