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.

Fork of UltraTest by

Revision:
1:56e6200a897b
Parent:
0:f91b4b2acbe1
```--- 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 ;
+
+{
+   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 ) {