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:
Mon Apr 25 10:08:05 2016 +0000
Parent:
6:b464a8a94d91
Child:
8:fe6ebe807b9d
Commit message:
BUZZER TO IMPLEMENT AND AVERAGE;

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
TMP102.lib Show annotated file Show diff for this revision Revisions of this file
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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Mon Apr 25 10:08:05 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/mbed/code/SDFileSystem/#7b35d1709458
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TMP102.lib	Mon Apr 25 10:08:05 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/eencae/code/TMP102/#1b601445b336
--- a/main.cpp	Fri Apr 15 17:23:17 2016 +0000
+++ b/main.cpp	Mon Apr 25 10:08:05 2016 +0000
@@ -1,114 +1,67 @@
-/*
-Philip Thompsonn
-EL15PJT
-Embedded System Project
-EL2645
+
+/**
+@file main.cpp
+@brief Program implementation
 */
 
-#include "mbed.h"
-#include "SRF02.h"
-#include "N5110.h"
-
-#define LOW 0
-#define HIGH 1
-#define METRIC 1
-#define IMPERIAL 0
-
-DigitalOut rr_led (PTA1);
-DigitalOut a_led (PTC2);
-DigitalOut gg_led(PTB23);
-PwmOut buzzer(PTA2);
-
-// Ranger object
-SRF02 srf02(I2C_SDA,I2C_SCL);
-
-//N5110 Object  VCC, SCE,RST, D/C, MOSI,SCLK, LED
-N5110           lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);
-
-Ticker ticker;
-Ticker ticker_srf02;
-Ticker ticker_tone;
-Timeout buzzoff;
-
-// K64F on-board LEDs
-DigitalOut r_led(LED_RED);
-DigitalOut g_led(LED_GREEN);
-DigitalOut b_led(LED_BLUE);
-
-// K64F on-board switches
-InterruptIn sw1(SW2);
-InterruptIn sw2(SW3);
-
-volatile int g_timer_flag_led = 0;
-volatile int g_timer_flag_srf02 = 0;
-volatile int g_timer_flag_tone = 0;
-volatile int g_sw1_flag = 0;
-volatile int g_sw2_flag = 0;
-
-int r1 = 3;
-int r2 = 20;
-int r3 = 40;
-int r4 = 60;
-int r5 = 80;
-int r6 = 100;
-int r7 = 120;
-
-int subpage;
-int page;
-int offset = 0;
-int alert;
-int distance;
-float bright = 1;
-char units = METRIC;
-
-// struct for state
-struct Alertlevel {
-    char srr_led;  // output value
-    char sa_led;    // time in state
-    char sgg_led;  // array of next states
-    char frr_led;
-    char fa_led;
-    char fgg_led;
-    float toneon;
-    float toneoff;
-};
-typedef const struct Alertlevel STyp;
-
-STyp Alertlevel[8] = {
-    {LOW,LOW,LOW,LOW,LOW,LOW,0,1}, // no output
-    {LOW,LOW,LOW,LOW,LOW,HIGH,0.05,1}, //flash green
-    {LOW,LOW,HIGH,LOW,LOW,LOW,0.1,1}, //steady green
-    {LOW,LOW,HIGH,LOW,HIGH,LOW,0.2,0.5}, //flash amber
-    {LOW,HIGH,HIGH,LOW,LOW,LOW,0.55,0.25}, //steady amber
-    {LOW,HIGH,HIGH,HIGH,LOW,LOW,0.8,0.1}, //flash red
-    {HIGH,HIGH,HIGH,LOW,LOW,LOW,1,1.05},// steady red
-    {LOW,LOW,LOW,HIGH,HIGH,HIGH,1,0.05} // all flash
-};
-void lcdoutput();
-void timer_isr_led(); // timed interuprt
-void timer_isr_srf02();
-void timer_isr_tone();
-void backlight();
-void init_K64F(); // set up board
-void sw2_isr();
-void sw1_isr();
-void setalert();
-void setleds();
-void setbuzzer();
-void nobuzz();
-void menu();
-void submenu();
+#include "main.h"
+#include "SDFileSystem.h"
+Serial serial(USBTX, USBRX);  // for PC debug
 
 
 int main()
 {
-    lcd.init();
-    init_K64F();
-    ticker.attach(&timer_isr_led,0.35);
-    ticker_srf02.attach(&timer_isr_srf02,0.02);
-    ticker_tone.attach(&timer_isr_tone,Alertlevel[alert].toneoff);
-    sw1.fall(&sw1_isr);
-    sw2.fall(&sw2_isr);
+    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);
+        wait (3);
+        */
+    /*
+    serial.baud(115200);  // full-speed!
+    serial.printf("#### SD Card Example #####\n");
+    FILE *fp; // this is our file pointer
+    wait(1);
+    {
+       /////////////////////// Reading from file example ////////////////////////
+
+       // now open file for reading...note the 'r'
+       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");
+       } else {
+           serial.printf("Reading file....\n");
+           int r1;
+           int r2;
+           int r3;
+           int r4;
+           int r5;
+           int r6;
+           int r7;
+           int offset; // create suitable variables to store the data in the file
+           float bright;
+           float value;
+           int i;
+
+           // in this example, we keep reading (using fscanf) until we reach
+           // the 'end of file'. Note we use the address operator & to write
+           // to the variables. Also the format of the string must match what
+           // is in the file
+           while (fscanf(fp, "%d,%f", &i, &value) != EOF) {
+               serial.printf("%d,%f\n",i,value);
+           }
+           serial.printf("Done.\n");
+           fclose(fp);  // ensure you close the file after reading
+       }
+    }
+    */
     while(1) {
 
         if (g_sw1_flag) {
@@ -123,71 +76,122 @@
             g_timer_flag_srf02 = 0;  // if it has, clear the flag
             if (units == METRIC) {
                 distance = srf02.getDistanceCm();
+                distance = distance-(offset*c);
             } else {
                 distance = srf02.getDistannceInch();
+                distance = distance -(offset*c);
             }
-            distance = distance-offset;
+
+
+
+
+
+
+
+
+            if (d == 9) {
+                d = 0;
+            } else {
+                d++;
+            }
+            myarray[d] = distance;
+            totaldistance = 0;
+            for (i = 0; i < 10; i++) {
+                totaldistance = totaldistance+myarray[i];
+            }
+            avgdistance = totaldistance;
         }
+
+
+
+
+
+
+
+
         setalert();
         lcdoutput();
-        //     setbuzzer();
+        setbuzzer();
         setleds();
         sleep();
     }
 }
 
