#include "LiquidCrystal_I2C.h"
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16 // numero de colunas que tem o display
#define LCD_LINES 2 // numero de linhas que tem o display
#define PIN_TRIG 13 // numero do PINO que está ligado o TRIG
#define PIN_ECHO 12 // numero do PINO que está ligado o ECHO
#define PIN_SPEAKER 2 // numero do PINO que está ligado o Buzzer
#define HEIGHT 100 // altura do deposito
#define MIN_LEVEL 4000 // altura minima de agua no deposito
const int metronometro = 1000; //quanto em quanto tempo gostaria que o sensor lesse o nivel de agua
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
//These 5 arrays paint the bars that go across the screen.
byte customChar[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
void setup() {
// Init
lcd.init();
lcd.clear();
lcd.backlight();
lcd.createChar(0, customChar);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
pinMode(PIN_SPEAKER, OUTPUT);
}
void loop() {
lcd.clear();
// Inicia uma nova medição:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
int duration = pulseIn(PIN_ECHO, HIGH);
int distance = (duration/2) / 29.1;
int water_level = HEIGHT - distance;
// primeira linha do display
lcd.setCursor(0,0);
lcd.print("Nivel: ");
lcd.print(water_level);
lcd.print(" cm");
int percentage = map(water_level, 0, HEIGHT, 0, 16);
// segunda linha do display
lcd.setCursor(0,1);
for(int i=0; i <= percentage; i++)
{
lcd.write(0);
}
if(MIN_LEVEL <= distance) {
tone(PIN_SPEAKER, 262, 250);
}
delay(metronometro);
}