#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHTesp.h>
#include <ESP32Servo.h>
#include <RTClib.h>
#include <Wire.h>
int countdown;
int i = 0;
int degree;
int currentMode = 0;
unsigned long lastButtonPressTime = 0;
int currentMenuOption = 0;
/* OLED Display */
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // 0OLED height, in pixels
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create an OLED display object connected to I2C
/* Servo */
const int servoPin = 4; // Servo Pin
Servo servo; // Servo Object
int pos = 0; // Servo Position
/* Push Button */
#define GREEN_BUTTON_PIN 19
#define RED_BUTTON_PIN 18
#define BLUE_BUTTON_PIN 5
const unsigned long LONG_PRESS_THRESHOLD = 1000;
unsigned long ButtonPressStartTime = 0;
bool longpress = false;
int green_button_state;
int red_button_state;
int blue_button_state;
/* LED */
#define GREEN_LED_PIN 16
#define RED_LED_PIN 17
int green_led_state = LOW;
int red_led_state = LOW;
/* RTC Clock */
RTC_DS1307 rtc;
char namaHari[7][12] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};
/* Speaker */
#define SPEAKER_PIN 2
/* DHT Input (Suhu-Humidity) */
const int DHT_PIN = 15;
DHTesp dhtSensor;
/* LDR Input (Kecerahan) */
#define LDR_PIN 13
const float GAMMA = 0.7;
const float RL10 = 50;
void dhtsensor();
void ldrsensor();
void settingalarm();
void optionmenu();
void defaultmenu();
void setup() {
Serial.begin(115200); // Start Serial Monitor
/* Setup OLED */
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // initialize OLED display with I2C address 0x3C
Serial.println(F("failed to start SSD1306 OLED")); // Handler
}
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("VI-ROSE"); // set text
oled.display(); // display on OLED
/* Setup Servo */
servo.attach(servoPin);
/* Setup Button */
pinMode(GREEN_BUTTON_PIN, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
pinMode(RED_BUTTON_PIN, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
pinMode(BLUE_BUTTON_PIN, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
green_button_state = digitalRead(GREEN_BUTTON_PIN);
red_button_state = digitalRead(RED_BUTTON_PIN);
blue_button_state = digitalRead(BLUE_BUTTON_PIN);
/* Setup LED */
pinMode(GREEN_LED_PIN, OUTPUT); // set ESP32 pin to output mode
pinMode(RED_LED_PIN, OUTPUT); // set ESP32 pin to output mode
/* Setup RTC CLock */
if (!rtc.begin()) {
Serial.println("RTC TIDAK TERBACA");
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // update rtc dari waktu komputer
}
/* Setup Speaker */
pinMode(SPEAKER_PIN, OUTPUT);
/* Setup DHT Sensor */
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
/* Setup LDR */
pinMode(LDR_PIN, INPUT);
Serial.println("Ready for action!");
}
void loop() {
unsigned long currentTime = millis();
green_button_state = digitalRead(GREEN_BUTTON_PIN);
red_button_state = digitalRead(RED_BUTTON_PIN);
blue_button_state = digitalRead(BLUE_BUTTON_PIN);
if (green_button_state == LOW || red_button_state == LOW || blue_button_state == LOW) {
lastButtonPressTime = currentTime;
}
if (currentTime - lastButtonPressTime > 30000 && currentMode == 0) {
currentMode = 0;
}
if (green_button_state == LOW) {
if (ButtonPressStartTime == 0) {
ButtonPressStartTime = millis();
Serial.println("Press Detected");
}
while (green_button_state == LOW) {
green_button_state = digitalRead(GREEN_BUTTON_PIN);
if (millis() - ButtonPressStartTime >= LONG_PRESS_THRESHOLD) {
currentMode = 0;
servo.write(0);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
Serial.println("Long Press Detected");
longpress = true;
delay(1000);
break;
}
}
if (currentMode == 0 && longpress == false) {
currentMode = 1;
} else if (currentMode == 1) {
if (currentMenuOption == 0) {
currentMode = 2;
} else if (currentMenuOption == 1) {
currentMode = 3;
} else if (currentMenuOption == 2) {
currentMode = 4;
}
}
ButtonPressStartTime = 0;
longpress = false;
} else if (red_button_state == LOW) {
currentMenuOption = (currentMenuOption + 2) % 3;
} else if (blue_button_state == LOW) {
currentMenuOption = (currentMenuOption + 1) % 3;
}
switch (currentMode) {
case 0:
defaultmenu();
break;
case 1:
optionmenu();
break;
case 2:
dhtsensor();
break;
case 3:
ldrsensor();
break;
case 4:
settingalarm();
break;
default:
break;
}
}
void defaultmenu() {
DateTime now = rtc.now();
String waktu = String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second());
String tanggal = String(namaHari[now.dayOfTheWeek()]) + ", " + String(now.day()) + "/" + String(now.month()) + "/" + String(now.year());
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0, 0);
oled.println(waktu);
oled.setCursor(0, 10);
oled.println(tanggal);
oled.display();
}
void optionmenu() {
oled.clearDisplay();
oled.setCursor(0, 0);
oled.setTextColor(WHITE);
oled.print("Menu Options:");
for (int i = 0; i < 3; ++i) {
if (i == currentMenuOption) {
oled.setTextColor(BLACK, WHITE);
} else {
oled.setTextColor(WHITE);
}
switch (i) {
case 0:
oled.setCursor(0, 10 + i * 10);
oled.print("1. Sensor DHT");
break;
case 1:
oled.setCursor(0, 10 + i * 10);
oled.print("2. Sensor LDR");
break;
case 2:
oled.setCursor(0, 10 + i * 10);
oled.print("3. Setting Alarm");
break;
default:
break;
}
}
oled.display();
}
void dhtsensor() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
String temperatureString = "Temp: " + String(data.temperature, 2) + "°C\n";
String humidityString = "Humidity: " + String(data.humidity, 1) + "%\n";
int degree = map(data.temperature, -40, 80, 0, 180);
if (degree > 90) {
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
servo.write(degree);
// if (degree / 180.0 > 0.9) {
// tone(SPEAKER_PIN, 1000);
// delay(100);
// noTone(SPEAKER_PIN);
// }
} else {
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
servo.write(degree);
// if (degree / 180.0 < 0.1) {
// tone(SPEAKER_PIN, 1000);
// delay(100);
// noTone(SPEAKER_PIN);
// }
}
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0, 0);
oled.println(temperatureString);
oled.println(humidityString);
oled.display();
}
void ldrsensor() {
int analogValue = analogRead(LDR_PIN);
int percentage = map(analogValue, 32, 4063, 100, 0);
String ldrString = "Kecerahan: " + String(percentage) + "%";
int degree = map(percentage, 0, 100, 0, 180);
if (degree > 90) {
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
servo.write(degree);
// if (degree / 180.0 > 0.9) {
// tone(SPEAKER_PIN, 1000);
// delay(100);
// noTone(SPEAKER_PIN);
// }
} else {
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
servo.write(degree);
// if (degree / 180.0 < 0.1) {
// tone(SPEAKER_PIN, 1000);
// delay(100);
// noTone(SPEAKER_PIN);
// }
}
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0, 0);
oled.println(ldrString);
oled.display();
}
void settingalarm() {
}