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

Committer:
loarri
Date:
Mon Nov 30 15:44:40 2015 +0000
Revision:
1:56e6200a897b
Parent:
0:f91b4b2acbe1
`The Nucleo Distance Calculator.; First release`

### Who changed what in which revision?

UserRevisionLine numberNew contents of line
loarri 1:56e6200a897b 1 //==================================
loarri 1:56e6200a897b 2 // The Nucleo Distance Calculator
loarri 1:56e6200a897b 3 // v. 1.0
loarri 1:56e6200a897b 4 //==================================
loarri 1:56e6200a897b 5 // by Lorenzo Arrigoni
loarri 1:56e6200a897b 6 // laelectronics@libero.it
loarri 1:56e6200a897b 7 //----------------------------------
loarri 1:56e6200a897b 8 // November 2015
loarri 1:56e6200a897b 9 ////////////////////////////////////////////
loarri 1:56e6200a897b 10 // This is a simple that measures the distance
loarri 1:56e6200a897b 11 // and signals in video and with a buzzer if
loarri 1:56e6200a897b 12 // the measured distance is below a certain threshold.
loarri 1:56e6200a897b 13 //////////////////////////////////////////
loarri 1:56e6200a897b 14
srsmitherman 0:f91b4b2acbe1 15 #include "mbed.h"
srsmitherman 0:f91b4b2acbe1 16 #include "HCSR04.h"
loarri 1:56e6200a897b 17 #include "NOKIA_5110.h"
srsmitherman 0:f91b4b2acbe1 18
loarri 1:56e6200a897b 19
loarri 1:56e6200a897b 20 DigitalOut buzzer(D7) ;
loarri 1:56e6200a897b 21 HCSR04 ultra(D3,D4); //pin trigger, pin echo
loarri 1:56e6200a897b 22
loarri 1:56e6200a897b 23 //treshold for buzzer
loarri 1:56e6200a897b 24 #define treshold 5 // cm
loarri 1:56e6200a897b 25
loarri 1:56e6200a897b 26 int start_position =0.5 ;
loarri 1:56e6200a897b 27 int current_position = start_position ;
loarri 1:56e6200a897b 28
loarri 1:56e6200a897b 29 // swith on/off for bluetooth
loarri 1:56e6200a897b 30 #define BLUE 0
loarri 1:56e6200a897b 31
loarri 1:56e6200a897b 32 // debug mode
loarri 1:56e6200a897b 33 #define DEBUG 1
loarri 1:56e6200a897b 34
loarri 1:56e6200a897b 35 #if DEBUG == 0
loarri 1:56e6200a897b 36 //#define PC_DEBUG(args...) pc.printf(args)
loarri 1:56e6200a897b 37 #else
loarri 1:56e6200a897b 38 //#define PC_DEBUG(args...)
loarri 1:56e6200a897b 39 #endif
loarri 1:56e6200a897b 40
loarri 1:56e6200a897b 41 #if BLUE == 1
loarri 1:56e6200a897b 42 Serial pc(D8,D2);
loarri 1:56e6200a897b 43 #else
loarri 1:56e6200a897b 44 Serial pc(USBTX, USBRX);
loarri 1:56e6200a897b 45 #endif
loarri 1:56e6200a897b 46
loarri 1:56e6200a897b 47
loarri 1:56e6200a897b 48
srsmitherman 0:f91b4b2acbe1 49
srsmitherman 0:f91b4b2acbe1 50 float range=0;
loarri 1:56e6200a897b 51 //char gg ;
loarri 1:56e6200a897b 52
loarri 1:56e6200a897b 53 void blink_buzz(DigitalOut name, float time)
loarri 1:56e6200a897b 54 {
loarri 1:56e6200a897b 55 name = 1 ;
loarri 1:56e6200a897b 56 wait_ms(time);
loarri 1:56e6200a897b 57 name = 0 ;
loarri 1:56e6200a897b 58 }
srsmitherman 0:f91b4b2acbe1 59
srsmitherman 0:f91b4b2acbe1 60 int main() {
srsmitherman 0:f91b4b2acbe1 61
loarri 1:56e6200a897b 62 // initialize
loarri 1:56e6200a897b 63 buzzer = 0 ;
loarri 1:56e6200a897b 64
loarri 1:56e6200a897b 65 LcdPins myPins;
loarri 1:56e6200a897b 66 myPins.sce = D8;
loarri 1:56e6200a897b 67 myPins.rst = D9;
loarri 1:56e6200a897b 68 myPins.dc = D10;
loarri 1:56e6200a897b 69 myPins.mosi = D11; //SPI_MOSI;
loarri 1:56e6200a897b 70 myPins.miso = NC;
loarri 1:56e6200a897b 71 myPins.sclk = D13; //SPI_SCK;
loarri 1:56e6200a897b 72 NokiaLcd myLcd( myPins );
loarri 1:56e6200a897b 73
loarri 1:56e6200a897b 74
loarri 1:56e6200a897b 75 /// LCD initialization
loarri 1:56e6200a897b 76 myLcd.InitLcd(); // LCD init
loarri 1:56e6200a897b 77 myLcd.DrawString("Loarri - 2015");
loarri 1:56e6200a897b 78 myLcd.SetXY(0,2);
loarri 1:56e6200a897b 79 myLcd.DrawString("LCD set up complete");
loarri 1:56e6200a897b 80 myLcd.SetXY(0,2);
loarri 1:56e6200a897b 81 wait(2);
loarri 1:56e6200a897b 82 myLcd.ClearLcdMem();
loarri 1:56e6200a897b 83 /// end LCD initialization
loarri 1:56e6200a897b 84
loarri 1:56e6200a897b 85
loarri 1:56e6200a897b 86 //pc.printf("Program Start\n\r");
loarri 1:56e6200a897b 87 myLcd.DrawString("Program Start");
loarri 1:56e6200a897b 88 wait(1);
loarri 1:56e6200a897b 89 myLcd.ClearLcdMem();
loarri 1:56e6200a897b 90 //myservo = start_position ; //set servo at 0 degree
loarri 1:56e6200a897b 91 current_position = start_position ;
srsmitherman 0:f91b4b2acbe1 92 while(1) {
loarri 1:56e6200a897b 93 //myLcd.ClearLcdMem();
srsmitherman 0:f91b4b2acbe1 94 ultra.startMeas();
srsmitherman 0:f91b4b2acbe1 95
srsmitherman 0:f91b4b2acbe1 96 wait(0.2);
srsmitherman 0:f91b4b2acbe1 97
srsmitherman 0:f91b4b2acbe1 98
srsmitherman 0:f91b4b2acbe1 99 if( ultra.getMeas(range) == RANGE_MEAS_VALID)
srsmitherman 0:f91b4b2acbe1 100 {
loarri 1:56e6200a897b 101 pc.printf("Range -> %f cm\n\r", range);
loarri 1:56e6200a897b 102 myLcd.SetXY(0,1);
loarri 1:56e6200a897b 103 myLcd.DrawString("Range -> ");
loarri 1:56e6200a897b 104 myLcd.SetXY(0,2);
loarri 1:56e6200a897b 105 int i=(int)range;
loarri 1:56e6200a897b 106 pc.printf("valore di i -> %i cm\n\r", i);
loarri 1:56e6200a897b 107 char gg[4] ;
loarri 1:56e6200a897b 108 sprintf(gg, "%f", range);
loarri 1:56e6200a897b 109 pc.printf("valore di gg -> %c cm\n\r", gg);
loarri 1:56e6200a897b 110 //gg = NokiaLcd::NumToStr(range) ;
loarri 1:56e6200a897b 111 myLcd.DrawString(gg);
loarri 1:56e6200a897b 112 myLcd.SetXY(0,3);
loarri 1:56e6200a897b 113 myLcd.DrawString(" cm");
loarri 1:56e6200a897b 114 if (range < treshold ) {
loarri 1:56e6200a897b 115 blink_buzz(buzzer,10);
loarri 1:56e6200a897b 116 myLcd.ClearLcdMem();
loarri 1:56e6200a897b 117 myLcd.SetXY(0,1);
loarri 1:56e6200a897b 118 myLcd.DrawString("Soglia superata.");
loarri 1:56e6200a897b 119 wait(1);
loarri 1:56e6200a897b 120 myLcd.ClearLcdMem();
loarri 1:56e6200a897b 121 }
srsmitherman 0:f91b4b2acbe1 122 }
srsmitherman 0:f91b4b2acbe1 123 }
srsmitherman 0:f91b4b2acbe1 124 }