Countdown sleep clock

Hello,

I 'm new to the Arduino , but my wife gave me the assignment to create a sleep timer for our son.

I had some ideas how to do it with plc's that I use at my work , but I came to the conclusion that all of the solutions are relative too large. So that's wy I want to switch to Arduino.

I've ordered the Arduino Uno with a DS3231.

The idea is to have 12 led's connected to the outputs, that all go on at 7pm and then every hour 1 led less.

Could you guys please get me started, since we need this timer fast due to lack of sleep!? ;) :P

I must say since I have read about all of the options the Arduino has, i'm going to order another one to play and learn! :wink:

So this is wat I got.

#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>

RTC_DS3231 RTC;

//LEDS

int led1 = 1;
int led2 = 2;
int led3 = 3;
int led4 = 4;
int led5 = 5;
int led6 = 6;
int led7 = 7;
int led8 = 8;
int led9 = 9;
int led10 = 10;
int led11 = 11;
int led12 = 12;

// the setup routine runs once when you press reset:
void setup() {
    Wire.begin();
    RTC.begin();
  
  }
 
  // initialize the digital pin as an output.
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);   
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);   
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT); 
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);   
  pinMode(led9, OUTPUT);
  pinMode(led10, OUTPUT);   
  pinMode(led11, OUTPUT);
  pinMode(led12, OUTPUT);

  AllOn();
    Alarm.alarmRepeat(07,00,0, sleep);  // 7:00pm every day
  led12off();
    Alarm.alarmRepeat(08,00,0, sleep12);  // 8:00pm every day
  led11off();
    Alarm.alarmRepeat(09,00,0, sleep11);  // 9:00pm every day
  led10off();
    Alarm.alarmRepeat(10,00,0, sleep10);  // 10:00pm every day
  led9off();
    Alarm.alarmRepeat(11,00,0, sleep9);  // 11:00pm every day
  led8off();
    Alarm.alarmRepeat(00,00,0, sleep8);  // 0:00am every day
  led7off();
    Alarm.alarmRepeat(01,00,0, sleep7);  // 1:00am every day
  led6off();
    Alarm.alarmRepeat(02,00,0, sleep6);  // 2:00am every day
  led5off();
    Alarm.alarmRepeat(03,00,0, sleep5);  // 3:00am every day
  led4off();
    Alarm.alarmRepeat(04,00,0, sleep4);  // 4:00am every day
  led3off();
    Alarm.alarmRepeat(05,00,0, sleep3);  // 5:00am every day
  led2off();
    Alarm.alarmRepeat(06,00,0, sleep2);  // 6:00am every day
  led1off();
    Alarm.alarmRepeat(07,00,0, sleep1);  // 7:00am every day
 
}

// the loop routine runs over and over again forever:
void loop() {

  DateTime now = RTC.now();

     
void sleep() {
   digitalWrite(led1, HIGH); // Set  LED On
   digitalWrite(led2, HIGH); // Set  LED On
   digitalWrite(led3, HIGH); // Set  LED On
   digitalWrite(led4, HIGH); // Set  LED On
   digitalWrite(led5, HIGH); // Set  LED On
   digitalWrite(led6, HIGH); // Set  LED On
   digitalWrite(led7, HIGH); // Set  LED On
   digitalWrite(led8, HIGH); // Set  LED On
   digitalWrite(led9, HIGH); // Set  LED On
   digitalWrite(led10, HIGH); // Set  LED On
   digitalWrite(led11, HIGH); // Set  LED On
   digitalWrite(led12, HIGH); // Set  LED On
}

void sleep12() {
  digitalWrite(led12, LOW); // Set  LED off
}

void sleep11() {
  digitalWrite(led11, LOW); // Set  LED off
}

void sleep10() {
  digitalWrite(led10, LOW); // Set  LED off
}

void sleep9() {
  digitalWrite(led9, LOW); // Set  LED off
}

void sleep8() {
  digitalWrite(led8, LOW); // Set  LED off
}

void sleep7() {
  digitalWrite(led7, LOW); // Set  LED off
}

void sleep6() {
  digitalWrite(led6, LOW); // Set  LED off
}

void sleep5() {
  digitalWrite(led5, LOW); // Set  LED off
}

void sleep4() {
  digitalWrite(led4, LOW); // Set  LED off
}

void sleep3() {
  digitalWrite(led3, LOW); // Set  LED off
}

void sleep2() {
  digitalWrite(led2, LOW); // Set  LED off
}

void sleep1() {
  digitalWrite(led1, LOW); // Set  LED off
}

If this is going into a room where people will be sleeping, be sure to use red/orange LEDs - certainly not blue/white ones. Studies have shown that exposure to blue light while sleeping or trying to sleep can significantly disrupt one's sleep cycle.

Yes, i was planning to use red LEDs.

DrAzzy: If this is going into a room where people will be sleeping, be sure to use red/orange LEDs - certainly not blue/white ones. Studies have shown that exposure to blue light while sleeping or trying to sleep can significantly disrupt one's sleep cycle.

Not in my experience. I've noticed that almost no blue light can penetrate closed eyelids, because the transmissive colour is red (due to the blood saturation of the tissue).

I have had an extremely bright blue LED in my room while sleeping, and it was blinking. It obnoxiously blazed up the entire room. Yet with my eyes closed, I could not even detect the blinking! So I think those studies are BS. Or else, their "blue" light was wideband.

I don't see how something you can't even see, could disrupt your sleep.

I have a Samsung monitor with a blue LED and that’s the most annoying thing to leave on when I’m in bed…

But back to the code. I never used the DS3232, only the DS1307. Can the RTClib read the DS3232? In the version I have I don’t see the DS3232 like you try to call in the code.

Bit the same for the whole alarm part, can’t find it…

But quick scan tough the DS3232 library I think something like this should do the trick

#include <DS3232RTC.h>    //http://github.com/JChristensen/DS3232RTC
#include <Time.h>         //http://www.arduino.cc/playground/Code/Time  
#include <Wire.h>         //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)

//LEDS
const byte Leds[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};


// the setup routine runs once when you press reset:
void setup() {
  Wire.begin();
 
  // initialize the digital pin as an output.
  for(byte i = 0; i < sizeof(Leds); i++){
    pinMode(Leds[i], OUTPUT);
  }
}


// the loop routine runs over and over again forever:
void loop() {
  tmElements_t now;
  RTC.read(now);
  
  //Check if it's between 19 and 7 (7pm and 7am)
  if(now.Hour >= 19 || now.Hour <= 7){
    //Calculate how many hours passed since 19 (7pm)
    byte hoursPassed = now.Hour;
    if(hoursPassed <= 7){
      hoursPassed += 24;
    }
    hoursPassed -= 19;
    
    for(byte i = 0; 1 < sizeof(Leds); i++){
      digitalWrite(Leds[i], i >= hoursPassed);
    }
  }
}

Your probably right about the libary, i'm a newby you know ;)

I'm gonna try your code asap. Thnx!