void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
#include <NewPing.h>
#define TRIGGER_PIN 13 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 12 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // 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.
// Defining 1st and last pins for 10 segment LED
int LED_FIRST_PIN = 2;
int LED_LAST_PIN = 11;
// Defining Watertank min max levels in cm
float WT_MAXLEVEL = 10;
float WT_MINLEVEL = 100;
float WT_DEEP = WT_MINLEVEL - WT_MAXLEVEL;
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
For (i = LED_FIRST_PIN; i < LED_LAST_PIN; i++) { //Initializing pins for LED
pinMode (j,OUTPUT);
}
}
void loop() {
delay(100); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}
void DistancePercent() {
float Distance_cm = sonar.ping_cm();
float Percent = 0;
Percent = (1 - ((Distance_cm - WT_MAXLEVEL) / WT_DEEP)) * 100;
If (Percent > 100) {
Serial.print("Tank overflow");
Percent = 100
}
If (Percent < 0){
Serial.pring("Tank level is too low")
Percent = 0
}
return Percent
}