Hi,
I made a distance meter to use simultaneously in four directions at the same time. It uses an Arduino Mega, LCD 16x2 and 4 sr04 ultrasonic sensors. The idea is to make it usefull to mark the wall to hang a picture and stuff like that. I’d like it to have 4 functions like to change with only one button “Prog”
- Measure the distance from the center hole to the four walls
- Reset it to 0 in and measure from that point.
- Measure the total distance, so you can put it in the ground and know the height of the roof.
- Measure the area so it will calculate the area using the distance to the roof and the distance to the wall, etc.
Also I have a button to change the units to cm or inches
I think I have the 1, 3 and 4 solved but I don’t know how to solve the 2 and the unit conversion.
Besides, I am getting wrong measures using the library newping. I get 1 or 2cm more in a meter, 5cm more in 2 meters and 12 or 13 cm more in 4 meters.
I am kind of a noob with the code, this is what I have until now:
#include <LiquidCrystal.h>
#include <NewPing.h>
#define SONAR_NUM 4 // Number or sensors.
#define MAX_DISTANCE 500 // Max distance in cm.
#define PING_INTERVAL 200 // Milliseconds between pings
LiquidCrystal lcd( 12, 10, 6, 5, 4, 3 );
unsigned long pingTimer[SONAR_NUM]; // When each pings.
unsigned int cm[SONAR_NUM]; // Store ping distances.
uint8_t currentSensor = 0; // Which sensor is active.
const int buttonPin = 40;
const int button2Pin = 44;
int buttonState = 0;
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(22, 24, MAX_DISTANCE),
NewPing(26, 28, MAX_DISTANCE),
NewPing(30, 32, MAX_DISTANCE),
NewPing(34, 36, MAX_DISTANCE),
};
void setup() {
lcd.begin(16, 2);
pinMode(buttonPin, INPUT);
pingTimer[0] = millis() + 75; // First ping start in ms.
for (uint8_t i = 1; i < SONAR_NUM; i++)
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}
void loop() {
if(digitalRead(buttonPin) ) {
// increment count
buttonState++;
// debounce
delay(300);
}
if (buttonState > 3){
buttonState = 0;
}
for (uint8_t i = 0; i < SONAR_NUM; i++) {
if (millis() >= pingTimer[i]) {
pingTimer[i] += PING_INTERVAL * SONAR_NUM;
if (i == 0 && currentSensor == SONAR_NUM - 1)
oneSensorCycle();
sonar[currentSensor].timer_stop();
currentSensor = i;
cm[currentSensor] = 0;
sonar[currentSensor].ping_timer(echoCheck);
}
}
}
void echoCheck() { // If ping echo, set distance to array.
if (sonar[currentSensor].check_timer())
cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}
void oneSensorCycle() {
switch (buttonState) {
case 0:
lcd.clear();
lcd.setCursor(7,0);
lcd.print(cm[0]+7);
lcd.setCursor(7,1);
lcd.print(cm[1]+8);
lcd.setCursor(0,0);
lcd.print(cm[2]+6);
lcd.setCursor(13,0);
lcd.print(cm[3]+6);
lcd.setCursor(0,1);
lcd.print("Hole");
break;
case 1:
lcd.clear();
lcd.setCursor(7,0);
lcd.print("0");
lcd.setCursor(7,1);
lcd.print("0");
lcd.setCursor(0,0);
lcd.print("0");
lcd.setCursor(13,0);
lcd.print("0");
lcd.setCursor(0,1);
lcd.print("Rel.");
break;
case 2:
lcd.clear();
lcd.setCursor(7,0);
lcd.print(cm[0]+15);
lcd.setCursor(7,1);
lcd.print(cm[1]+15);
lcd.setCursor(0,0);
lcd.print(cm[2]+13);
lcd.setCursor(13,0);
lcd.print(cm[3]+13);
lcd.setCursor(0,1);
lcd.print("Total");
lcd.setCursor(14 ,1);
lcd.print("cm");
break;
case 3:
//area up/right up/left
lcd.clear();
lcd.setCursor(0,0);
lcd.print((cm[0]-15)*(cm[2]-13));
lcd.setCursor(13,0);
lcd.print((cm[0]-15)*(cm[3]-13));
// Area down/left down/right
// lcd.setCursor(0,1);
// lcd.print(cm[1]*cm[2]);
// lcd.setCursor(13,1);
// lcd.print(cm[1]*cm[3]);
lcd.setCursor(6,1);
lcd.print("Area");
break;
}
}