#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#define RED_PIN 5
#define GREEN_PIN 17
#define BLUE_PIN 16
#define TRIG_PIN 4
#define ECHO_PIN 15
Adafruit_SSD1306 display(128, 64, &Wire, -1);
const int minDistance = 6;
const int maxDistance = 40;
void setup() {
Serial.begin(115200);
Wire.begin(18, 19);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED nao encontrado!");
while (true);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
}
void loop() {
int distance = getDistance();
// Converte distância para porcentagem
int waterLevel = map(distance, minDistance, maxDistance, 100, 0);
// Limita entre 0 e 100%
waterLevel = constrain(waterLevel, 0, 100);
Serial.print("Distancia: ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("Nivel da agua: ");
Serial.print(waterLevel);
Serial.println("%");
// Mostra no OLED
displayWaterLevel(waterLevel);
// Controla LED de acordo com o nível
controlarLED(waterLevel);
delay(1000);
}
// =====================================
// MEDIR DISTÂNCIA
// =====================================
int getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, 30000);
// Se não recebeu retorno
if (duration == 0) {
return maxDistance;
}
int distance = duration * 0.0343 / 2;
return distance;
}
// =====================================
// CONTROLAR LED RGB
// =====================================
void controlarLED(int nivel) {
if (nivel >= 70) {
// VERDE - tanque cheio
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
}
else if (nivel >= 30) {
// AMARELO - tanque perto de acabar
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
}
else {
// VERMELHO - pouca água
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
}
// =====================================
// MOSTRAR NÍVEL NO OLED
// =====================================
void displayWaterLevel(int level) {
display.clearDisplay();
// Texto "Water"
display.setCursor(20, 5);
display.setTextSize(1);
display.print("Water");
// Texto "Level"
display.setCursor(20, 15);
display.print("Level");
// Porcentagem
display.setCursor(0, 30);
display.setTextSize(3);
display.print(level);
display.print("%");
// =================================
// TANQUE
// =================================
int tankX = 72;
int tankY = 5;
int tankWidth = 55;
int tankHeight = 50;
// Corpo do tanque
display.drawRoundRect(
tankX,
tankY,
tankWidth,
tankHeight,
5,
WHITE
);
// Tampa
display.drawLine(
tankX + 5,
tankY - 3,
tankX + tankWidth - 5,
tankY - 3,
WHITE
);
display.drawLine(
tankX + 5,
tankY - 3,
tankX,
tankY,
WHITE
);
display.drawLine(
tankX + tankWidth - 5,
tankY - 3,
tankX + tankWidth,
tankY,
WHITE
);
// Tubo de entrada
display.drawLine(
tankX + 8,
tankY - 6,
tankX + 8,
tankY - 12,
WHITE
);
display.drawLine(
tankX + 8,
tankY - 12,
tankX + 14,
tankY - 12,
WHITE
);
display.drawLine(
tankX + 14,
tankY - 12,
tankX + 14,
tankY - 6,
WHITE
);
// =================================
// ÁGUA
// =================================
int waterHeight = map(
level,
0,
100,
0,
tankHeight
);
if (waterHeight > 0) {
display.fillRect(
tankX + 2,
tankY + tankHeight - waterHeight,
tankWidth - 4,
waterHeight,
WHITE
);
}
display.display();
}