/*
baca inputan listrik (1 / 0) sensor gas
ada jadwal nyala / mati bisa disetting
3 tombol untuk nyetting
4 relay sebagai led info
1 relay sebagai alarm
1 relay sebagai stopkontak
ceklist:
program baca sensor
program display lcd sensor
program set waktu on
program set waktu off
program set listrik sensor
program alarm
memory
program backlit lcd
*/
#include<Wire.h>
#include<RtcDS1307.h>
#include<LiquidCrystal_I2C.h>
RtcDS1307<TwoWire> Rtc(Wire);
const int LCD_W = 16, LCD_H = 2;
LiquidCrystal_I2C lcd(0x27, LCD_W, LCD_H);
// input pin
const int
S1 = 2, // A0
S2 = 3, // A1
S3 = 4, // A2
S4 = 5, // A3
pinbtn1 = 6, // 2
pinbtn2 = 7, // 3
pinbtn3 = 8; // 4
// output pin
const int
L1 = 13, // 5
L2 = A0, // 6
L3 = A1, // 7
L4 = A2, // 8
Alarm = 12, // 9 // alarm
Listrik = 11; // stopkontak
#include "p_btn.hpp"
bool run_once = true;
short jam_on = 0, menit_on = 0, jam_off = 0, menit_off = 0;
bool listrik_sensor = true;
bool wasError(const char* errorTopic = "")
{
uint8_t error = Rtc.LastError();
if (error != 0)
{
// we have a communications error
// see https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
// for what the number means
Serial.print("[");
Serial.print(errorTopic);
Serial.print("] WIRE communications error (");
Serial.print(error);
Serial.print(") : ");
switch (error)
{
case Rtc_Wire_Error_None:
Serial.println("(none?!)");
break;
case Rtc_Wire_Error_TxBufferOverflow:
Serial.println("transmit buffer overflow");
break;
case Rtc_Wire_Error_NoAddressableDevice:
Serial.println("no device responded");
break;
case Rtc_Wire_Error_UnsupportedRequest:
Serial.println("device doesn't support request");
break;
case Rtc_Wire_Error_Unspecific:
Serial.println("unspecified error");
break;
case Rtc_Wire_Error_CommunicationTimeout:
Serial.println("communications timed out");
break;
}
return true;
}
return false;
}
void rtc_init() {
Rtc.Begin();
#if defined(WIRE_HAS_TIMEOUT)
Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
#endif
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
if (!Rtc.IsDateTimeValid())
{
if (!wasError("setup IsDateTimeValid"))
{
Serial.println("RTC lost confidence in the DateTime!");
}
}
if (!Rtc.GetIsRunning())
{
if (!wasError("setup GetIsRunning"))
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("setup GetDateTime"))
{
if (now < compiled)
{
Serial.println("RTC is older than compile time, updating DateTime");
// Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time, this is expected");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time, while not expected all is still fine");
}
}
Rtc.SetSquareWavePin(DS1307SquareWaveOut_Low);
wasError("setup SetSquareWavePin");
}
short lcd_printCenter(const char* text, int y = 0, bool printText = true) {
const int x = (LCD_W - strlen(text)) / 2;
lcd.setCursor(x, y);
if (printText) lcd.print(text);
return x;
}
void prog_alarm() {
const bool S1_state = !digitalRead(S1);
const bool S2_state = !digitalRead(S2);
const bool S3_state = !digitalRead(S3);
const bool S4_state = !digitalRead(S4);
bool alarm_state = (S1_state || S2_state || S3_state || S4_state);
digitalWrite(Alarm, alarm_state);
}
void prog_standby() {
const bool S1_state = !digitalRead(S1);
const bool S2_state = !digitalRead(S2);
const bool S3_state = !digitalRead(S3);
const bool S4_state = !digitalRead(S4);
const char* S1_text = (S1_state) ? "ON " : "OFF";
const char* S2_text = (S2_state) ? "ON " : "OFF";
const char* S3_text = (S3_state) ? "ON " : "OFF";
const char* S4_text = (S4_state) ? "ON " : "OFF";
lcd.setCursor(0,0);
if (listrik_sensor) {
lcd.print("S1 S2 S3 S4");
} else {
lcd.print("S1. S2. S3. S4.");
}
lcd.setCursor(0,1);
lcd.print(S1_text);
lcd.print(" ");
lcd.print(S2_text);
lcd.print(" ");
lcd.print(S3_text);
lcd.print(" ");
lcd.print(S4_text);
lcd.print(" ");
digitalWrite(L1, S1_state);
digitalWrite(L2, S2_state);
digitalWrite(L3, S3_state);
digitalWrite(L4, S4_state);
}
void prog_set_on() {
static short jam = 0, menit = 0, pos = 0;
static bool blink = false;
static unsigned long lastM;
if (run_once) {
run_once = false;
jam = jam_on;
menit = menit_on;
lastM = millis();
}
const int intervalON = 500, intervalOFF = 100;
lcd.setCursor(0,0);
lcd_printCenter("Sett Waktu ON");
int x1 = lcd_printCenter("00:00", 1, false);
lcd.setCursor(x1,1);
if (jam < 10) lcd.print("0");
lcd.print(jam);
lcd.print(":");
if (menit < 10) lcd.print("0");
lcd.print(menit);
if (millis() - lastM >= intervalON) {
if (pos == 0) {
lcd.setCursor(x1,1);
}
else if (pos == 1) {
lcd.setCursor(x1 + 3,1);
}
lcd.print(" ");
if (millis() - lastM >= (intervalON + intervalOFF)) {
lastM = millis();
}
}
if(btn1.isPressing()) {
switch (pos) {
case 0: if(--jam < 0) jam = 23; break;
case 1: if(--menit < 0) menit = 59; break;
}
}
if(btn3.isPressing()) {
switch (pos) {
case 0: if(++jam > 23) jam = 0; break;
case 1: if(++menit > 58) menit = 0; break;
}
}
if (btn2.isPressing()) {
switch (pos) {
case 0: jam_on = jam; pos++; break;
case 1: menit_on = menit; pos++; break;
default:
pos = 0;
Serial.println("done");
}
}
}
void prog_set_off() {
static short jam = 0, menit = 0, pos = 0;
static bool blink = false;
static unsigned long lastM;
if (run_once) {
run_once = false;
jam = jam_off;
menit = menit_off;
lastM = millis();
}
const int intervalON = 500, intervalOFF = 100;
lcd.setCursor(0,0);
lcd_printCenter("Sett Waktu OFF");
int x1 = lcd_printCenter("00:00", 1, false);
lcd.setCursor(x1,1);
if (jam < 10) lcd.print("0");
lcd.print(jam);
lcd.print(":");
if (menit < 10) lcd.print("0");
lcd.print(menit);
if (millis() - lastM >= intervalON) {
if (pos == 0) {
lcd.setCursor(x1,1);
}
else if (pos == 1) {
lcd.setCursor(x1 + 3,1);
}
lcd.print(" ");
if (millis() - lastM >= (intervalON + intervalOFF)) {
lastM = millis();
}
}
if(btn1.isPressing()) {
switch (pos) {
case 0: if(--jam < 0) jam = 23; break;
case 1: if(--menit < 0) menit = 59; break;
}
}
if(btn3.isPressing()) {
switch (pos) {
case 0: if(++jam > 23) jam = 0; break;
case 1: if(++menit > 58) menit = 0; break;
}
}
if (btn2.isPressing()) {
switch (pos) {
case 0: jam_off = jam; pos++; break;
case 1: menit_off = menit; pos++; break;
default:
pos = 0;
Serial.println("done");
}
}
}
void prog_set_listrik() {
static bool set = listrik_sensor;
if (run_once) {
run_once = false;
set = listrik_sensor;
}
lcd.setCursor(0,0);
lcd_printCenter("Setting Sensor");
if (set) {
lcd_printCenter("ON ", 1);
} else {
lcd_printCenter("OFF", 1);
}
if (btn1.isPressing()) {
set = false;
}
if (btn3.isPressing()) {
set = true;
}
if (btn2.isPressing()) {
listrik_sensor = set;
Serial.println("done");
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(L3, OUTPUT);
pinMode(L4, OUTPUT);
pinMode(Alarm, OUTPUT);
pinMode(Listrik, OUTPUT);
pinMode(S1, INPUT_PULLUP);
pinMode(S2, INPUT_PULLUP);
pinMode(S3, INPUT_PULLUP);
pinMode(S4, INPUT_PULLUP);
pinMode(pinbtn1, INPUT);
pinMode(pinbtn2, INPUT);
pinMode(pinbtn3, INPUT);
rtc_init();
lcd.init();
lcd.backlight();
}
void loop() {
// put your main code here, to run repeatedly:
prog_alarm();
prog_set_listrik();
delay(100);
}