// LCD1602 custom characters example
#include <LiquidCrystal.h>
#include <Arduino.h>
#include "RTClib.h"
#include "pitch.h"
RTC_DS1307 rtc;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const byte buttonPins[] = {0, 2, 4};
#define SPEAKER_PIN 5
uint8_t btn_prev0;
uint8_t btn_prev1;
uint8_t btn_prev2;
int buffer;
int buffer2;
char bufftime[50];
char bufftime2[50];
bool ClockState = true;
bool AlarmState = false;
bool SetState = false;
bool ProbState = false;
int SetHour = 0;
int SetMin = 0;
int SetSec = 0;
int randomint1;
int randomint2;
int RealAnswer;
int SetAnswer;
int ProbCount = 1;
int firstdigit = 10;
int seconddigit = 10;
int thirddigit = 10;
uint8_t one[8] = {
0b00000,
0b10000,
0b10000,
0b10000,
0b10000,
0b10000,
0b10000,
0b00000,
};
uint8_t two[8] = {
0b00000,
0b10100,
0b10100,
0b10100,
0b10100,
0b10100,
0b10100,
0b00000,
};
uint8_t three[8] = {
0b00000,
0b10101,
0b10101,
0b10101,
0b10101,
0b10101,
0b10101,
0b00000,
};
uint8_t heart[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000,
};
uint8_t tick[8] = {
0b00000,
0b00000,
0b00001,
0b00010,
0b10100,
0b01000,
0b00000,
0b00000,
};
uint8_t cross[8] = {
0b00000,
0b00000,
0b10001,
0b01010,
0b00100,
0b01010,
0b10001,
0b00000,
};
void setup() {
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
for (byte i = 0; i < 3; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
btn_prev0 = HIGH;
btn_prev1 = HIGH;
btn_prev2 = HIGH;
randomSeed(analogRead(A0));
lcd.createChar(6, one);
lcd.createChar(1, two);
lcd.createChar(2, three);
lcd.createChar(3, heart);
lcd.createChar(4, tick);
lcd.createChar(5, cross);
lcd.begin(16, 2);
lcd.setCursor(4,0);
lcd.print("Hello! \x03");
}
void loop() {
DateTime now2 = rtc.now();
int TimeHour2 = now2.hour();
int TimeMin2 = now2.minute();
int TimeSec2 = now2.second();
static unsigned long lastrun;
if (ClockState == true) {
if (millis() - lastrun >= 50) {
lastrun=millis();
timeupdate();
if (SetState == true) {
setupdate();
}
else {
alarmupdate();
}
}
}
else {
game();
}
int value0 = digitalRead(buttonPins[0]);
int value1 = digitalRead(buttonPins[1]);
int value2 = digitalRead(buttonPins[2]);
if (value0 == LOW && btn_prev0 == HIGH)
{
Serial.println("0 pressed");
if (ClockState == true) {
if(SetState == true){
delay(100);
if (SetHour < 24-1) {
SetHour++;
}
else {
SetHour = 0;
}
}
else{
delay(100);
if (ProbCount > 1) {
ProbCount--;
}
else {
ProbCount = 3;
}
}
}
else {
delay(100);
if (firstdigit< 9) {
firstdigit++;
}
else {
firstdigit = 0;
}
}
}
btn_prev0 = digitalRead(buttonPins[0]);
if (value1 == LOW && btn_prev1 == HIGH)
{
Serial.println("1 pressed");
if (ClockState == true) {
if(SetState == true){
delay(100);
if (SetMin < 60-1) {
SetMin++;
}
else {
SetMin = 0;
SetHour++;
}
}
else{
delay(100);
if (ProbCount < 3) {
ProbCount++;
}
else {
ProbCount = 1;
}
}
}
else {
delay(100);
if (seconddigit< 9) {
seconddigit++;
}
else {
seconddigit = 0;
}
}
}
btn_prev1 = digitalRead(buttonPins[1]);
if (value2 == LOW && btn_prev2 == HIGH)
{
Serial.println("2 pressed");
if (ClockState == true) {
if(SetState == false) {
delay(100);
SetState = true;
tone(SPEAKER_PIN, NOTE_E5);
delay(150);
noTone(SPEAKER_PIN);
}
else {
delay(100);
SetState = false;
if (SetHour == 0 && SetMin == 0){
AlarmState = false;
tone(SPEAKER_PIN, NOTE_C5);
delay(150);
noTone(SPEAKER_PIN);
}
else {
AlarmState = true;
tone(SPEAKER_PIN, NOTE_E5);
delay(150);
noTone(SPEAKER_PIN);
}
}
}
else {
delay(100);
if (thirddigit< 9) {
thirddigit++;
}
else {
thirddigit = 0;
}
}
}
btn_prev2 = digitalRead(buttonPins[2]);
if (AlarmState == true && SetHour == TimeHour2 && SetMin == TimeMin2) {
ClockState = false;
}
if (AlarmState == true && ClockState == false) {
SetAnswer = firstdigit*100 + seconddigit*10 + thirddigit;
if (SetAnswer == RealAnswer) {
ProbCount--;
if (ProbCount == 0){
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Well Played!!");
tone(SPEAKER_PIN, NOTE_A4);
delay(200);
tone(SPEAKER_PIN, NOTE_G4);
delay(100);
tone(SPEAKER_PIN, NOTE_A4);
delay(300);
tone(SPEAKER_PIN, NOTE_C5);
delay(200);
tone(SPEAKER_PIN, NOTE_D5);
delay(200);
tone(SPEAKER_PIN, NOTE_C5);
delay(600);
tone(SPEAKER_PIN, NOTE_A4);
delay(200);
tone(SPEAKER_PIN, NOTE_G4);
delay(100);
tone(SPEAKER_PIN, NOTE_F4);
delay(300);
tone(SPEAKER_PIN, NOTE_D4);
delay(200);
tone(SPEAKER_PIN, NOTE_A4);
delay(200);
tone(SPEAKER_PIN, NOTE_G4);
delay(600);
noTone(SPEAKER_PIN);
delay(400);
ClockState = true;
AlarmState = false;
SetState = false;
ProbState = false;
SetHour = 0;
SetMin = 0;
SetSec = 0;
firstdigit = 10;
seconddigit = 10;
thirddigit = 10;
ProbCount = 1;
}
else {
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("More!?");
tone(SPEAKER_PIN, NOTE_A4);
delay(150);
noTone(SPEAKER_PIN);
delay(850);
randomint1 = random(100,500);
randomint2 = random(200,450);
RealAnswer = randomint1 + randomint2;
ProbState = true;
firstdigit = 10;
seconddigit = 10;
thirddigit = 10;
}
}
}
static unsigned long lastrun2;
if (millis() - lastrun2 >= 2000) {
lastrun2=millis();
if (ClockState == false) {
tone(SPEAKER_PIN, NOTE_G4);
delay(100);
tone(SPEAKER_PIN, NOTE_D4);
delay(100);
tone(SPEAKER_PIN, NOTE_G5);
delay(100);
noTone(SPEAKER_PIN);
}
}
}
void timeupdate() {
DateTime now = rtc.now();
int TimeHour = now.hour();
int TimeMin = now.minute();
int TimeSec = now.second();
buffer = sprintf(bufftime, "%02d:%02d:%02d", TimeHour,TimeMin,TimeSec);
// Serial.print("Time: ");
// Serial.println(bufftime);
lcd.setCursor(0, 0);
for (int i = 0; i < 16; ++i) {
lcd.write(" ");
}
lcd.setCursor(4, 0);
lcd.print(bufftime);
lcd.setCursor(15, 0);
if (ProbCount == 1) {
lcd.print("\x06");
}
if (ProbCount == 2) {
lcd.print("\x01");
}
if (ProbCount == 3) {
lcd.print("\x02");
}
}
void setupdate() {
buffer2 = sprintf(bufftime2, ">> %02d:%02d:%02d <<", SetHour,SetMin,SetSec);
// Serial.print("Set: ");
// Serial.println(bufftime);
lcd.setCursor(0, 1);
for (int i = 0; i < 16; ++i) {
lcd.write(" ");
}
lcd.setCursor(1, 1);
lcd.print(bufftime2);
}
void alarmupdate() {
lcd.setCursor(0, 1);
for (int i = 0; i < 16; ++i) {
lcd.write(" ");
}
if (AlarmState == true) {
lcd.setCursor(3,1);
lcd.print("Alarm set \x04");
}
else {
lcd.setCursor(3,1);
lcd.print("Alarm set \x05");
}
}
void game() {
if(ProbState != true) {
randomint1 = random(100,500);
randomint2 = random(200,450);
RealAnswer = randomint1 + randomint2;
ProbState = true;
}
// Serial.println("GAME");
lcd.setCursor(0, 0);
for (int i = 0; i < 16; ++i) {
lcd.write(" ");
}
lcd.setCursor(1,0);
lcd.print(randomint1);
lcd.print(" + ");
lcd.print(randomint2);
lcd.print(" = ");
lcd.print((char)219);
lcd.setCursor(0, 1);
for (int i = 0; i < 16; ++i) {
lcd.write(" ");
}
lcd.setCursor(4,1);
lcd.print("Ans: ");
lcd.setCursor(9,1);
if (firstdigit == 10) {
lcd.print((char)95);
}
else {
lcd.print(firstdigit);
}
lcd.setCursor(10,1);
if (seconddigit == 10) {
lcd.print((char)95);
}
else {
lcd.print(seconddigit);
}
lcd.setCursor(11,1);
if (thirddigit == 10) {
lcd.print((char)95);
}
else {
lcd.print(thirddigit);
}
}