Hi. I’m trying to make an alarm clock that would display a math equation and you have to solve that equation correctly before the alarm sound stops. The code below is not yet finish. I’m trying to do it part by part. So first I did an LCD and push buttons program that would display a number that would increase or decrease based on the button pushed. It worked. Then I add the clock display using time.h library. Everything is fine, the clock is ticking and the number is still there increasing/decreasing in every push.
But the problem started when I tried to add the alarm part where when you reached the certain time, it would display “ALAAAARM!” and would be able to let you increase/decrease the number by pushing buttons. Clock displaying and ticking, yes. Number displaying and decreasing/increasing, no. Displays “ALAAAARM!”, no. Here is where I stopped editing my code. Anyone who saw a wrong thing or think something is wroung about my code and project, please tell me what you think. It would be a great help. Thanks!
#include <LiquidCrystal.h>
#include "Time.h"
LiquidCrystal lcd(12,11,5,4,3,2);
int k;
const int buttonPin = 8; // the pin that the Up pushbutton is attached to
const int buttonPin1 = 9; // the pin that the Down pushbutton is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState5 = 0; // current state of the button
int buttonState6 = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void Clock() {
lcd.noDisplay();
lcd.display();
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 setup() {
// initialize the button pin as a input:
setTime(20,34,0,10,07,16);
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
lcd.begin(16,2);
lcd.setCursor(0,1);
}
void loop() {
while(k != 1) {
lcd.setCursor(0, 0);
Clock();
if (hour() == 20 && minute() == 34 && second() == 5) {
lcd.noDisplay();
k == 1;
}
}
while(k == 1) {
lcd.noDisplay();
lcd.display();
lcd.setCursor(0, 0);
lcd.print("ALAAAAARM!");
buttonState5 = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState5 != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState5 == HIGH)
{
delay(100);
buttonPushCounter++;
lcd.setCursor(12,1);
lcd.print(buttonPushCounter);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState5;
// read the pushbutton down input pin:
buttonState6 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState6 != lastButtonState) {
// if the state has changed, decrement the counter
if (buttonState6 == HIGH)
{
delay(100);
buttonPushCounter--;
lcd.setCursor(12,1);
lcd.print(buttonPushCounter);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState6;
}
}