A program designed to get the distance from an SRF02 distance sensor and create an audible and visual indication of that distance with data logging capabilities.

Dependencies:   N5110 PowerControl SRF02 mbed

Revision:
1:387d5e6fa05f
Parent:
0:7d01de89a1ff
Child:
2:ebcfecb68cca
--- a/main.cpp	Wed Apr 08 16:57:08 2015 +0000
+++ b/main.cpp	Wed Apr 08 18:41:31 2015 +0000
@@ -1,54 +1,9 @@
 // Distance Sensor Project
 // Main File "main.cpp"
 // Designed By Sam Russell (200773195)
-// Date: 07/04/2015 || Version: 0.1
-
-#include "mbed.h"
-
-DigitalOut myled(LED1);
-
-int main() {
-    pc.baud(9600); //setting the baud rate
-    timer.attach(&timerExpired,1);
-    display.init(); // Initialise the display.
-    display.clear(); // Clears the starting pattern from the screen.
-    display.printString("--++--++--++--",0,0);     //Print string of "" at x,y locations.
-    display.printString("Distance",18,1);
-    display.printString("Sensor",22,2);
-    display.printString("--++--++--++--",0,3);
-    display.printString("Sam Russell",10,4);
-    display.printString("--++--++--++--",0,5);
-    wait(2); //Delay between the introduction and the begining of the game.
-    display.clear(); //Clears the display.
-    drawRect(int 0,int 0,int 80,int 20,int 0)
-    }
-}
+// Date: 07/04/2015 || Version: 0.1b
 
-while(1) {
-        if(timerflag) {
-            timerflag = 0;
-            ///Read sensor distance in cm and print to the serial port.
-            distance = avgDist();
-            pc.printf("Distance = %.2f cm\n",distance);
-            if(distance<75) {
-                leds = 15;
-            } else if(distance<150) {
-                leds = 7;
-            } else if(distance<275) {
-                leds = 3;
-            } else {
-                leds = 1;
-            }
-        }
-
-    }
-}
-
-//When timer expires set flag to equal 1.
-void timerExpired()
-{
-    timerflag = 1;
-}
+#include "main.h"
 
 void error(int code)
 //ERROR CODE
@@ -61,6 +16,40 @@
     }
 }
 
+void setTime()
+{
+// print time for debugging
+    pc.printf("set_time - %s",rxString);
+// atoi() converts a string to an integer
+    int time = atoi(rxString);
+// update the time
+    set_time(time);
+}
+
+void serialISR()
+{
+// when a serial interrupt occurs, read rx string into buffer
+    pc.gets(rxString,16);
+// set flag
+    setTimeFlag = 1;
+}
+
+void writeDataToFile(char* data, float dataDistance)
+{
+        logLED = 1; // turn on LEDs for feedback
+        pc.printf("Data Logged\n"); //DEBUG MESSAGE
+        FILE *fp = fopen("/local/log.csv", "a"); // open 'log.txt' for appending
+// if the file doesn't exist it is created, if it exists, data is appended to the end
+        fprintf(fp,"%s %.2f\n",data,dataDistance); // print string to file
+        fclose(fp); // close file
+}
+
+//When timer expires set flag to equal 1.
+void timerExpired()
+{
+    timerflag = 1;
+}
+
 float avgDist()
 //Read the distance from the sensor every 10ms.
 //After 10 readings calculate the average and define that as the total distance.
@@ -88,3 +77,61 @@
     return distance;
 }
 
+int main()
+{
+    pc.baud(9600); //setting the baud rate
+    timer.attach(&timerExpired,1);
+    display.init(); // Initialise the display.
+    display.clear(); // Clears the starting pattern from the screen.
+    display.printString("--++--++--++--",0,0);     //Print string of "" at x,y locations.
+    display.printString("Distance",18,1);
+    display.printString("Sensor",22,2);
+    display.printString("--++--++--++--",0,3);
+    display.printString("Sam Russell",10,4);
+    display.printString("--++--++--++--",0,5);
+    wait(2); //Delay between the introduction and the begining of the game.
+    display.clear(); //Clears the display.
+    display.drawRect(0,0,82,20,0); //Draw initial bar graph border.
+    pc.attach(&serialISR); // attach serial ISR
+    char buffer[30]; // buffer used to store time string
+    set_time(0); // initialise time to 1st January 1970
+    
+    while(1) {
+        if(timerflag) {
+            timerflag = 0;
+            ///Read sensor distance in cm and print to the serial port.
+            distance = avgDist(); //Set the distance variable to the averaged value from the sensor.
+            time_t seconds = time(NULL); // get current time
+            // format time into a string (time and date)
+            strftime(buffer, 30 , "%R %d/%m/%y", localtime(&seconds));
+            // print over serial
+            pc.printf("%s , %.2f cm\n",buffer,distance); //print the temperature value and the date/time to the serial.
+            float L = Switch.read();
+            pc.printf("L = %.2f\n", L);
+            if(L>0.9) {
+                //When switch is on, save the data to file and turn indicator on.
+                writeDataToFile(buffer,distance);
+                pc.printf("Logging Data\n"); //DEBUG MESSAGE
+            }
+            else {
+               //When switch is off, don't save the data to file and turn indictor off.
+             logLED = 0;
+             pc.printf("Not Logging Data\n");
+            }
+
+            if(distance<75) {
+                leds = 15;
+            } else if(distance<150) {
+                leds = 7;
+            } else if(distance<275) {
+                leds = 3;
+            } else {
+                leds = 1;
+            }
+        }
+        if (setTimeFlag) { // if updated time has been sent
+        setTimeFlag = 0; // clear flag
+        setTime(); // update time
+        }
+    }
+}