Jakobi Blackburn / Mbed 2 deprecated UltrasonicDistanceSensor-el13jb

Dependencies:   N5110WN PowerControl SRF02 mbed

Revision:
1:7f151ab172cb
Parent:
0:73bfbe8729d4
Child:
2:a1eaa4d74b63
--- a/main.cpp	Fri Mar 13 09:28:37 2015 +0000
+++ b/main.cpp	Fri Mar 20 11:46:08 2015 +0000
@@ -1,68 +1,43 @@
-#include "mbed.h"
-#include "PowerControl/PowerControl.h"
-#include "PowerControl/EthernetPowerControl.h"
-#include "Speaker.h"
-#include "N5110.h"
-#include "SRF02/SRF02.h"
-
-// Inputs
-InterruptIn UnitTog(p14);
-AnalogIn LCDTog(p15);
-InterruptIn LogTog(p16);
-InterruptIn VisTog(p17);
-AnalogIn BuzVol(p19);
-AnalogIn BLEDLevel(p20);
-SRF02 sensor(p28,p27);//SDA SCL
-
-// Outputs
-//     VCC,SCE,RST,D/C,MOSI,SCLK,LED
-N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
-Speaker Buzzer(p18);
-PwmOut BLED(p26);
-DigitalOut WLED(p21);
-Serial pc(USBTX,USBRX);// the tx and rx respectively
-BusOut leds(LED4,LED3,LED2,LED1);
+/**
+@file main.cpp
 
-float scanSpeed=1; //float that changes the speed that the distance is taken
-float volume=1; //float used to vary the volume
-int distance; //integer that stores the distance
-float unitX=1; // if 1 in cm, 0.01 = m, 10 = mm
-int timerFlag=0;
-int U=0;  // if 0 cm, 1 is m, 2 is mm
-string units;
+@brief Program implementation
 
-Ticker timer;
-//functions
-void warnings();    // deals with the visual and audiable alerts.
-void getDistance(); // takes 10 distances and averages them.
-void unitToggle();  // changes the units cm/m/mm
-void timerExpired();// timers ISR
-void setScanSpeed(int dist); // sets the speed of the scanning, dependant on the distance
-
+*/
+#include "main.h"
 
-struct State {
- float unitMultiple; // unit mulitple
-  Unit; // unit
- int nextState[2]; // array of next states
-};
-typedef const struct State STyp;
-STyp fsm[3] = {
- {1,"cm",{0,1}}, // State 0 
- {0.01,"m",{1,2}}, // 
- {10,"mm",{2,0}}
- }; 
-int state=0;
 
 
 int main() {
-    timer.attach(&timerExpired,scanSpeed);  
+    pc.baud(9600);  //sets baud rate
+    timer.attach(&timerExpired,0.5);
+    LogTog.rise(&logButtonPressed);
+    UnitTog.rise(&unitToggle);
+    pc.attach(&serialISR); // attach serial ISR
+    
+    char buffer[30]; // buffer used to store time string
+    set_time(1427846400); // initialise time to 1st April 2015 00:00:00
+  
     while(1) {
-     //timer.attach(&timerExpired,scanSpeed);
         if(timerFlag){
-            timerFlag=0;
-            unitToggle();
-            getDistance();
+            
+            timerFlag = 0; //resets flag
+            time_t seconds = time(NULL); // get current time
+            // format time into a string (time and date)
+            strftime(buffer, 30 , "%d/%m/%y %R", localtime(&seconds));
+            float dist = getDistance(); //reads the temperature and sets it to a float value.
+            //prints the value to the serial port.
+            if(unitX==0.01){pc.printf("@ %s , Distance = %.2f m \n",buffer,dist*unitX);}//prints the value to the serial port.
+            else if(unitX==1){pc.printf("@ %s , Distance = %.0f cm \n",buffer,dist*unitX);}//prints the value to the serial port.
+            else if(unitX==10){pc.printf("@ %s , Distance = %.0f mm \n",buffer,dist*unitX);}//prints the value to the serial port.
+            leds=15;
+            logging(buffer,dist);
+            leds=0;
          }
+          if (setTimeFlag) { // if updated time has been sent
+            setTimeFlag = 0; // clear flag
+            setTime(); // update time
+        }
     }
 }
 
@@ -77,60 +52,107 @@
     Buzzer.PlayNote(800.0, 0.1, volume);
     WLED=0;   
 }
