Jakobi Blackburn / Mbed 2 deprecated UltrasonicDistanceSensor-el13jb

Dependencies:   N5110WN PowerControl SRF02 mbed

Revision:
3:00933efbe463
Parent:
2:a1eaa4d74b63
Child:
4:2e9aa626a02c
--- a/main.cpp	Fri Mar 20 17:25:39 2015 +0000
+++ b/main.cpp	Sat Mar 21 19:14:04 2015 +0000
@@ -1,6 +1,5 @@
 /**
 @file main.cpp
-
 @brief Program implementation
 
 */
@@ -8,47 +7,62 @@
 
 
 
-int main() {
+int main()
+{
     pc.baud(9600);  //sets baud rate
     timer.attach(&timerExpired,0.5);
     LogTog.rise(&logButtonPressed);
     UnitTog.rise(&unitToggle);
+    VisTog.rise(&visToggle);
     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
-    
+
     BLED.period(0.02);  //sets the frequency of the Backlight at 50Hz
+    BLED=BLEDLevel.read();  //sets the brightness
     lcd.init(); //to initilize the display
     checkerBoard();
     wait(0.2);
     lcd.clear();
-    
+
     introTune(); // plays the intro tune
     lcd.printString("Parking",20,0);
-    lcd.printString("Sensor",6,1);
+    lcd.printString("Sensor",23,1);
     lcd.printString("by",35,2);
     lcd.printString("Jakobi",24,3);
     lcd.printString("Blackburn",16,4);
-    wait(0.2);
+    wait(2);
     lcd.clear();
     while(1) {
-        if(timerFlag){
-            
+        BLED=BLEDLevel.read();  //sets the brightness
+        if(timerFlag) {
+
             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 distance and sets it to a float value.
+            float dist = getDistance()*unitX; //reads the distance and sets it to a float value.
             //prints the value to the serial port.
-            if(state==1){pc.printf("%s , Distance = %.2f %s \n",buffer,dist*unitX,units);}//prints the value to the serial port.
-            else if(state !=1){pc.printf("%s , Distance = %.0f %s \n",buffer,dist*unitX,units);}//prints the value to the serial port.
-           
-            leds=15;
-            logging(buffer,dist);
-            leds=0;
-         }
-          if (setTimeFlag) { // if updated time has been sent
+            if(state==1) {
+                pc.printf("%s , Distance = %.2f %s \n",buffer,dist,units);   //prints the value to the serial port.
+            } 
+            else if(state !=1) {
+                pc.printf("%s , Distance = %.0f %s \n",buffer,dist,units);   //prints the value to the serial port.
+            }
+            logging(buffer,distance);
+            if(visual==0) {
+                LCDVis0();
+            } 
+            else if(visual==1) {
+                LCDVis1();
+            } 
+            else if(visual==2) {
+                LCDVis2();
+            }
+            else{ error(2);}
+
+        }
+        if (setTimeFlag) { // if updated time has been sent
             setTimeFlag = 0; // clear flag
             setTime(); // update time
         }
@@ -56,18 +70,21 @@
     }
 }
 
-void timerExpired(){
-    timerFlag=1;   
+void timerExpired()
+{
+    timerFlag=1;
 }
 
 
-void warnings(){
+void warnings()
+{
     WLED=1;
     volume= BuzVol.read();
-    Buzzer.PlayNote(800.0, 0.1, volume);
-    WLED=0;   
+    Buzzer.PlayNote(1000.0, 0.1, volume);
+    WLED=0;
 }
-float getDistance(){
+float getDistance()
+{
     int dist0 = sensor.getDistanceCm();
     wait(0.1*scanSpeed);
     int dist1 = sensor.getDistanceCm();
@@ -87,38 +104,35 @@
     int dist8 = sensor.getDistanceCm();
     wait(0.1*scanSpeed);
     int dist9 = sensor.getDistanceCm();
-    distance=(dist0+dist1+dist2+dist3+dist4+dist5+dist6+dist7+dist8+dist9)/10; 
+    distance=(dist0+dist1+dist2+dist3+dist4+dist5+dist6+dist7+dist8+dist9)/10;
     setScanSpeed();
     warnings();
     return distance;
 }
 
-void unitToggle(){
+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; // 
+    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 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);}
 }
-void error(int E){
-    while(1){
+void error(int E) //up to 14. IF 15 shown it is a ack bit error for the SRF02
+{
+    while(1) {
         leds=0;
         wait(0.2);
         leds=E;
@@ -162,7 +176,7 @@
     int time = atoi(rxString);
 // update the time
     set_time(time);
-     pc.printf("set time - %d",time);
+    pc.printf("set time - %d",time);
 }
 void serialISR()
 {
@@ -197,4 +211,56 @@
         }
     }
     lcd.refresh();
+}
+
+void LCDVis0()    //the max range is 249 (according to the data sheet) and there are 84 pixels wide on the LCD so this creates the amount of cm for each pixel
+{
+    lcd.clear();
+    int I = distance*0.337;  //and then multiplies the current distance by that value as (84/249) =0.337...
+     pc.printf("bar width - %d pixels \n",I);
+    for(int j=0; j<48; j++) {
+        for(int i=0; i<I; i++) {
+            lcd.setPixel(i,j);
+        }
+    }
+    lcd.refresh();
+}
+
+void LCDVis1()  //the max range is 249 (according to the data sheet) and there are 48 pixels high on the LCD so this creates the amount of cm for each pixel
+{
+    lcd.clear();
+    int J = distance*0.19;  //and then multiplies the current distance by that value as (48/249) = 0.19..
+     pc.printf("bar height - %d pixels \n",J);
+    for(int i=0; i<84; i++) {
+        for(int j=0; j<J; j++) {
+            lcd.setPixel(i,48-j); //using 48-j here makes the bar fill from the bottom rather than from the top as pixel(0,0) is the top left corner
+           
+        }
+    }
+    lcd.refresh();
+}
+
+void LCDVis2(){
+    for(int i=0; i<84; i++) {
+        for(int j=0; j<48; j++) {
+            lcd.setPixel(i,j);
+        }
+    }
+    lcd.refresh();
+}
+
+void visToggle()
+{
+    Vstate = Vfsm[Vstate].nextState[VisTog]; // read input and update current state
+    visual = Vfsm[Vstate].visual; // set output depending on current state
+    wait(0.2);
+    if(visual==0) { 
+        pc.printf("----Left to Right----\n\r");
+    }
+    if(visual==1) {
+        pc.printf("----Up to Down----\n\r");
+    }
+    if(visual==2) {
+        pc.printf("----Other----\n\r");
+    }
 }
\ No newline at end of file