#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS1307 rtc;
const int sensorPin = A0;
// Kalibrasyon Değerleri (Kapasitif Sensör v1.2)
const int KURU_DEGER = 580;
const int ISLAK_DEGER = 280;
const int KRITIK_NEM_ESIGI = 30; // %30 altı acil durum uyanması
// 1. Pixel Art Saksı ve Fidan İkonu (Gündüz)
const unsigned char PROGMEM pot_icon16x16[] = {
0b00000001, 0b10000000,
0b00000011, 0b11000000,
0b00011001, 0b10011000,
0b00111101, 0b10111100,
0b00111111, 0b11111100,
0b00011101, 0b10111000,
0b00000001, 0b10000000,
0b01111111, 0b11111110,
0b01111111, 0b11111110,
0b00111111, 0b11111100,
0b00111111, 0b11111100,
0b00111111, 0b11111100,
0b00111111, 0b11111100,
0b00011111, 0b11111000,
0b00011111, 0b11111000,
0b00001111, 0b11110000
};
// 2. Kapalı Form Tam Hilal Ay (16x16 - Gece)
const unsigned char PROGMEM hilal_tam_16x16[] = {
0b00000111, 0b11100000,
0b00011111, 0b11110000,
0b00111111, 0b11000000,
0b01111110, 0b00000000,
0b01111100, 0b00000000,
0b11111000, 0b00000000,
0b11111000, 0b00000000,
0b11111000, 0b00000000,
0b11111000, 0b00000000,
0b11111000, 0b00000000,
0b01111100, 0b00000000,
0b01111110, 0b00000000,
0b00111111, 0b00000000,
0b00111111, 0b11000000,
0b00011111, 0b11110000,
0b00000111, 0b11100000
};
unsigned long sonGuncelleme = 0;
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("OLED Bulunamadi!"));
for(;;);
}
display.setRotation(2);
if (!rtc.begin()) {
Serial.println(F("RTC Bulunamadi!"));
for(;;);
}
// RTC çalışmıyorsa veya pili yeni takıldıysa derleme anındaki gerçek saate ayarlar
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
}
void cizGunduzEkrani(DateTime now, int nemYuzde) {
display.clearDisplay();
// 1. İkon
display.drawBitmap(4, 0, pot_icon16x16, 16, 16, SSD1306_WHITE);
// 2. Başlık
display.setTextSize(1);
display.setCursor(26, 4);
display.print("SMART PLANTER");
display.drawFastHLine(0, 18, 128, SSD1306_WHITE);
// 3. Saat
display.setTextSize(2);
display.setCursor(35, 22);
if (now.hour() < 10) display.print("0");
display.print(now.hour());
display.print(":");
if (now.minute() < 10) display.print("0");
display.print(now.minute());
display.drawFastHLine(0, 39, 128, SSD1306_WHITE);
// 4. Nem ve Durum
display.setTextSize(1);
display.setCursor(4, 43);
display.print("NEM: %");
display.print(nemYuzde);
display.setCursor(75, 43);
if(nemYuzde < KRITIK_NEM_ESIGI) {
display.print("[KURU!]");
} else if(nemYuzde <= 70) {
display.print("[IDEAL]");
} else {
display.print("[ISLAK]");
}
// 5. Progress Bar
display.drawRect(4, 54, 120, 8, SSD1306_WHITE);
int barWidth = map(nemYuzde, 0, 100, 0, 116);
display.fillRect(6, 56, barWidth, 4, SSD1306_WHITE);
display.display();
}
void cizGeceUykuEkrani() {
display.clearDisplay();
// Hilal Ay İkonu (X: 42, Y: 24)
display.drawBitmap(42, 24, hilal_tam_16x16, 16, 16, SSD1306_WHITE);
// Kademeli Yükselen Zzz
display.setTextSize(1);
display.setCursor(65, 32);
display.print("z");
display.setCursor(75, 22);
display.print("z");
display.setCursor(86, 12);
display.print("Z");
display.display();
}
void loop() {
if (millis() - sonGuncelleme >= 100) {
sonGuncelleme = millis();
// Analog okuma kararlılığı için çift okuma
analogRead(sensorPin);
int sensorRaw = analogRead(sensorPin);
int nemYuzde = map(sensorRaw, KURU_DEGER, ISLAK_DEGER, 0, 100);
nemYuzde = constrain(nemYuzde, 0, 100);
DateTime now = rtc.now();
int saat = now.hour();
bool geceMi = (saat >= 0 && saat < 6);
bool kritikKuruMu = (nemYuzde < KRITIK_NEM_ESIGI);
if (!geceMi || (geceMi && kritikKuruMu)) {
cizGunduzEkrani(now, nemYuzde);
} else {
cizGeceUykuEkrani();
}
}
}Loading
ssd1306
ssd1306