-//Set up board switches and LEDS
-void init_K64F()
-{
-//on-board LEDs are active-low, so set pin high to turn them off.
-    r_led = 1;
-    g_led = 1;
-    b_led = 1;
-// since the on-board switches have external pull-ups, we should disable the internal pull-down
-// resistors that are enabled by default using InterruptIn
-    sw2.mode(PullNone);
-    sw1.mode(PullNone);
-}
-
 void lcdoutput()
 {
-    int length;
-    int length1;
-    char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
-    char buffer1[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
-    int length2;
-    char buffer2[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
-    int length3;
-    char buffer3[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
+    /**
+    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 alert == 0 no need to display range path is clear
     if (alert == 0) {
+        if (g_timer_flag_standby) {
+            g_timer_flag_standby = 0;
+            T = tmp102.get_temperature();
+            standby++;
+            if (standby >3) {
+                standby = 3;
+            }
+        }
+
+        switch (standby) {
+            case 1:
+                lcd.clear();
+                sprintf(buffer,"**PATH CLEAR**");
+                lcd.printString(buffer,0,0);
+                lcd.refresh();
+                break;
+            case 2:
+                lcd.clear();
+                sprintf(buffer3,"TEMP = %.2f",T);
+                sprintf(buffer2,"TEMPERATER");
+                lcd.printString(buffer3,4,3);
+                lcd.printString(buffer2,12,2);
+                break;
+            case 3:
+                lcd.clear();
+                sprintf(buffer3,"TEMP = %.2f",T);
+                sprintf(buffer2,"TEMPERATER");
+                lcd.refresh();
+                lcd.printString(buffer3,4,3);
+                lcd.printString(buffer2,12,2);
+                lcd.setBrightness(0);
+                break;
+        }
+    }
+
+    //If alert isn't 0 then the distance is to be dispayed alonng with the the distance bar
+    else {
+        lcd.setBrightness(bright);
+        standby =0;
         lcd.clear();
 
-        length1 = sprintf(buffer1,"**PATH CLEAR**",bright);
-        lcd.printString(buffer1,0,0);
-        lcd.refresh();
-
-    }
+        if (units == METRIC) {
+            sprintf(buffer,"%i Cm",distance);
+            sprintf(buffer1,"****RANGE!****");
+            sprintf(buffer2,"DISTANCE");
+            sprintf(buffer3,"%0.2f",avgdistance);
+            sprintf(buffer4,"Menu");
 
-    else {
-        lcd.clear();
-        if (units == METRIC) {
-            length = sprintf(buffer,"%i Cm",distance);
-            length1 = sprintf(buffer1,"****RANGE!****");
-            length2 = sprintf(buffer2,"***DISTANCE***");
-            length3 = sprintf(buffer3,"Alert = %i",alert);
         } else {
-            length = sprintf(buffer,"%i Inches",distance);
-            length1 = sprintf(buffer1,"****RANGE!****");
-            length2 = sprintf(buffer2,"***DISTANCE***");
+            sprintf(buffer,"%i Inches",distance);
+            sprintf(buffer1,"****RANGE!****");
+            sprintf(buffer2,"***DISTANCE***");
+            sprintf(buffer2,"Menu");
         }
-        lcd.printString(buffer,20,3);
+        lcd.printString(buffer,25,2);
         lcd.printString(buffer1,0,0);
-        lcd.printString(buffer2,0,1);
-        lcd.printString(buffer3,0,2);
-        float i = r7/84;
-        float distbar = distance*i;
+        lcd.printString(buffer2,16,1);
+        lcd.printString(buffer4,0,5);
+        lcd.printString(buffer3,0,3);
+
+        float i = (r7/84);
+        float distbar = (distance*i);
         //drawRect(int x0,int y0,int width,int height,int fill);
-        lcd.drawRect(0,38,distbar,7,1);
+        lcd.drawRect(0,29,distbar,7,1); //    MOVE BAR UP!!!!!!!!!!!!!!!!!!!!!!!!!
         lcd.refresh();
 
     }
@@ -196,29 +200,34 @@
 
 void setalert()
 {
-    if (distance >= r6 && distance < r7) {  // r6 150 and r7 200
-        alert = 1; //flash green
-    } else if (distance >= r5 && distance  < r6) {  // r5 90 and r6 150
-        alert = 2; //steady green
-    } else if (distance >= r4 && distance < r5) {  // r4 60 and r5 90
-        alert = 3; //flashing amber
-    } else if (distance >= r3 && distance < r4) {  // r3 40 and r4 60
-        alert = 4; //steady amber
-    } else if ( distance > r2 && distance < r3) {  // r2 20 and r3 40
-        alert = 5; //flashing red
-    } else if (distance > 1 && distance <= r2) { //r1 3 and r2 20
-        alert = 6; //steady red
-    } else if (distance <=1) {
-        alert = 7; //all flashing
+    /** A fuction used to determin the alert level given a range with the use of IF statments
+    @param distance The distance read from sensor
+    @param alert The level that distance falls with in 0 -7
+    @returns alert
+    */
+
+    if (distance >= (r6*c) && distance < (r7*c)) {  // r6 150 and r7 200
+        alert = 1; /// alert 1 distance between preset 150Cm to 200Cm
+    } else if (distance >= (r5*c) && distance  < (r6*c)) {
+        alert = 2; /// alert 2 when between preset 90Cm to 150Cm
+    } else if (distance >= (r4*c) && distance < (r5*c)) {
+        alert = 3; /// alert 3 when distance between 60Cm to 90Cm
+    } else if (distance >= (r3*c) && distance < (r4*c)) {
+        alert = 4; /// alert 4 when distance between 40Cm and 60Cm
+    } else if (distance >= (r2*c) && distance < (r3*c)) {
+        alert = 5; ///alert 5 when distance between 20Cm and 40m
+    } else if (distance >= (r1*c) && distance <= (r2*c)) { //r1 3 and r2 20
+        alert = 6; ///alert 6 when distance between 1 and 20
+    } else if (distance <=(r1*c)) {
+        alert = 7; ///alert 7 when distance below 1Cm
     } else {
-        alert = 0; //no output
+        alert = 0; /// alert 0 all else
     }
 }
 
 void setleds()
 {
-    // If statments to determin the output of each LED
-    if (Alertlevel[alert].frr_led and Alertlevel[alert].fgg_led and Alertlevel[alert].fa_led == HIGH) {
+    if (alert ==7) {
         if (g_timer_flag_led) {
             g_timer_flag_led = 0;  // if it has, clear the flag
             rr_led = !rr_led;
@@ -247,25 +256,39 @@
     }
 
 }
-/*
+
 void setbuzzer()
 {
+    int beat;
+    /**
+    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
+    */
     if (g_timer_flag_tone) {
-        g_timer_flag_tone = 0;
-        buzzer.period (1/1000);
-        buzzer=0.5;
-        buzzoff.attach(&nobuzz,Alertlevel[alert].toneon);
+        g_timer_flag_tone = 1;
+        if (beat == 10) {
+            beat = 0;
+        } else {
+            beat++;
+        }
+
+
+
+        if (Alertlevel[alert].toneon < beat) {
+            buzzer.period (1/10000);
+            buzzer = 0.5;
+        } else {
+            buzzer = 0.0;
+        }
+
+
     }
-}*/
-
-void nobuzz()
-{
-    buzzer = 0.0;
 }
 
 void backlight ()
 {
-
     if (bright == 1.0) {
         bright = 0;
     } else {
@@ -274,51 +297,43 @@
     lcd.setBrightness(bright);
 }
 
-
-
-
 void sw2_isr()
 {
-    g_sw2_flag = 1;   // set flag in 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
+    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
+    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
+    g_timer_flag_srf02 = 1;   /** set flag in ISR by ticker_srf02 @param g_timer_flag_srf02 0 or 1 */
 }
 
 void timer_isr_tone()
 {
-    g_timer_flag_tone = 1;
+    g_timer_flag_tone = 1; /** set flag in ISR by ticker_tone @param g_timer_flag_tone 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()
 {
-    int length2;
-    int length1;
-    int length3;
-    int length4;
-    char buffer2[14];
-    char buffer3[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
-    char buffer1[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
-    char buffer4[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14
-
     while(1) {
 
 
         if (g_sw1_flag) {
             g_sw1_flag = 0;
-            page++;
+            page++; /// Moves page
             lcd.clear();
         }
         switch (page) {
@@ -329,13 +344,15 @@
                     lcd.clear();
                 } else {
                     int lightbar = bright*84;
-                    length2 = sprintf(buffer2,"*****MENU*****");
-                    length1 = sprintf(buffer1,"BACKLIGHT");
-                    length3 = sprintf(buffer3,"%.0f%%",bright*100);
+                    sprintf(buffer2,"*****MENU*****");
+                    sprintf(buffer1,"BACKLIGHT");
+                    sprintf(buffer3,"%.0f%%",bright*100);
+                    sprintf(buffer4,"Next       Adj");
                     lcd.printString(buffer2,0,0);
                     lcd.printString(buffer1,0,1);
                     lcd.printString(buffer3,0,2);
-                    lcd.drawRect(0,38,lightbar,7,1);
+                    lcd.printString(buffer4,0,5);
+                    lcd.drawRect(0,26,lightbar,7,1);  // move bar up!!!!!!!!!!!!!!!!
                     lcd.refresh();
 
                 }
@@ -353,9 +370,11 @@
                     length2 = sprintf(buffer2,"*****MENU*****");
                     length1 = sprintf(buffer1,"OFFSET");
                     length3 = sprintf(buffer3,"%i",offset);
+                    sprintf(buffer4,"Next       Adj");
                     lcd.printString(buffer2,0,0);
                     lcd.printString(buffer1,0,1);
                     lcd.printString(buffer3,0,2);
+                    lcd.printString(buffer4,0,5);
                     lcd.refresh();
                 }
 
@@ -366,22 +385,26 @@
                     g_sw2_flag = 0;  // if it has, clear the flag
                     if (units == METRIC) {
                         units = IMPERIAL;
+                        c= 0.3937;
                     } else {
                         units = METRIC;
+                        c = 1;
                     }
                 }
                 if (units == METRIC) {
-                    length3 = sprintf(buffer3,"METRIC");
+                    sprintf(buffer3,"METRIC");
+                    lcd.printString(buffer3,0,2);
                 } else {
-                    length3 = sprintf(buffer3,"IMPERIAL");
+                    sprintf(buffer3,"IMPERIAL");
+                    lcd.printString(buffer3,0,2);
                 }
-
-                length2 = sprintf(buffer2,"*****MENU*****");
-                length1 = sprintf(buffer1,"UNITS");
+                sprintf(buffer2,"*****MENU*****");
+                sprintf(buffer1,"UNITS");
+                sprintf(buffer4,"Next       Adj");
                 lcd.printString(buffer2,0,0);
                 lcd.printString(buffer1,0,1);
-                lcd.printString(buffer3,0,2);
-
+                lcd.printString(buffer4,0,5);
+                lcd.refresh();
 
                 break;
 
@@ -394,36 +417,67 @@
                 length2 = sprintf(buffer3,"*****MENU*****");
                 length3 = sprintf(buffer1,"RANGE");
                 length3 = sprintf(buffer2,"PARAMETERS");
+                sprintf(buffer4,"Next       Adj");
                 lcd.printString(buffer3,0,0);
                 lcd.printString(buffer1,0,1);
                 lcd.printString(buffer2,0,2);
-
+                lcd.printString(buffer4,0,5);
 
                 break;
 
             default:
                 lcd.clear();
+                /*   //save();
+
+                ///////////////////// Writing list to file example //////////////////////
+
+                   // for this example, I'll create some numbers to write to file in a big list
+                   // a data logger for example will usually append to a file - at a reading
+                   // at the end rather than creating a new file
+                   fp = fopen("/sd/settinngs.txt", "a");
+
+                   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....");
+                       for(int i = 1; i <= 50; i++) {
+                           int r1;
+                           int r2;
+                           int r3;
+                           int r4;
+                           int r5;
+                           int r6;
+                           int r7;
+                           int offset; // create suitable variables to store the data in the file
+                           float bright;
+                           float dummy = 1000.0F/i;  // dummy variable
+                           fprintf(fp, "%d,%f\n",i,dummy);  // print formatted string to file (CSV)
+                       }
+                       serial.printf("Done.\n");
+                       fclose(fp);  // ensure you close the file after writing
+                   }
+
+                   // you can comment out the writing example to check that the writing has
+                   // worked - when you run it after commenting, it should still open the
+                   // file that exists on the SD card - assuming you didn't delete it!
+                */
                 return;
         }
     }
-    lcd.refresh();
+    // lcd.refresh();
+
 }
 void submenu()
 {
-    int length2;
-    int length1;
-    int length3;
-    int length4;
-    char buffer2[14];
-    char buffer3[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
-    char buffer1[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
-    char buffer4[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14
+
     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
@@ -437,10 +491,12 @@
                 length3 = sprintf(buffer2,"RANGE");
                 length3 = sprintf(buffer3,"PARAMETERS");
                 length4 = 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(buffer4,0,3);
+                lcd.printString(buffer,0,5);
                 break;
 
             case 1:
@@ -454,14 +510,16 @@
                     }
                 }
 
-                length2 = sprintf(buffer1,"*****MENU*****");
-                length3 = sprintf(buffer2,"RANGE");
-                length3 = sprintf(buffer3,"PARAMETERS");
-                length3 = sprintf(buffer4,"%iCm  to  %iCm",r2,r3);
+                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(buffer4,0,3);
+                lcd.printString(buffer,0,5);
                 break;
 
             case 2:
@@ -475,14 +533,16 @@
                     }
                 }
 
-                length2 = sprintf(buffer1,"*****MENU*****");
-                length3 = sprintf(buffer2,"RANGE");
-                length3 = sprintf(buffer3,"PARAMETERS");
-                length3 = sprintf(buffer4,"%iCm  to  %iCm",r3,r4);
+                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(buffer4,0,3);
+                lcd.printString(buffer,0,5);
                 break;
 
             case 3:
@@ -496,14 +556,16 @@
                     }
                 }
 
-                length2 = sprintf(buffer1,"*****MENU*****");
-                length3 = sprintf(buffer2,"RANGE");
-                length3 = sprintf(buffer3,"PARAMETERS");
-                length3 = sprintf(buffer4,"%iCm to %iCm",r4,r5);
+                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);
                 lcd.printString(buffer4,0,3);
