Skip to content

Instantly share code, notes, and snippets.

@heyitsnovi
Created March 13, 2023 06:08
Show Gist options
  • Save heyitsnovi/cb8b12b941fd832af5c1cafbb4b41834 to your computer and use it in GitHub Desktop.
Save heyitsnovi/cb8b12b941fd832af5c1cafbb4b41834 to your computer and use it in GitHub Desktop.
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>
#include <SPI.h>
#include <Ethernet.h>
const int trigPin = 9;
const int echoPin = 10;
const int max_distance = 1400; //in centimeter
// defines variables
long duration;
int distance;
//lcd
LiquidCrystal_I2C maindisplay(0x27, 20, 4);
NewPing sonar(trigPin, echoPin, max_distance);
void setup() {
// put your setup code here, to run once:
initLCD();
Serial.begin(9600); // Starts the serial communication
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
ultrasonic_sensor_listener();
}
void initLCD(){
maindisplay.init();
maindisplay.backlight();
maindisplay.setCursor(1, 0);
maindisplay.setCursor(0, 1);
maindisplay.print("Initializing....");
delay(5000);
maindisplay.clear();
maindisplay.print("Water Level:");
}
void ultrasonic_sensor_listener(){
delay(500);
maindisplay.setCursor(0, 1);
maindisplay.print(String( get_current_distance() )+String(" cm below sensor"));
int waterLevel = get_current_distance();
if(waterLevel>=120){
turnOnWarningLED();
}else{
turnOnOKLED();
}
}
void turnOnWarningLED(){
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
maindisplay.setCursor(0,3);
maindisplay.print("LOW WATER LEVEL");
}
void turnOnOKLED(){
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
maindisplay.setCursor(0,3);
maindisplay.print("WATER LEVEL OK!");
}
int get_current_distance(){
return sonar.ping_cm();
}
void send_http_request(){
String message = "Current Water Level is ";
//perform http request here:
Serial.println(String(message)+ get_current_distance());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment