# time won't stop ticking and won't proceed to the alarm

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!");

// 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:

// 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;
}

}
``````
``````k == 1;
``````

Oops.

Delta_G:

``````k == 1;
``````

Oops.

what shoul I do then?

what shoul I do then?

Fix it.
Why would you want a naked comparison there?

njmsjmdct:
what shoul I do then?

Delta_G:

Here’s my final code. I can’t compile it. Here’s the error message:

Arduino: 1.6.4 (Windows 8), Board: “Arduino Uno”

C:\Users\SPONGEBOB~1\AppData\Local\Temp\build163822235456560492.tmp/core.a(wiring.c.o): In function `delay': C:\Users\Spongebob\Downloads\Desktop\arduino-1.6.4\hardware\arduino\avr\cores\arduino/wiring.c:114: undefined reference to `yield’
collect2.exe: error: ld returned 1 exit status
Error compiling.

“Show verbose output during compilation”
enabled in File > Preferences.

``````#include <LiquidCrystal.h>
#include "Time.h"

LiquidCrystal lcd(12,11,5,4,3,2);

int k;

int mathValOne = random(1, 10);
int mathValTwo = random(2, 5);
int mathValThree = random(20, 25);
int realAnswer = mathValOne * mathValTwo + mathValThree;

const int Buzzer = 9;
int toneOn = 0;
long previousMillis = 0;
int x = 0;
long interval = 70;
long interval2 = 500;

const int  buttonPin = 8;    // the pin that the Up pushbutton is attached to

const int  buttonPin1 = 13;    // the pin that the Down pushbutton is attached to

const int  buttonOk = 10;

// Variables will change:

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState5 = 0;         // current state of the button
int buttonState6 = 0;
int buttonState7 = 0;
int lastButtonState = 0;     // previous state of the button

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.clear();
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);

pinMode(buttonOk, INPUT);

pinMode(Buzzer, OUTPUT);

lcd.begin(16,2);

lcd.setCursor(0,1);

}

void loop() {

while(k != 1) {
lcd.setCursor(0, 0);
Clock();

if (hour() == 20 && minute() == 34 && second() == 20) {
lcd.noDisplay();
(k = 1);
}
}

while(k == 1) {
NOISE();
lcd.setCursor(0, 0);
lcd.display();
lcd.print("GOOD MORNING~~");
lcd.setCursor(0, 1);
lcd.print(mathValOne);
lcd.print("*");
lcd.print(mathValTwo);
lcd.print("+");
lcd.print(mathValThree);
lcd.print(" = ");

if (buttonState5 != lastButtonState) {

if (buttonState5 == HIGH)

{
delay(200);
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:

// compare the buttonState to its previous state

if (buttonState6 != lastButtonState) {

// if the state has changed, decrement the counter

if (buttonState6 == HIGH)

{
delay(200);
buttonPushCounter--;
lcd.setCursor(12,1);

lcd.print(buttonPushCounter);
}
}
int input = buttonPushCounter;
if (buttonState7 == HIGH && input == realAnswer)
{
noTone(9);
(k = 0);
}

}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState6;
}
``````

njmsjmdct:
Here's my final code.

Perhaps you meant current posted code as I'm sure you likely modified it by now.

``````(k = 0);
``````

Why the useless parenthesis?

You must have something set up wrong in the IDE if you are getting an error from a core function like delay.

So here’s another modified code. The alarm supposedly won’t stop unless you the realAnswer matches the pushButtonCounter. But in this case, the alarm would stop after clicking the Ok button several times. It also stops after a number of wrong attempts. What could be the problem?

``````#include <LiquidCrystal.h>
#include "Time.h"

LiquidCrystal lcd(12,11,5,4,3,2);

int k;

int mathValOne = random(1, 10);
int mathValTwo = random(2, 5);
int mathValThree = random(20, 25);

const int Buzzer = 9;
int toneOn = 0;
long previousMillis = 0;
int x = 0;
long interval = 70;
long interval2 = 500;

const int  buttonPin = 8;    // the pin that the Up pushbutton is attached to

const int  buttonPin1 = 13;    // the pin that the Down pushbutton is attached to

const int  buttonOk = 10;

// Variables will change:

int buttonPushCounter = 28;   // counter for the number of button presses
int buttonState5 = 0;         // current state of the button
int buttonState6 = 0;
int buttonState7 = 0;
int lastButtonState = 0;     // previous state of the button

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.clear();
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);

pinMode(buttonOk, INPUT);

pinMode(Buzzer, OUTPUT);

lcd.begin(16,2);

lcd.setCursor(0,1);

}

void loop() {

while(k != 1) {
lcd.setCursor(0, 0);
Clock();

if (hour() == 20 && minute() == 34 && second() == 3) {
lcd.noDisplay();
(k = 1);
}
}

while(k == 1) {
NOISE();
realAnswer = mathValOne * mathValTwo + mathValThree;
lcd.setCursor(0, 0);
lcd.display();
lcd.print("GOOD MORNING~~");
lcd.setCursor(0, 1);
lcd.print(mathValOne);
lcd.print("*");
lcd.print(mathValTwo);
lcd.print("+");
lcd.print(mathValThree);
lcd.print(" = ");
lcd.setCursor(12, 1);
lcd.print(buttonPushCounter);

if (buttonState5 != lastButtonState) {

if (buttonState5 == HIGH)

{
delay(200);
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:

// compare the buttonState to its previous state

if (buttonState6 != lastButtonState) {

// if the state has changed, decrement the counter

if (buttonState6 == HIGH)

{
delay(200);
buttonPushCounter--;
lcd.setCursor(12,1);

lcd.print(buttonPushCounter);
}
}
if (buttonState7 == HIGH && buttonPushCounter != realAnswer)
{
mathValOne = random(2, 7);
mathValTwo = random(1, 8);
mathValThree = random(18, 25);
k = 1;
}
if (buttonState7 == HIGH && buttonPushCounter == realAnswer)
{
noTone(9);
(k = 0);
}

}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState6;
}
``````

Why do buttonState6 and buttonState5 share the same lastButtonState variable?