This page describes how to use the sonic sensor HCSR04 and how it has been configured and used in the multidimensional box
If you need additional specific information about this topic or if you want to look it personally please write an email
#include
Using this library you will need just to declare a sonar object passing the pin parameters into the constructor
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MaxDistance);
Where TRIGGER_PIN and ECHO_PIN are the two pins you defined (and connected) for the related function. Be carefull to not invert those two pins. One of the most common errors is to invert them so to not read any value.
You can retrieve the distance measured in cm using the function:
sprintf(Centimeter, "%lu", sonar.ping_cm());
It returns an unsigned long that you can use to display something in your debug area or, like in my case, in the oled screen
NewPing library supports also some other functions. Here attached a list of all methods available:
In the picture, the max value=50 is the value in centimeters evaluated based on the potentiometer current value.
else if (Display == DISPLAY_SONAR) {
int BarLong;
//display.drawCircle((display.width() - WIDTH_SIGNAL_AREA) / 2, display.height() / 2, display.height() / 2-2, WHITE);
display.drawLine((display.width() - WIDTH_SIGNAL_AREA) / 2 - 30, 0, (display.width() - WIDTH_SIGNAL_AREA) / 2, display.height(), WHITE);
display.drawLine((display.width() - WIDTH_SIGNAL_AREA) / 2 + 30, 0, (display.width() - WIDTH_SIGNAL_AREA) / 2, display.height(), WHITE);
Serial.print("Distanza:");
Serial.println(atoi(stringGPS));
BarLong = map(atoi(stringGPS), 0, MaxDistance/2, 0, display.height());
Serial.print("BarLong:");
Serial.println(BarLong);
display.drawLine((display.width() - WIDTH_SIGNAL_AREA) / 2, display.height(), (display.width() - WIDTH_SIGNAL_AREA) / 2, display.height() - BarLong, Colour);
display.setCursor(5, 5);
display.print(stringGPS);
display.setCursor((display.width() - WIDTH_SIGNAL_AREA) / 2 + 10, display.height() - 8);
//Fix the Max Value
display.setTextColor(WHITE);
display.print("Max:");
display.print(MaxDistance/2);
display.setTextColor(Colour);
}
In this function you will see some parameters which depend on the dimension of your screen and on the dimension of the working area you decided to implement
in your available dinamic screen (I mean the space available for non service objects like the blutooth signal presence indicator or like the gps signal indicator).