#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
////////////////Task States ///////////////////////////
#define LED 0
#define PushButton 1
#define Buzzer 2
#define RTC 3
#define End 4
///////////////////////////////////////////////////////
/////////////////////////////////////General Variables////////////////
char Task = 0;
unsigned long currentMillis = millis(); /// tick timer
unsigned int second =0;
unsigned int x =0;
////////////////////////////////////////////////////////////
///////////////////LED Task Global veriables////////
// Variables will change:
int ledState = LOW; // ledState used to set the LED
////////////////////////////////////////////////////
///////////////Task timer veriables/////////////////
unsigned long LED_previousMillis = 0; // will store last time LED was updated
const long LED_Timer = 500; // interval at which to blink (milliseconds)
unsigned long RTC_previousMillis = 0; // will store last time LED was updated
const long RTC_Timer = 1000; // interval at which to blink (milliseconds)
////////////////////////////////////////////////////////////
////////////Task counter ////////////////////////
unsigned long LED_counter =0;
///////////////////////////////////////
///////////////Function Prototypes/////////////////////
void Red_LED_Task ();
void Push_Button_Task ();
void Buzzer_Task ();
int RTC_Task ();
////////////////////////////////////////////////////////////
void setup() {
///////////////////////////RTC
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
///////////////////////////////////////////////////////
pinMode(7, INPUT);
pinMode(8, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
currentMillis = millis();
switch(Task){
case LED :
Red_LED_Task();
Task = PushButton ;
break;
case PushButton:
Push_Button_Task ();
Task = Buzzer ;
break;
case Buzzer:
Buzzer_Task ();
Task = RTC;
break;
case RTC:
second = RTC_Task ();
Serial.println(second , DEC);
if(second < 15){
Task = LED;
}
else{
Task = End;
}
break;
}
}
void Red_LED_Task(){
if (currentMillis - LED_previousMillis >= LED_Timer) {
// save the last time you blinked the LED
LED_previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(8, ledState);
}
}
void Push_Button_Task (){
if(digitalRead(7)){
digitalWrite(9, HIGH);
}
else{
digitalWrite(9, LOW);
}
}
void Buzzer_Task (){
tone(6, 262, 10); // Plays 262Hz tone for 0.250 seconds
}
int RTC_Task (){
DateTime now = rtc.now();
if (currentMillis - RTC_previousMillis >= RTC_Timer) {
// save the last time you blinked the LED
RTC_previousMillis = currentMillis;
// Serial.print("Current time: ");
// Serial.print(now.year(), DEC);
// Serial.print('/');
// Serial.print(now.month(), DEC);
// Serial.print('/');
// Serial.print(now.day(), DEC);
// Serial.print(" (");
// Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
// Serial.print(") ");
// Serial.print(now.hour(), DEC);
// Serial.print(':');
// Serial.print(now.minute(), DEC);
// Serial.print(':');
// Serial.print(now.second(), DEC);
// Serial.println();
// Serial.println();
}
x = now.second();
return x;
}