#define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 10 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 475 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(2000); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
}
cheers for the help if you can. if you cant dont worry.
i just want to display the sensor readings on a little LCD graph depending on how far away the reading is.
Ok, then use a FOR loop. You do know your lcd is only 2 by 16 right, so the max you can have is 32 bytes, unless you make it display a character every 2cm.
This should print a bar across both rows as temp increases or decreases.
Note: this is not tested, so if it does not compile, just check the closing brackets.
This goes in your loop(), at the end.
int count = 0; // DO NOT KEEP THIS HERE, set globally at the top
if(count < temp) {
for(count; count < temp; count++)
{
if(count < 16)
{
lcd.setCursor(count, 0);
lcd.print("]");
}
else {
lcd.setCursor(count - 16, 1);
lcd.print("]");
}
}
}
else {
for(count; count >= temp; count--)
{
if(count < 16)
{
lcd.setCursor(count, 0);
lcd.print(" "); // you want to clear what was put on
}
else
{
lcd.setCursor(count - 16, 1);
lcd.print(" "); // you want to clear what was put on
}
}
}