#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 //OLEDcdisaplay width , in pixels
#define SCREEN_HEIGHT 64 // OLED dispaly height , in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C /// < See datasheet for address ; 0x3D for 128x64 , 0x3C for 128x32
//Declare objects
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//Global Variables
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
unsigned timeNow = 0;
unsigned timeLast = 0;
bool alarm_enabled = true;
int n_alarms = 2;
int alarm_hours[] = {0, 1}; //initial alarm hours
int alarm_minutes[] = {1, 10};
int alarm_triggered[] = {false, false}; //keep track alarm is turn off
#define BUZZER 5
#define LED_1 15
#define PB_CANCEL 34
int n_notes = 8;
int C = 262;
int D = 294;
int E = 330;
int F = 349;
int G = 392;
int A = 440;
int B = 494;
int C_H = 523;
int notes[8] = {C, D, E, F, G, A, B, C_H};
void setup() {
pinMode(BUZZER, OUTPUT);
pinMode(LED_1, OUTPUT);
pinMode(PB_CANCEL, OUTPUT);
Serial.begin(9600);
//put the setup code here to run once:
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation faild"));
for (;;); //Don't procced , loop forever
}
//show initial display buffer contents on the screen---
//the library initializes this with an Adafruit splash screen
display.display();
delay(2000); // pause for 2 seconds
//clear the buffer
display.clearDisplay();
print_line("Welcome to medibox", 10, 20, 2);
display.clearDisplay();
}
void loop() {
update_time_with_check_alarm();
//delay(10); // this speeds up the simulation
}
void print_line(String text, int column, int row, int text_size) {
display.setTextSize(text_size); // Noraml 1:1 pixel scale
display.setTextColor(SSD1306_WHITE);
display.setCursor(column, row);
display.println(text);
display.display();
//delay(2000);
}
void print_time_now(void) {
display.clearDisplay();
print_line(String(days), 0, 0, 2); //called casting convert strings values into string
print_line(":", 20, 0, 2);
print_line(String(hours), 30, 0, 2);
print_line(":", 50, 0, 2);
print_line(String(minutes), 60, 0, 2);
print_line(":", 80, 0, 2);
print_line(String(seconds), 90, 0, 2);
}
void update_time() {
timeNow = millis() / 1000; //seonds pass from bootup//millis Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
seconds = timeNow - timeLast;// print vema seconds gana eka equal venava time now - time last ta tme last increase karanava every minuits sarayakma thappara sankaya valin
if (seconds >= 60) {
minutes += 1 ;
timeLast += 60;
}
if (minutes == 60) {
hours += 1;
minutes = 0;
}
if (hours == 24) {
days += 1;
hours = 0;
}
}
void ring_alarm() {
display.clearDisplay();
print_line("MEDICINE TIME!", 0, 0, 2);
digitalWrite(LED_1, HIGH); //ligh up the LE
bool break_happen = false;//to stop while statment
//Ring the BUZZER
while (break_happen == false && digitalRead(PB_CANCEL) == HIGH) {
for (int i = 0; i <=n_notes; i++) {
if (digitalRead(PB_CANCEL) == LOW) {
delay(200);//prevent bouncing of the push button //while need to be stop >fast push kale nathotho aye aye ebenavane
break_happen = true;
break;//stop for statement
}
tone(BUZZER, notes[i]);
delay(500);
noTone(BUZZER);
delay(2);
}
}
digitalWrite(LED_1, LOW);
display.clearDisplay();
}
void update_time_with_check_alarm(void) {
update_time();
print_time_now();
if (alarm_enabled == true) {
for (int i = 0; i < n_alarms; i++) {
if (alarm_triggered[i] == false && alarm_hours[i] == hours && alarm_minutes[i] == minutes) {
ring_alarm();
}
}
}
}