// libraries #include #include #include "RTClib.h" // pin numbers const int potentioPin = 0; // analog A0 pin const int napPin = 4; // digital pin const int lightPin = 3; // digital pin const int servoPin = 9; // digital pin const int dayLight = 7; // digital pin const int nightLight = 8; // digital pin //sleep and timer settings RTC_DS1307 RTC; // RTC date DateTime time; // time object of the RT const int wake_h=12; const int wake_m=44; // time to get up const int sleep_h=12; const int sleep_m=54; // time to go to bed // variables for the nap int napState = 0; // the state of the button int napBusy = 0; // int to indicate if the system is in nap state int napSec = 1800; // length of the nap in seconds DateTime endNap; // time object holding the calculated endtime of the nap // variables for the light conditions int lightState = 0; // the state of the LED button int lightsOn = 1; // int to indicate if the lights are on or off. // variables for the servo element, determine these while constructing the mechanism Servo myservo; int awakePos = 72; int sleepPos = 154; //initialisation of the Arduino, runs only once void setup() { Serial.begin(57600); Wire.begin(); RTC.begin(); //if (! RTC.isrunning()) { // safety check for when the battery of the RTC is dead // Serial.println("RTC is NOT running!"); //} pinMode(napPin, INPUT); // initialize the input pins pinMode(lightPin, INPUT); // initialize the input pins pinMode(dayLight, OUTPUT); // initialize the output pins pinMode(nightLight, OUTPUT); // initialize the output pins myservo.attach(servoPin); // initialize the output pins } //loop that keeps running, over and over again void loop() { time=RTC.now(); //get current time from the RTC lightsOn=listenToLedPin(); //listen to the LED button napBusy=listenToNapPin(time,napBusy); //listen to the nap button setState(time,napBusy,lightsOn); //change the lights and servo corresponding to the button values and time delay(1000); //wait a second, to avoid bouncing. } //******************************************************************************// // help functions // //******************************************************************************// int listenToNapPin(DateTime time,int napBusyIn){ //listen to the nap button. If it is pressed, the current time is used together //with the potentiometer to calculate the endtime of the nap. The int napBusy //returns 1 if the nap is busy and 0 if the nap is not busy. //if the nap is busy, the button cannot be pressed and the time is compared to the //endtime of the nap and changed to 0 if the end of the nap is over. napState = digitalRead(napPin); if (napState == HIGH && napBusyIn==0){ napBusyIn=1; napSec = map(analogRead(potentioPin), 0, 1023, 1800, 7200); //scale the potentiometer value to something between 0.5h and 2h endNap = (time.unixtime() + napSec); } if (napBusyIn){ if (time.unixtime()>endNap.unixtime()){ napBusyIn=0; //nap ended }} return napBusyIn; } int listenToLedPin(){ //listen to the LED pin, if it is pressed, change the state of the int lightsOn //@return: the state of the LED lights, 1 if they are on, 0 if they are off. lightState = digitalRead(lightPin); if (lightState == HIGH) { lightState = LOW; Serial.println("light button pressed"); if (lightsOn){ return 0; } else { return 1; } } return lightsOn; } int setState(DateTime time,int napBusy,int lightsOn){ Serial.print(time.hour(), DEC); Serial.print(':'); Serial.print(time.minute(), DEC); Serial.print(':'); Serial.print(time.second(), DEC); Serial.println(); Serial.println(pastTime(time,sleep_h,sleep_m)); if (pastTime(time,wake_h,wake_m) && !pastTime(time,sleep_h,sleep_m)){ if (napBusy){ Serial.println("taking a nap"); Sleep(lightsOn); }else{ Serial.println("awake"); Awake(lightsOn); } } else { Serial.println("Sleeping during the night"); Sleep(lightsOn); } } int pastTime(DateTime time,int hours,int mins){ //because the sunrise and sunset times are not a full timestamp, only the hour and //minute values are compared. //@return int that is 1 if the first DateTime object is larger than the second. if (time.hour()>=(hours+1)){ return 1; }else{ if((time.hour()>=hours) && (time.minute() >= mins)){ return 1; }else{ return 0; } } } void Awake(int LightsOn){ //actuating the lights and servo to the AWAKE state if (LightsOn){ digitalWrite(dayLight, HIGH); //set the LED signal of the green leds to high }else{ digitalWrite(dayLight, LOW); } digitalWrite(nightLight, LOW); //set the LED signal of the red leds to low myservo.write(awakePos); } void Sleep(int LightsOn){ //actuating the lights and servo to the sleep state digitalWrite(dayLight, LOW); if (lightsOn){ digitalWrite(nightLight, HIGH); }else{ digitalWrite(nightLight, LOW); } myservo.write(sleepPos); }