The Nucleo Distance Calculator Simple code to use HCSR04 and nokia 5110 to display distance and ring a buzzer when the distance is lower than a certain threshold.

Dependencies:   N5110 NOKIA_5110 mbed

Fork of UltraTest by stephen smitherman

Files at this revision

API Documentation at this revision

Comitter:
loarri
Date:
Mon Nov 30 15:44:40 2015 +0000
Parent:
0:f91b4b2acbe1
Commit message:
The Nucleo Distance Calculator.; First release

Changed in this revision

HCSR04.lib Show annotated file Show diff for this revision Revisions of this file
N5110.lib Show annotated file Show diff for this revision Revisions of this file
NOKIA_5110.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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r f91b4b2acbe1 -r 56e6200a897b HCSR04.lib
--- a/HCSR04.lib	Tue Mar 11 21:20:40 2014 +0000
+++ b/HCSR04.lib	Mon Nov 30 15:44:40 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/srsmitherman/code/HCSR04/#3ebde19131af
+https://developer.mbed.org/users/loarri/code/HCSR04_la/#1a68890774f3
diff -r f91b4b2acbe1 -r 56e6200a897b N5110.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/N5110.lib	Mon Nov 30 15:44:40 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/eencae/code/N5110/#ba8addc061ea
diff -r f91b4b2acbe1 -r 56e6200a897b NOKIA_5110.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NOKIA_5110.lib	Mon Nov 30 15:44:40 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Fuzball/code/NOKIA_5110/#9cfce382e741
diff -r f91b4b2acbe1 -r 56e6200a897b main.cpp
--- a/main.cpp	Tue Mar 11 21:20:40 2014 +0000
+++ b/main.cpp	Mon Nov 30 15:44:40 2015 +0000
@@ -1,18 +1,96 @@
+//==================================
+//    The Nucleo Distance Calculator
+//        v. 1.0
+//==================================
+//    by Lorenzo Arrigoni 
+//    laelectronics@libero.it
+//----------------------------------
+//     November 2015
+////////////////////////////////////////////
+// This is a simple that measures the distance
+// and signals in video and with a buzzer if
+// the measured distance is below a certain threshold.
+//////////////////////////////////////////
+
 #include "mbed.h"
 #include "HCSR04.h"
+#include "NOKIA_5110.h" 
 
-DigitalOut myled(LED1);
-Serial pc(USBTX,USBRX);
-HCSR04 ultra(p12,p11);
+
+DigitalOut buzzer(D7) ;
+HCSR04 ultra(D3,D4);   //pin trigger, pin echo
+
+//treshold for buzzer
+#define treshold 5 // cm
+
+int start_position =0.5 ;
+int current_position = start_position ;
+
+// swith on/off for bluetooth
+#define BLUE 0
+
+// debug mode
+#define DEBUG 1
+
+#if DEBUG == 0
+//#define PC_DEBUG(args...) pc.printf(args)
+#else
+//#define PC_DEBUG(args...)
+#endif
+
+#if BLUE == 1
+Serial pc(D8,D2);
+#else
+Serial pc(USBTX, USBRX);
+#endif
+
+
+
 
 float range=0;
+//char gg ;
+
+void blink_buzz(DigitalOut name, float time)
+{
+   name = 1 ;
+   wait_ms(time); 
+   name = 0 ; 
+}
 
 int main() {
     
-        
-    pc.printf("Program Start\n");    
+    // initialize 
+    buzzer = 0 ; 
+    
+    LcdPins myPins;
+    myPins.sce  = D8;
+    myPins.rst  = D9;
+    myPins.dc   = D10;
+    myPins.mosi = D11;      //SPI_MOSI;
+    myPins.miso = NC;
+    myPins.sclk = D13;      //SPI_SCK;
+    NokiaLcd myLcd( myPins );
+    
+     
+    /// LCD initialization
+    myLcd.InitLcd();        // LCD init
+    myLcd.DrawString("Loarri - 2015");
+    myLcd.SetXY(0,2); 
+    myLcd.DrawString("LCD set up complete");
+    myLcd.SetXY(0,2);    
+    wait(2);
+    myLcd.ClearLcdMem();
+    /// end LCD initialization
+    
+    
+    //pc.printf("Program Start\n\r");    
+    myLcd.DrawString("Program Start");
+    wait(1);
+    myLcd.ClearLcdMem();
+    //myservo = start_position ;  //set servo at 0 degree
+    current_position = start_position ;
     while(1) {
-        
+        //myLcd.ClearLcdMem();
         ultra.startMeas();
         
         wait(0.2);
@@ -20,7 +98,27 @@
         
         if( ultra.getMeas(range) == RANGE_MEAS_VALID)
         {
-            pc.printf("Range - %f\n\r",  range);
+            pc.printf("Range -> %f cm\n\r",  range);
+            myLcd.SetXY(0,1);
+            myLcd.DrawString("Range -> ");
+            myLcd.SetXY(0,2);
+            int i=(int)range;
+             pc.printf("valore di i -> %i cm\n\r",  i);
+            char gg[4] ; 
+            sprintf(gg, "%f", range);
+            pc.printf("valore di gg -> %c cm\n\r",  gg);
+            //gg = NokiaLcd::NumToStr(range) ;
+            myLcd.DrawString(gg);
+            myLcd.SetXY(0,3);
+            myLcd.DrawString(" cm");
+            if (range < treshold ) {
+                blink_buzz(buzzer,10);
+                myLcd.ClearLcdMem();
+                myLcd.SetXY(0,1);
+                myLcd.DrawString("Soglia superata.");
+                wait(1);
+                myLcd.ClearLcdMem();
+                }
         }
     }
 }
diff -r f91b4b2acbe1 -r 56e6200a897b mbed.bld
--- a/mbed.bld	Tue Mar 11 21:20:40 2014 +0000
+++ b/mbed.bld	Mon Nov 30 15:44:40 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/8e73be2a2ac1
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68
\ No newline at end of file