I am trying to make an alarm clock. When the button is bushed the
while(now.hour() == 16 && now.minute() == 19){
Buzz();
if(buttonState == 1){
break;
}
}
should exit the whole while statement and return to the loop, but it doesn't seem to do anything.
Here is the whole sketch in case it is anything else.
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
//LiquidCrystal lcd(2,3,4,5,6,7);
LiquidCrystal lcd(7,8,9,10,11,12);
RTC_DS3231 RTC;
#define buz 13
const int buzzer = 13;
const int buzzer2 = 6;
const int button = 2;
int buttonState = digitalRead(button);
void setup () {
Serial.begin(9600);
Wire.begin();
RTC.begin();
lcd.begin(16, 2);
pinMode(buzzer, OUTPUT);
pinMode(buzzer2, OUTPUT);
pinMode(button, INPUT);
}
void loop () {
Serial.println(digitalRead(button));
DateTime now = RTC.now();
lcd.setCursor(0, 0);
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.print(' ');
lcd.setCursor(0, 2);
if (now.hour()<10)
lcd.print('0');
lcd.print(now.hour(), DEC);
lcd.print(':');
if (now.minute()<10)
lcd.print('0');
lcd.print(now.minute(), DEC);
lcd.print(':');
if (now.second()<10)
lcd.print('0');
lcd.print(now.second(), DEC);
lcd.setCursor(12, 0);
while(now.hour() == 16 && now.minute() == 19){
Buzz();
if(buttonState == 1){
break;
}
}
}
void Buzz() {
digitalWrite(buzzer2,HIGH);
tone(buzzer, 3000); // Send 3KHz sound signal...
delay(500); // ...for 1 sec
digitalWrite(buzzer2,LOW);
noTone(buzzer); // Stop sound...
delay(500); // ...for 1sec
}
void printTime() {
int dayofweek;
switch(dayofweek){
case 1:
lcd.print("Mon");
break;
case 2:
lcd.print("Tue");
break;
case 3:
lcd.print("Wed");
break;
case 4:
lcd.print("Thu");
break;
case 5:
lcd.print("Fri");
break;
case 6:
lcd.print("Sat");
break;
case 0:
lcd.print("Sun");
break;
delay(1000);
}
}