#include <Ticker.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <WiFi.h>
#include "time.h"
#define OLED_RESET -1
#define row 4 // four rows
#define col 4 // four columns
#define LED 13
String str;
String Alarm[100];
int click = 0;
int row_click = 0;
int col_click = 0;
int counter_delay = 0;
bool capslock;
char keys[row][col] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte row_pin[row] = {18, 5, 32, 33}; // GIOP19, GIOP5, GIOP32, GIOP33 connect to the row pins
byte col_pin[col] = {25, 26, 27, 14}; // GIOP25, GIOP26, GIOP27, GIOP14 connect to the column pins
Ticker timer;
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// create an OLED display object connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
const char* ssid = "adzkiakhns";
const char* password = "Creamy123";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 3600;
int N = 0;
String code = "A";
int kedip = 0;
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
for (int i = 0; i < 4; i++) {
pinMode(col_pin[i], INPUT_PULLUP);
pinMode(row_pin[i], OUTPUT);
}
Serial.println(ssid);
// WiFi.begin(ssid, password);
WiFi.begin("Adzkia Khansa:", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
display.clearDisplay();
display.setTextSize(2); // set text size
display.setTextColor(WHITE); // set text color
display.setCursor(0,0);
display.println("Adzkia Khansa Meara");
display.setTextSize(1);
display.setCursor(0, 16);
display.print("21/473474/TK/52182");
display.display();
display.clearDisplay();
delay(3000);
// Init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void timerCallback() {
counter_delay++;
}
void loop() {
getKey();
digitalWrite(LED, HIGH);
}
void printAlarm() {
display.clearDisplay();
display.setTextSize(2); // set text size
display.setTextColor(WHITE); // set text color
display.setCursor(0, 2);
String temp = Alarm[0];
String jam = temp.substring(0,2);
Serial.println(jam);
display.println("List Alarm");
for (int i = 0; i < N; i++) {
display.println(Alarm[i]); // set text
}
display.display();
}
char timeHour[3];
char timeMinute[3];
char timeSecond[3];
void printLocalTime() {
delay(1000);
checkAlarm();
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
display.clearDisplay();
display.setTextSize(2); // set text size
display.setTextColor(WHITE); // set text color
display.setCursor(0, 2);
display.println( &timeinfo, "%A, %B %d %Y %H:%M:%S");
display.display();
strftime(timeHour, 3, "%H", &timeinfo);
strftime(timeMinute, 3, "%M", &timeinfo);
strftime(timeSecond, 3, "%S", &timeinfo);
char timeWeekDay[10];
strftime(timeWeekDay, 10, "%A", &timeinfo);
}
void addAlarm() {
display.clearDisplay();
display.setTextSize(2); // set text size
display.setTextColor(WHITE); // set text color
display.setCursor(0, 2);
display.println("Input Alarm Time:");
int panjangStr = str.length();
if (panjangStr == 2 || panjangStr == 5) {
str += ":";
}
display.println(str);
display.display();
}
bool nyala = false;
void checkAlarm(){
Serial.println("timeHourcheck");
for(int i=0;i<N;i++){
String temp = Alarm[i];
String jam = temp.substring(0,2);
String menit = temp.substring(3,5);
String detik = temp.substring(6,8);
String timeHourcheck(timeHour);
String timeMinutecheck(timeMinute);
String timeSecondcheck(timeSecond);
if(nyala && kedip < 26){
digitalWrite(LED, HIGH);
delay(400);
digitalWrite(LED, LOW);
delay(400);
kedip++;
}else {
nyala = false;
kedip = 0;
}
if(timeHourcheck == jam && timeMinutecheck == menit && timeSecondcheck == detik && kedip < 26){
digitalWrite(LED, HIGH);
delay(400);
digitalWrite(LED, LOW);
delay(400);
kedip++;
nyala = true;
}
}
}
void getKey() {
if(code == "A"){
printLocalTime();
}else if(code == "B"){
addAlarm();
}else if(code == "C"){
printAlarm();
}
//printLocalTime();
for (int r = 0; r < 4; r++) {
digitalWrite(row_pin[r], LOW);
for (int i = 0; i < 4; i++) {
if (i != r) digitalWrite(row_pin[i], HIGH);
}
for (int c = 0; c < 4; c++) {
if (digitalRead(col_pin[c]) == LOW) {
if (c == 3 && r == 0) {
code = "A";
} else if (c == 3 && r == 1) {
code = "B";
} else if (c == 3 && r == 2) {
code = "C";
} else if (c == 3 && r == 3) {
Alarm[N] = str;
N++;
str = "";
}else if(c == 2 && r == 3){
str.remove(str.length() - 1);
} else {
str = str + keys[r][c];
}
delay(400);
}
}
}
}