Turn on and off a light at a specific hour

Hello everyone.

I know almost nothing about electronics, although I have done some projects with arduino and also with raspberry pi.

I want to turn on and off a light at certain hour (for example, at 14:30pm the light is on until 23:30pm, and the rest of the time off.

I dont want to buy a RTC module..... so, some people tell me i can use millis function.

If for some reason it were to cut off the power supply, i power up the arduino at a specific hour, like 00:00am.
I think that at some time (for example at 00:00, the arduino is reset... to count again)

somebody can give me a hand with the code??

Sorry for my ignorance, and thanks!

(i dont speak english)

Really buying an RTC would save you some hassle... can I ask if there is a specific reason why you want to avoid that?
at £ 2 on banggood, price seems hardly a constrain

That said, you can surely do that with millis(), but as you said "if the power cut off" when it will come up again the arduino will reset and you would not know that, unless you add in the code some alarm that will tell you that the reset has happened (for instance turning up an LED if millis() is less than a certain value. even so, the time at which you turn on and off the light will drift after a few days, and at some point millis() will overflow and your timings will be off. There are ways to overcome the millis() overflow, but that will be much more difficult than adding a RTC

Millis() will drift over time. Why don't you want to use a real RTC? There are alternatives, but they'll probably end up costing more.

Or use an ESP8266 based board and get your time off the Internet. More accurate even than an RTC.

get your time off the Internet. More accurate even than an RTC.

Yeah, we KNOW how accurate EVERYTHING in the internet is. 8)

NTP servers tend to be very accurate. You're not going to improve on that with a simple RTC.

Hi and thanks to all.
Finally i buy a RTC DS1307.
And have the code for what i want.
The problem for me is...
Im doing this for a aquarium.
In this arduino i have also a temperature sensor. And the idea is control the light...
So i have the two codes that works fine. But...... i dont know how mix them in just one code to upload.
Can give me a hand????
Thanks
(i dont speak english)

The code for time.

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

RTC_DS1307 rtc;

int segundo,minuto,hora,dia,mes,diaDeLaSemana;

DateTime HoraFecha;

void setup () {
  Serial.begin(9600);
  rtc.begin(); //Inicializamos el RTC
  pinMode(2, OUTPUT);
}

void loop () {
    HoraFecha = rtc.now(); //obtenemos la hora y fecha actual
    
    segundo=HoraFecha.second();
    minuto=HoraFecha.minute();
    hora=HoraFecha.hour();
    diaDeLaSemana=HoraFecha.dayOfTheWeek(); //0:Dominogo, 1:Lunes, 2:Martes ...
    
    float horas=hora+minuto/60.0;

    //condicion: >Domingo y  <sabado    y   >6.30pm  y <11.30pm
    if(diaDeLaSemana>0&&diaDeLaSemana<7 && horas>18.5&&horas<21.5)
    {
      digitalWrite(2, HIGH);
      Serial.print("Salida digital 13 = ON");
    }
    else
    {
      digitalWrite(2, LOW);
      Serial.print("Salida digital 13 = OFF");
    }

    //Enviamos por el puerto serie la hora y fecha.
    Serial.print("   hora = ");
    Serial.print(hora);
    Serial.print(":");
    Serial.print(minuto);
    Serial.print(":");
    Serial.print(segundo);
    Serial.print("  dia de La semana = ");
    Serial.print(HoraFecha.dayOfTheWeek());
    Serial.println();
    delay(1000);
}

This is the code for the temperature in the aquarium

/******************************/
/*  Medir Temperatura ds18b20 y activar cooler por relé */
/******************************/

/*** Librerías ***/

#include <OneWire.h>                 //Se importan las librerías
#include <DallasTemperature.h>

//** Definiciones **//
 
#define Pin 10                        //Se declara el pin donde se conectará la DATA
 
OneWire ourWire(Pin);            //Se establece el pin declarado como bus para la comunicación OneWire
 
DallasTemperature sensors(&ourWire); //Se llama a la librería DallasTemperature


//** Programa **//

void setup() {
delay(1000);
Serial.begin(9600);
sensors.begin();   //Se inician los sensores
pinMode(7,OUTPUT);
}
 
void loop() {
sensors.requestTemperatures();       //Prepara el sensor para la lectura
float temperatura = sensors.getTempCByIndex(0);
Serial.print(temperatura); //Se lee e imprime la temperatura en grados Centigrados
Serial.println(" Grados Centigrados");


delay(1000);                         //Se provoca una parada de 1 segundo antes de la próxima lectura

if (temperatura>29)
{
    digitalWrite(7,HIGH); // se enciende el ventilador
}
if (temperatura<26)
{
    digitalWrite(7,LOW); //se apaga el ventilador
}
}

i dont know how mix them in just one code to upload.

Since you don't have any requirements for the resulting code, just take the even lines from one file and the odd lines from the other file. Sort them into the proper order in the third file.

Now, if you HAVE requirements, but just forgot to share them, there's no time like the present to rectify that oversight.

Hi and thanks.
Well i want to add a second light to manage... and i dont know how... i suposse is related to "void loop" and in digitalWrite put another connection but i dont know how write this, where put the code.
And for mix the codes... i dont get what you say, can put me a little example from the code??
Thanks for your time.

I can do it over webclient, if you have web on internet, you will have GUI where you can set that time from.. to... ON, else off :slight_smile:
E-mail me if interested: martinius96@gmail.com
Web: https://arduino.php5.sk