We're making an alarm clock using arduino uno. This alarm clock displays arithmetic problem once the alarm goes on. And as the user input the correct answer using the 4x4 keypad, the alarm goes off.
Our clock already displays the time and math problem as the alarm goes on. We're only having a problem with the keypad. It's not working with our code. We can't input the correct answer.
Here's our code. Please someone help us. Thank you.
#include "Time.h"
#include "TimeAlarms.h"
#include <Wire.h>
#include "Keypad.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(A0,A1,A2,A3,A4,A5);
int mathValOne = 0;
int mathValTwo = 0;
int mathValThree = 0;
int realAnswer = 0;
const int Buzzer = 9;
int toneOn = 0;
long previousMillis = 0;
int x = 0;
long interval = 70;
long interval2 = 500;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] =
{
{'7','8','9','/'},
{'4','5','6','X'},
{'1','2','3','-'},
{'#','0','=','+'}
};
byte rowPins[ROWS] = { 3, 4, 5, 2 };
byte colPins[COLS] = { 6, 7, 8, 9 };
Keypad kpd = Keypad( makeKeymap(keys), rowPins,
colPins, ROWS, COLS );
void setup()
{
lcd.begin(16, 2);
setTime(20, 34, 0, 10, 07, 16);
Alarm.alarmRepeat(20, 35, 0, Alaarm);
Wire.begin();
pinMode(Buzzer, OUTPUT);
}
void BuzzerOn() {
NOISE();
MathProblem();
}
void BuzzerOff() {
noTone(9);
delay(500);
Clock();
}
void NOISE() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval && x < 8) {
previousMillis = currentMillis;
if (toneOn == 0) {
toneOn = 2048;
}
else
toneOn = 0;
tone(9, toneOn);
if (toneOn == 0)
noTone(9);
x++;
}
if (currentMillis - previousMillis > interval2 && x == 8) {
previousMillis = currentMillis;
noTone(9);
x = 0;
}
}
void Clock() {
lcd.setCursor(0,0);
lcd.print(hour());
printDigits(minute());
printDigits(second());
lcd.println(" ");
}
void printDigits(int digits) {
lcd.print(":");
if (digits < 10)
lcd.print('0');
lcd.print(digits);
}
void loop()
{
lcd.setCursor(0, 0);
Clock();
Alarm.delay(1000); // wait one second between clock display
// Turn off the display:
lcd.noDisplay();
// Turn on the display:
lcd.display();
}
void Alaarm() {
BuzzerOn();
}
void MathProblem() {
mathValOne = random(11, 17);
mathValTwo = random(7, 10);
mathValThree = random(20, 80);
realAnswer = mathValOne * mathValTwo + mathValThree;
delay(500);
lcd.setCursor(0, 1);
lcd.print(mathValOne);
lcd.print("*");
lcd.print(mathValTwo);
lcd.print("+");
lcd.print(mathValThree);
lcd.print(" = ");
char key
= kpd.getKey();
if(key)
{
lcd.print(key);
delay(100);
}
if (realAnswer != key) {
MathProblem();
}
if (realAnswer == key) {
BuzzerOff();
delay(500);
}
}