+                lcd.printString(buffer,0,5);
                 break;
             case 4:
 
@@ -520,10 +582,12 @@
                 length3 = sprintf(buffer2,"RANGE");
                 length3 = sprintf(buffer3,"PARAMETERS");
                 length3 = 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(buffer4,0,3);
+                lcd.printString(buffer,0,5);
                 break;
             case 5:
 
@@ -539,16 +603,39 @@
                 length2 = sprintf(buffer1,"*****MENU*****");
                 length3 = sprintf(buffer2,"RANGE");
                 length3 = sprintf(buffer3,"PARAMETERS");
+                sprintf(buffer,"Next       Adj");
                 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);
+                lcd.printString(buffer,0,5);
                 break;
 
             default:
                 lcd.clear();
+                //  save();
                 return;
         }
     }
+}
+void setup()
+{
+    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_tone.attach(&timer_isr_tone,1); /// Attach the ticker which controls the off time of the buzzer 0.1 = 10 beats per second
+    ticker_standby.attach(&timer_isr_standby,3.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
+
+    /**Set up LEDs switches and LEDS
+    @param r_led 1 or 0
+    @param g_led 1 or 0
+    @param b_led 1 or 0
+    */
+    rr_led = 0;
+    a_led = 0;
+    gg_led = 0;
+    sw2.mode(PullDown);
+    sw1.mode(PullDown);
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Mon Apr 25 10:08:05 2016 +0000
@@ -0,0 +1,489 @@
+/**
+@file main.h
+@brief Header file containing functions prototypes, defines and global variables.
+@brief Ranger Project
+@brief Revision 1.0.
+@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
+create a programe that can read a distance from an SRF02 sensor and then based aponn the reading disply the distance on the screen
+ and increment the LEDs and buzzer according the provision of a temperature sensor is also provied as a secondary function when no
+ object is detected with in range.
+*/
+
+#ifndef MAIN_H
+#define MAIN_H
+#include "mbed.h"
+#include "mbed.h"
+#include "SRF02.h"
+#include "N5110.h"
+#include "TMP102.h"
+#include "SDFileSystem.h"
+
+#define LOW 0 /// No output
+#define HIGH 1 /// High output
+#define METRIC 1
+#define IMPERIAL 0
+
+
+/**
+@namespace LEDs
+@brief Output for Alert LEDs
+*/
+/** Red LED connect to pin PTA1*/
+DigitalOut rr_led (PTA1);
+/** amber LED connect to pin PTC2*/
+DigitalOut a_led (PTC2);
+/** Green LED connect to pin PTB23*/
+DigitalOut gg_led(PTB23);
+
+/**
+@namespace BOARDLEDs
+@brief On board LEDs
+*/
+DigitalOut r_led(LED_RED);
+DigitalOut g_led(LED_GREEN);
+DigitalOut b_led(LED_BLUE);
+
+/**
+@namespace  Buzzer
+@brief PWM output for Buzzer
+*/
+PwmOut buzzer(PTA2);
+
+/**
+@namespace  Buttons
+@brief Button triggered Interrupts
+*/
+InterruptIn sw1(PTB19);
+InterruptIn sw2(PTB18);
+
+/**
+@namespace  Timers
+@brief Tickers and time outs
+*/
+Ticker ticker;
+Ticker ticker_srf02;
+Ticker ticker_tone;
+Ticker ticker_standby;
+Timeout buzzoff;
+
+// Create TMP102 object
+TMP102 tmp102(I2C_SDA,I2C_SCL);
+
+/**
+@namespace  Ranger
+@brief Creat the Ranger object
+*/
+SRF02 srf02(I2C_SDA,I2C_SCL);
+
+/**
+@namespace  LCD
+@brief Creats the LCD object
+*/
+N5110           lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);
+
+/// Connections to SD card holder on K64F (SPI interface)
+SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
+
+/**
+@namespace  TimerFlags
+@brief Flags for use with timed interupts
+*/
+volatile int g_timer_flag_led = 0, g_timer_flag_srf02 = 0,  g_timer_flag_tone = 0; /** Flag rised by interupts*/
+volatile int g_timer_flag_standby = 0;
+/**
+@namespace  ButtonFlages
+@brief Flags for use with button interupts
+*/
+volatile int g_sw1_flag = 0, g_sw2_flag = 0;
+
+/**
+@namespace  RangePresets
+@brief Preset range of the differant alert levels
+*/
+int r1 = 3,/*!< Upper limit of alert 1 */r2 = 20,/*!< Upper limit of alert 2 */r3 = 40,/*!< Upper limit of alert 3 */r4 = 60,/*!< Upper limit of alert 4 */r5 = 80,/*!< Upper limit of alert 5 */r6 = 100,/*!< Upper limit of alert 6 */r7 = 120; /*!< Upper limit of alert 7 */
+
+FILE *fp;
+
+int myarray[9];/*!< array to hold last ten range readings  */
+int d =0;
+int t;
+int i;
+float avgdistance;
+int totaldistance;
+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;/*!< veriable to hold the current LED backlight of the 5110 LCD */
+char units = METRIC;/*!< 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]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
+float T;
+int standby =1;
+float c =1; /// convertion factor from Cm to inch
+
+
+struct Alertlevel {
+    char srr_led;  /// stead RED LED state
+    char sa_led;    /// stead AMBER LED state
+    char sgg_led;  /// stead GREEN LED state
+    char frr_led;///FLASHING RED LED state
+    char fa_led; ///FLASHING AMBER LED state
+    char fgg_led; ///FLASHING GREEN LED state
+    int toneon;
+    int toneoff;
+ }; /*!< Stucture to hold all outputs. Steady LEDs, Flashing LEDs, Tone on, Tonne off*/
+typedef const struct Alertlevel STyp;
+
+STyp Alertlevel[8] = {
+    {LOW,LOW,LOW,LOW,LOW,LOW,0,1}, // no output
+    {LOW,LOW,LOW,LOW,LOW,HIGH,1,8}, //flash green
+    {LOW,LOW,HIGH,LOW,LOW,LOW,2,7}, //steady green
+    {LOW,LOW,HIGH,LOW,HIGH,LOW,4,5}, //flash amber
+    {LOW,HIGH,HIGH,LOW,LOW,LOW,6,3}, //steady amber
+    {LOW,HIGH,HIGH,HIGH,LOW,LOW,8,1}, //flash red
+    {HIGH,HIGH,HIGH,LOW,LOW,LOW,9,0},// steady red
+    {LOW,LOW,LOW,HIGH,HIGH,HIGH,9,0} // all flash
+};/*!< Array contaning structures for diffent outputs */
+
+
+void lcdoutput();
+void timer_isr_led();
+void timer_isr_srf02();
+void timer_isr_tone();
+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
+@returns lcd.setbrightness
+@code
+if (bright == 1.0) {
+    bright = 0;
+} else {
+    bright += 0.2;
+}
+lcd.setBrightness(bright);
+@endcode
+*/
+void backlight();
+void init_K64F();
+void sw2_isr();
+void sw1_isr();
+void setup();
+
+/** A fuction used to determin the alert level given a range with the use of IF statments
+@param distance The distance read from sensor
+@param alert The level that distance falls with in 0 -7
+@returns alert
+@code
+if (distance >= r6 && distance < r7) {  // r6 150 and r7 200
+    alert = 1; /// alert 1 distance between preset 150Cm to 200Cm
+} else if (distance >= r5 && distance  < r6) {
+    alert = 2; /// alert 2 when between preset 90Cm to 150Cm
+} else if (distance >= r4 && distance < r5) {
+    alert = 3; /// alert 3 when distance between 60Cm to 90Cm
+} else if (distance >= r3 && distance < r4) {
+    alert = 4; /// alert 4 when distance between 40Cm and 60Cm
+} else if ( distance > r2 && distance < r3) {
+    alert = 5; ///alert 5 when distance between 20Cm and 40m
+} else if (distance > r1 && distance <= r2) { //r1 3 and r2 20
+    alert = 6; ///alert 6 when distance between 1 and 20
+} else if (distance <=r1) {
+    alert = 7; ///alert 7 when distance below 1Cm
+} else {
+    alert = 0; /// alert 0 all else
+}
+}
+@endcode
+*/
+void setalert();
+/** If statments to determine the output of each LED by inspecting the struct in the relevent element of the alertlevel array
+
+@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;
+   }
+
+}
+@endcode*/
+void setleds();
+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) {
+
+
+        if (g_sw1_flag) {
+            g_sw1_flag = 0;
+            page++; /// Moves page
+            lcd.clear();
+        }
+        switch (page) {
+            case 0:
+                if (g_sw2_flag) {
+                    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();
+
+                }
+                break;
+
+            case 1:
+                if (g_sw2_flag) {
+                    g_sw2_flag = 0;  // if it has, clear the flag
+                    if (offset == 20) {
+                        offset = 0;
+                    } 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();
+                }
+
+                break;
+
+            case 2:
+                if (g_sw2_flag) {
+                    g_sw2_flag = 0;  // if it has, clear the flag
+                    if (units == METRIC) {
+                        units = IMPERIAL;
+                    } else {
+                        units = METRIC;
+                    }
+                }
+                if (units == METRIC) {
+                    sprintf(buffer3,"METRIC");
+                    lcd.printString(buffer3,0,2);
+                } else {
+                    sprintf(buffer3,"IMPERIAL");
+                    lcd.printString(buffer3,0,2);
+                }
+
+                sprintf(buffer2,"*****MENU*****");
+                sprintf(buffer1,"UNITS");
+                lcd.printString(buffer2,0,0);
+                lcd.printString(buffer1,0,1);
+                lcd.refresh();
+
+                break;
+
+            case 3:
+
+                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);
+
+
+                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;
+                    }
+                }
+
+                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;
+        }
+    }
+    }
+    @endcode
+    */
+    void submenu();
+    void save();
+    void load();
+    void standby1();
+
+#endif
\ No newline at end of file