#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
//#include <Wire.h>
#include "pitches.h"
#define SERIAL_OPTION 0
LiquidCrystal_I2C lcd(0x27,16,2);
RTC_DS1307 RTC;
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
#define melodyPin 3
//Mario main theme melody
int melody[] = {
NOTE_E7, NOTE_E7, 0, NOTE_E7,
0, NOTE_C7, NOTE_E7, 0,
NOTE_G7, 0, 0, 0,
NOTE_G6, 0, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0
};
//Mario main them tempo
int tempo[] = {
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
};
//Underworld melody
int underworld_melody[] = {
NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
NOTE_AS3, NOTE_AS4, 0,
0,
NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
NOTE_AS3, NOTE_AS4, 0,
0,
NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
NOTE_DS3, NOTE_DS4, 0,
0,
NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
NOTE_DS3, NOTE_DS4, 0,
0, NOTE_DS4, NOTE_CS4, NOTE_D4,
NOTE_CS4, NOTE_DS4,
NOTE_DS4, NOTE_GS3,
NOTE_G3, NOTE_CS4,
NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
NOTE_GS4, NOTE_DS4, NOTE_B3,
NOTE_AS3, NOTE_A3, NOTE_GS3,
0, 0, 0
};
//Underwolrd tempo
int underworld_tempo[] = {
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
3,
12, 12, 12, 12,
12, 12, 6,
6, 18, 18, 18,
6, 6,
6, 6,
6, 6,
18, 18, 18, 18, 18, 18,
10, 10, 10,
10, 10, 10,
3, 3, 3
};
int buzzer = 3;
int song = 0;
int x = A0; //Saída analógica (Eixo X)
int y = A1;
int estadobotao=0;
int estadobotao2=0;
int estadobotao3=0;
int estadobotao4=0;
int estadobotao5=0;
int estadobotao6=0;
int hora =00;
int horaC=00;
int minuto=00;
int minutoC=00;
int hora2 =00;
int hora2C=00;
int minuto2=00;
int minuto2C=00;
int hora3 =00;
int hora3C=00;
int minuto3=00;
int minuto3C=00;
int led1=0;
int led2=0;
int led3=0;
int alarme1=0;
int alarme2=0;
int alarme3=0;
int tela=0;
int cont=0;
int cont2=7;
int cont3=14;
void setup() {
pinMode(13, INPUT);
pinMode(12, INPUT);
pinMode(7, INPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, INPUT);
if (SERIAL_OPTION) Serial.begin(9600);
lcd.init();
lcd.backlight();
RTC.begin();
}
void sing(int s) {
// iterate over the notes of the melody:
song = s;
if (song == 2) {
Serial.println(" 'Underworld Theme'");
int size = sizeof(underworld_melody) / sizeof(int);
for (int thisNote = 0; thisNote < size; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / underworld_tempo[thisNote];
buzz(melodyPin, underworld_melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
buzz(melodyPin, 0, noteDuration);
}
} else {
Serial.println(" 'Mario Theme'");
int size = sizeof(melody) / sizeof(int);
for (int thisNote = 0; thisNote < size; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / tempo[thisNote];
buzz(melodyPin, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
buzz(melodyPin, 0, noteDuration);
}
}
}
void buzz(int targetPin, long frequency, long length) {
digitalWrite(13, HIGH);
long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait again or the calculated delay value
}
digitalWrite(13, LOW);
}
void lerbotoes(){
estadobotao=digitalRead(8);
estadobotao2=digitalRead(13);
estadobotao3=digitalRead(12);
estadobotao4=digitalRead(11);
estadobotao5=digitalRead(7);
estadobotao6=digitalRead(2);
}
void setar_alarme(){
DateTime now=RTC.now();
if (tela==4){
horaC=hora;
minutoC=minuto;
alarme1=(now.hour()*60)+now.minute();
alarme1=alarme1 + ((horaC*60)+minutoC);
}
if (tela==5){
hora2C=hora2;
minuto2C=minuto2;
alarme2=(now.hour()*60)+now.minute();
alarme2=alarme2 + ((hora2C*60)+minuto2C);
}
if (tela==6){
hora3C=hora3;
minuto3C=minuto3;
alarme3=(now.hour()*60)+now.minute();
alarme3=alarme3 + ((hora3C*60)+minuto3C);
}
}
void tocar_alarme(){
DateTime now=RTC.now();
if (horaC != 0 || minutoC != 0){
if (alarme1==((now.hour()*60)+now.minute())){
alarme1=alarme1 + ((horaC*60)+minutoC);
cont = cont+1;
tela = 8;
lcd.clear();
atualiza_display();
if (cont>7){
cont=1;
}
tone(3, HIGH);
while (tela!=0){
lerbotoes();
if (estadobotao6==1){
noTone(3);
tela=0;
}
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(500);
digitalWrite(led1, HIGH);
digitalWrite(led1, LOW);
}
}
}
if (hora2C != 0 || minuto2C != 0){
if (alarme2==((now.hour()*60)+now.minute())){
alarme2=alarme2 + ((hora2C*60)+minuto2C);
cont2 = cont2+1;
tela = 9;
lcd.clear();
if (cont2>14){
cont2=8;
}
tone(3, HIGH);
while (tela!=0){
lerbotoes();
if (estadobotao6==1){
noTone(3);
tela=0;
}
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
delay(500);
digitalWrite(led2, HIGH);
digitalWrite(led2, LOW);
}
}
}
if (hora3C != 0 || minuto3C != 0){
if (alarme3==((now.hour()*60)+now.minute())){
alarme3=alarme3 + ((hora3C*60)+minuto3C);
cont3 = cont3+1;
tela = 10;
lcd.clear();
if (cont3>21){
cont3=15;
}
tone(3, HIGH);
while (tela!=0){
lerbotoes();
if (estadobotao6==1){
noTone(3);
tela=0;
}
digitalWrite(led3, HIGH);
delay(500);
digitalWrite(led3, LOW);
delay(500);
digitalWrite(led3, HIGH);
digitalWrite(led3, LOW);
}
}
}
}
void atualiza_display() {
if (tela==1) {
lcd.setCursor(0,0); lcd.print("Definir Alarme 1");lcd.write(1);lcd.write(1);
lcd.setCursor(0,1);
lcd.print(hora);
lcd.print(":");
lcd.print(minuto);
lcd.print(" em ");
lcd.print(hora);
lcd.print(":");
lcd.print(minuto);
lcd.print("Hr");
}
if (tela==2) {
lcd.setCursor(0,0); lcd.print("Definir Alarme 2");lcd.write(1);lcd.write(1);
lcd.setCursor(0,1);
lcd.print(hora2);
lcd.print(":");
lcd.print(minuto2);
lcd.print(" em ");
lcd.print(hora2);
lcd.print(":");
lcd.print(minuto2);
lcd.print("Hr");
}
if (tela==3) {
lcd.setCursor(0,0); lcd.print("Definir Alarme 3");lcd.write(1);lcd.write(1);
lcd.setCursor(0,1);
lcd.print(hora3);
lcd.print(":");
lcd.print(minuto3);
lcd.print(" em ");
lcd.print(hora3);
lcd.print(":");
lcd.print(minuto3);
lcd.print("Hr");
}
if (tela==0) {
DateTime now=RTC.now();
char dia[3]; char mes[3];
sprintf(dia,"%02d",now.day());sprintf(mes,"%02d",now.month());
lcd.setCursor(0,0); lcd.print("DATA: ");
lcd.print(dia); lcd.print("/"); lcd.print(mes); lcd.print("/"); lcd.print(now.year());
char hour[3]; char minute[3]; char second[3];
sprintf(hour,"%02d",now.hour());sprintf(minute,"%02d",now.minute());sprintf(second,"%02d",now.second());
lcd.setCursor(0,1); lcd.print("HORA: ");
lcd.print(hour); lcd.print(":"); lcd.print(minute); lcd.print(":"); lcd.print(second);
}
if (tela==4){
lcd.setCursor(0,0); lcd.print("Definir LED");
lcd.setCursor(0,1); lcd.print("Alarme1 - LED:");lcd.print(led1);
}
if (tela==5){
lcd.setCursor(0,0); lcd.print("Definir LED");
lcd.setCursor(0,1); lcd.print("Alarme2 - LED:");lcd.print(led2);
}
if (tela==6){
lcd.setCursor(0,0); lcd.print("Definir LED");
lcd.setCursor(0,1); lcd.print("Alarme3 - LED:");lcd.print(led3);
}
if (tela==7){
lcd.print("Alarme Definido! LED: ");lcd.print(led1);
}
if (tela==8){
lcd.setCursor(0,0);lcd.print("Alarme 1!");
lcd.setCursor(0,1);lcd.print("Pegar Caixa ");lcd.print(cont);
}
if (tela==9){
lcd.setCursor(0,0);lcd.print("Alarme 2!");
lcd.setCursor(0,1);lcd.print("Pegar Caixa ");lcd.print(cont2);
}
if (tela==10){
lcd.setCursor(0,0);lcd.print("Alarme 3!");
lcd.setCursor(0,1);lcd.print("Pegar Caixa ");lcd.print(cont3);
}
}
void loop() {
lerbotoes();
atualiza_display();
tocar_alarme();
if (analogRead(x)>1000 && tela!= 4 && tela!= 5 && tela!= 6 && tela!= 7 && tela!= 8 && tela!= 9 && tela!= 10){
lcd.clear();
tela++;
delay(100);
if (tela>3){
tela=3;
}
delay(100);
}
if (analogRead(x)<10 && tela!= 4 && tela!= 5 && tela!= 6 && tela!= 7 && tela!= 8 && tela!= 9 && tela!= 10){
lcd.clear();
tela=tela - 1;
delay(100);
if (tela<0){
tela=0;
}
delay(100);
}
if (estadobotao2==1){
if (tela==1){
hora = hora+1;
atualiza_display();
delay(100);
if (hora > 24){
hora=0;
}
}
if (tela==2){
hora2 = hora2+1;
atualiza_display();
delay(100);
if (hora2 > 24){
hora2=0;
}
}
if (tela==3){
hora3 = hora3+1;
atualiza_display();
delay(100);
if (hora3 > 24){
hora3=0;
}
}
if (tela==4){
led1 = led1 +1;
delay(100);
atualiza_display();
if (led1>3){
led1 =0;
}
}
if (tela==5){
led2 = led2 +1;
delay(100);
atualiza_display();
if (led2>3){
led2 =0;
}
}
if (tela==6){
led3 = led3 +1;
delay(100);
atualiza_display();
if (led3>3){
led3 =0;
}
}
delay(50);
}
if (estadobotao3==1){
if (tela==1){
minuto = minuto+1;
atualiza_display();
delay(100);
if (minuto > 59){
minuto=0;
}
}
if (tela==2){
minuto2 = minuto2+1;
atualiza_display();
delay(100);
if (minuto2 > 59){
minuto2=0;
}
}
if (tela==3){
minuto3 = minuto3+1;
atualiza_display();
delay(100);
if (minuto3 > 59){
minuto3=0;
}
}
delay(50);
}
if (estadobotao5==1){
if (tela==1){
tela = 4;
lcd.clear();
atualiza_display();
}
if (tela==2){
tela = 5;
lcd.clear();
atualiza_display();
}
if (tela==3){
tela = 6;
lcd.clear();
atualiza_display();
}
if (tela==4){
if (led1==1){
led1=6;
lcd.clear();
lcd.print("Alarme definido");
delay(3000);
lcd.clear();
setar_alarme();
tela = 0;
atualiza_display();
}
if (led1==2){
led1=5;
lcd.clear();
lcd.print("Alarme definido");
delay(3000);
lcd.clear();
setar_alarme();
tela = 0;
atualiza_display();
}
if (led1==3){
led1=4;
lcd.clear();
lcd.print("Alarme definido");
delay(3000);
lcd.clear();
setar_alarme();
tela = 0;
atualiza_display();
}
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(500);
digitalWrite(led1, HIGH);
digitalWrite(led1, LOW);
}
if (tela==5){
if (led2==1){
led2=6;
lcd.clear();
lcd.print("Alarme definido");
delay(5000);
setar_alarme();
tela = 0;
atualiza_display();
}
if (led2==2){
led2=5;
lcd.clear();
lcd.print("Alarme definido");
delay(5000);
setar_alarme();
tela = 0;
atualiza_display();
}
if (led2==3){
led2=4;
lcd.clear();
lcd.print("Alarme definido");
delay(5000);
setar_alarme();
tela = 0;
atualiza_display();
}
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
delay(500);
digitalWrite(led2, HIGH);
digitalWrite(led2, LOW);
}
if (tela==6){
if (led3==1){
led3=6;
lcd.clear();
lcd.print("Alarme definido");
delay(5000);
setar_alarme();
tela = 0;
atualiza_display();
}
if (led3==2){
led3=5;
lcd.clear();
lcd.print("Alarme definido");
delay(5000);
setar_alarme();
tela = 0;
atualiza_display();
}
if (led3==3){
led3=4;
lcd.clear();
lcd.print("Alarme definido");
delay(5000);
setar_alarme();
tela = 0;
atualiza_display();
}
digitalWrite(led3, HIGH);
delay(500);
digitalWrite(led3, LOW);
delay(500);
digitalWrite(led3, HIGH);
digitalWrite(led3, LOW);
}
delay(50);
}
if (estadobotao6==1 && tela != 0 && tela != 1 && tela != 2 && tela != 3 && tela != 4 && tela != 5 && tela != 6 && tela != 7){
noTone(3);
tela=0;
atualiza_display();
}
}