I need some help and am pretty new to using an Arduino.
Below is code I have used to have 2 sensors measuring a distance, And need help in how to output those readings to an LCD in the form of
"Distance Sensor
Distance1 : A
Distance2 : B
"
#include <NewPing.h>
#include <LiquidCrystal.h>
#define SONAR_NUM 2 // Number or sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int backLight = 6; // pin 13 will control the backlight
unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0; // Keeps track of which sensor is active.
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(43, 42, MAX_DISTANCE), // Each sensor’s trigger pin, echo pin, and max distance to ping.
NewPing(45, 44, MAX_DISTANCE)
///////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
};
void setup() {
Serial.begin(115200);
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
pingTimer = pingTimer[i - 1] + PING_INTERVAL;
/////////////////////////////////////////
-
lcd.begin(20, 4);*
-
lcd.print(“Powering On…”);*
}
void loop() { -
pinMode(backLight, OUTPUT);*
-
digitalWrite(backLight, HIGH); // turn backlight on. Replace ‘HIGH’ with ‘LOW’ to turn it off.*
-
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.*
_ if (millis() >= pingTimer*) { // Is it this sensor’s time to ping?_
pingTimer += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.
if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
_ currentSensor = i; // Sensor being accessed.
cm[currentSensor] = 0; // Make distance zero in case there’s no ping echo for this sensor._
sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
_ }
}*_
lcd.clear();
* lcd.setCursor(0, 0);*
* lcd.print(“Reversing Sensor”);*
//////// code to send data to LCD//////////////////
}
void echoCheck() { // If ping received, set the sensor distance to array.
* if (sonar[currentSensor].check_timer())
cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
_}
void oneSensorCycle() { // Sensor ping cycle complete, do something with the results._
for (uint8_t i = 0; i < SONAR_NUM; i++) {
_ Serial.print(i);
Serial.print("=");
Serial.print(cm);
Serial.print("cm ");
}
Serial.println();
}*_