So I'm working on a project that includes reading out a distance from a Ping))) sensor to a 16x2 lcd. I have the prototype done on my board and the code works brilliantly... a little too brilliantly. Using the double variable the lcd is displaying the inches rounded to the nearest 100th, however I want the lcd to display to the nearest quarter inch instead. For example; if ping.inches() returns 3.81 inches, I want it to display 4 inches instead by rounding up to the nearest .25.
Getting it to round to the nearest whole number is easy (replace "double" with "int"), but that's not accurate enough...
Any guidance on how this can be accomplished would be much appreciated!
Here is the code too in case you wanna check it out:
#include <Ping.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
const int buttonPin = 8;
Ping ping = Ping(7,0,0);
double tareDist;
double constDist;
double curDist;
int val = 0;
void setup() {
pinMode(buttonPin, INPUT);
lcd.begin(16,2);
ping.fire();
constDist = ping.inches();
tareTheDist();
}
void loop() {
lcd.setCursor(0,0);
lcd.print("Patient's Ht: ");
lcd.setCursor(0,1);
val = digitalRead(buttonPin);
if (val == HIGH){
tareTheDist();
}
ping.fire();
constDist = ping.inches();
lcd.print(tareDist-constDist);
lcd.print(" inches ");
delay(500);
val == LOW;
}
void tareTheDist(){
tareDist = constDist;
lcd.setCursor(0,0);
lcd.print("------TARE------");
lcd.setCursor(0,1);
lcd.print("--Please Wait!--");
delay(1000);
}