#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define LDR_PIN 32
#define SM_PIN 33
#define LED_PIN 18
#define DHTPIN 19
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const uint8_t loadingBarHeight = 3;
const uint8_t loadingBarY = SCREEN_HEIGHT - loadingBarHeight - 1;
unsigned long animationStartTime = 0;
const unsigned long animationDuration = 6000;
void setup() {
Serial.begin(9600);
dht.begin();
Wire.begin(); // Initialize I2C communication
pinMode(LED_PIN, OUTPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Failed to start SSD1306 OLED"));
while (1); // Stop here if OLED initialization failed
}
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
int br = analogRead(LDR_PIN);
br = 4095 - br;
br = map(br, 0, 4095, 0, 100);
int sm = analogRead(SM_PIN);
sm = map(sm, 0, 4095, 0, 100);
if (isnan(h) || isnan(t)) {
Serial.println(F("DHT HAS NO DATA"));
delay(2000);
return;
}
display.clearDisplay();
displayTemperature(t);
displayHumidity(h);
displayBrightness(br);
displayMoisture(sm);
displayLoadingBar();
display.display();
// Toggle LED based on soil moisture
if( sm>65){
digitalWrite(LED_PIN, LOW);
}
if( sm<30){
digitalWrite(LED_PIN, HIGH);
}
delay(800);
}
void displayTemperature(float temp) {
unsigned long Cmillis = millis();
unsigned long Pmillis = 0;
if (Cmillis-Pmillis>=3000){
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Temp: ");
display.setTextSize(1);
display.setCursor(30, 0);
display.print(temp);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(1);
display.print("C");
}
}
void displayHumidity(float humi) {
unsigned long Cmillis = millis();
unsigned long Pmillis = 0;
if (Cmillis-Pmillis>=3000){
display.setTextSize(1);
display.setCursor(0, 11);
display.print("Humi: ");
display.setTextSize(1);
display.setCursor(30, 11);
display.print(humi);
display.print(" %");
}
}
void displayBrightness(int br) {
// int br = analogRead(LDR_PIN);
// br = 4095 - br;
// br = map(br, 0, 4095, 0, 100);
display.setTextSize(1);
display.setCursor(0, 22);
display.print("bright: ");
display.setTextSize(1);
display.setCursor(43, 22);
display.print(br);
display.print(" %");
}
void displayMoisture(int sm) {
// int sm = analogRead(SM_PIN);
// sm = map(sm, 0, 4095, 0, 100);
display.setTextSize(1);
display.setCursor(0, 33);
display.print("moisture: ");
display.setTextSize(1);
display.setCursor(53, 33);
display.print(sm);
display.print(" %");
}
void displayLoadingBar() {
// Draw loading bar outline
display.drawRect(0, loadingBarY, SCREEN_WIDTH, loadingBarHeight, WHITE);
// Calculate the progress of the loading bar
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - animationStartTime;
uint8_t progress = map(elapsedTime, 0, animationDuration, 1, SCREEN_WIDTH - 2);
// Fill the loading bar progress
display.fillRect(1, loadingBarY + 1, progress, loadingBarHeight - 2, SSD1306_WHITE);
if (elapsedTime >= animationDuration) {
animationStartTime = currentTime; // Start the animation again
}
}