I 'm trying to make a water level sensor with LEDs and Arduino uno to indicate the level of water in a tank. I tried writing this code but im getting a error. I want the all three leds to light up when the tank is filled (less distance).
I followed this tutorial but the code it had was showing 0 for the sensor value constantly.
Here is my code
#include <Ultrasonic.h>
#define TRIGGER_PIN 6
#define ECHO_PIN 7
Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
}
void loop()
{
float cmMsec, inMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
Serial.print("MS: ");
Serial.print(microsec);
Serial.print(", CM: ");
Serial.print(cmMsec);
Serial.print(", IN: ");
Serial.println(inMsec);
delay(1000);
}
void led () {
digitalWrite(13,LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
if (cmMsec > 20) {
digitalWrite(13,HIGH);
}
if (cmMsec >=20 and cmMsec >= 10) {
digitalWrite(12,HIGH);
}
if (cmMsec < 10) {
digitalWrite(11,HIGH);
}
}
}