#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include "RTClib.h"
#define SPEAKER_PIN 5
#define THERM_PIN A0 // Терморезистор підключений до A0
#define SERIES_RESISTOR 10000 // 10k резистор у дільнику напруги
// ----------------- НАЛАШТУВАННЯ КЛАВІАТУРИ -----------------
const uint8_t ROWS = 4;
const uint8_t COLS = 3;
char keys[ROWS][COLS] = {
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' },
{ '*', '0', '#' }
};
uint8_t colPins[COLS] = {48, 46, 44};
uint8_t rowPins[ROWS] = {53, 52, 51, 50};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// ----------------- LCD -----------------
LiquidCrystal_I2C lcd(0x27, 20, 4);
// ----------------- RTC -----------------
RTC_DS1307 rtc;
// ----------------- ПАМ’ЯТЬ ДЛЯ НОТАТОК -----------------
String drugs[10];
String times[10];
int indexCount = 0;
String note = "";
String status_t = "Good";
// ---------------- ФУНКЦІЯ ДЛЯ БУЗЕРА ----------------
void BuzzerSong(int p) {
tone(SPEAKER_PIN, p, 150);
}
// ---------------- ФУНКЦІЯ РОЗРАХУНКУ ТЕМПЕРАТУРИ ----------------
float getTemperature() {
int raw = analogRead(THERM_PIN);
// Захист від обриву або короткого замикання
if (raw < 5 || raw > 1018) return NAN;
float voltage = raw * (5.0 / 1023.0);
float resistance = SERIES_RESISTOR * (5.0 / voltage - 1.0);
// Формула Стейнхарта–Харта / Бета-формула
float tempK = 1.0 / (1.0 / 298.15 + (1.0 / 3950.0) * log(resistance / 10000.0));
float tempC = tempK - 273.15;
return tempC;
}
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
rtc.begin();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Smart Thermometer");
delay(800);
lcd.clear();
}
void loop() {
// -------------- ЧАС ------------------
DateTime now = rtc.now();
int hourNow = now.hour();
// -------------- НАГАДУВАННЯ ПРО ЛІКИ ------------------
for (int i = 0; i < indexCount; i++) {
if (times[i].toInt() == hourNow) {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Use the drugs!");
BuzzerSong(800);
delay(1200);
}
}
// -------------- ТЕМПЕРАТУРА ------------------
float t = getTemperature();
if (!isnan(t)) {
if (t < 34) {
status_t = "Bad";
BuzzerSong(1000); // сигнал низької температури
}
else if (t <= 37) {
status_t = "Good";
}
else if (t >= 38) {
status_t = "Bad";
BuzzerSong(1200); // сигнал високої температури
}
}
// -------------- ВИВІД НА LCD ------------------
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T: ");
if (isnan(t)) lcd.print("??");
else lcd.print(t, 1);
lcd.print(" C ");
lcd.print(status_t);
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(now.hour());
lcd.print(":");
if (now.minute() < 10) lcd.print("0");
lcd.print(now.minute());
lcd.setCursor(0, 2);
lcd.print("Add drug: ");
lcd.print(note);
// ------------- ОБРОБКА КЛАВІАТУРИ ----------------
char key = keypad.getKey();
if (key != NO_KEY) {
// Очищення нагадувань
if (key == '*') {
for (int i = 0; i < indexCount; i++) {
if (times[i].toInt() == hourNow) {
times[i] = "";
drugs[i] = "";
indexCount--;
}
}
}
// Підтвердження вводу
else if (key == '#' && note != "") {
if (indexCount < 10) {
int spaceIndex = note.indexOf('_');
if (spaceIndex != -1) {
String drug = note.substring(0, spaceIndex);
String time = note.substring(spaceIndex + 1);
drugs[indexCount] = drug;
times[indexCount] = time;
indexCount++;
note = ""; // очищаємо введення
} else {
lcd.setCursor(0, 3);
lcd.print("Use '_' to split!");
delay(1200);
}
}
}
// Звичайне додавання символу
else {
note += key;
}
// Обмеження довжини вводу
if (note.length() > 20) {
note = "";
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Too long!");
delay(1000);
}
}
delay(250);
}