-void getDistance(){
+float getDistance(){
     int dist0 = sensor.getDistanceCm();
-    wait(0.5*scanSpeed);
+    wait(0.1*scanSpeed);
     int dist1 = sensor.getDistanceCm();
-    wait(0.5*scanSpeed);
+    wait(0.1*scanSpeed);
     int dist2 = sensor.getDistanceCm();
-    wait(0.5*scanSpeed);
+    wait(0.1*scanSpeed);
     int dist3 = sensor.getDistanceCm();
-    wait(0.5*scanSpeed);
+    wait(0.1*scanSpeed);
     int dist4 = sensor.getDistanceCm();
-    wait(0.5*scanSpeed);
+    wait(0.1*scanSpeed);
     int dist5 = sensor.getDistanceCm();
-    wait(0.5*scanSpeed);
+    wait(0.1*scanSpeed);
     int dist6 = sensor.getDistanceCm();
-    wait(0.5*scanSpeed);
+    wait(0.1*scanSpeed);
     int dist7 = sensor.getDistanceCm();
-    wait(0.5*scanSpeed);
+    wait(0.1*scanSpeed);
     int dist8 = sensor.getDistanceCm();
-    wait(0.5*scanSpeed);
+    wait(0.1*scanSpeed);
     int dist9 = sensor.getDistanceCm();
-    distance=(dist0+dist1+dist2+dist3+dist4+dist5+dist6+dist7+dist8+dist9)/10;
-    setScanSpeed(distance);
-    
-    pc.printf("Distance = %d %a\n",distance*unitX,units);
+    distance=(dist0+dist1+dist2+dist3+dist4+dist5+dist6+dist7+dist8+dist9)/10; 
+    setScanSpeed();
     warnings();
+    return distance;
 }
 
 void unitToggle(){
+    state = fsm[state].nextState[UnitTog]; // read input and update current state
     unitX = fsm[state].unitMultiple; // set output depending on current state
-    units= fsm[state].Unit; // wait in that state for desired time
-    state = fsm[state].nextState[UnitTog]; // read input (BusIn) and update current state
-    //U++;
-   // if(U==3){U=0;}
-   // if(U==0){unitX=1;units="cm";}
-   // if(U==1){unitX=0.01;units="m";}
-   // if(U==2){unitX=10;units="mm";}
-   // pc.printf("Units = %s \n",units);   
+//    units= fsm[state].Unit; // 
+    wait(0.2);
+    pc.printf("unit multiple = %.2f \n",unitX);
+   // pc.printf("Units = %s \n",units);
+       
+}
+
+void setScanSpeed(){
+    if(distance>=100){
+        scanSpeed=2; // sets the speed to normal    
+    }
+    else if(distance>=25){
+        scanSpeed=1; // double speed        
+    }
+    else if(distance<25){
+        scanSpeed=0.25; // quad speed 
+    }
+    else error(1);
+    
+    pc.printf("Scan Speed = %.2f/times a second \n\n",(1/scanSpeed));
+}
+void error(int E){
+    while(1){
+        leds=0;
+        wait(0.2);
+        leds=E;
+        wait(0.2);
+    }
 }
 
-void setScanSpeed(int dist){
-    if(dist>=100){
-        scanSpeed=1/0.5; // sets the speed to every 2 seconds    
-    }
-    if(dist>=50){
-        scanSpeed=1/1; // sets the speed to every second    
+void logButtonPressed()
+{
+    logButtonFlag = !logButtonFlag; //flips the flag
+    if(logButtonFlag) {
+        LLED=1; // Logging LED will be on if the device is logging
+        pc.printf("-------\n Data Logging\n\r");
+    } else {
+        LLED=0; // Logging LED will be off if the device isn't logging
+        pc.printf("-------\n Stopped Logging Data\n\r");
     }
-    if(dist>=25){
-        scanSpeed=1/2; // sets the speed to 2 times a second        
-    }
-    if(dist>=10){
-        scanSpeed=1/4; // sets the speed to 4 times a second    
+    wait(0.2);
+}
+
+void logging(char* data,float data1)
+{
+    if(logButtonFlag) {
+        FILE *fp = fopen("/local/log.csv", "a"); // open 'log.csv' 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,data1); // print string to file
+        fclose(fp); // close file
+        pc.printf("Data Logged\n\r");
+        LLED=1; //keeps the led on if it is logging
+    } else {
+        pc.printf("WARNING : Unlogged Data\n\r");
+        LLED=0;
     }
-    if(dist<10){
-        scanSpeed=1/8; // sets the speed to 8 times a second    
-    }
-    pc.printf("Scan Speed = %d times/sec \n",(1/scanSpeed));
+}
+
+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);
+     pc.printf("set time - %d",time);
+}
+void serialISR()
+{
+// when a serial interrupt occurs, read rx string into buffer
+    pc.gets(rxString,16);
+// set flag
+    setTimeFlag = 1;
 }
\ No newline at end of file