Go Down

Topic: Two light code... dont work. (Read 474 times) previous topic - next topic

9acca9

Jul 29, 2018, 07:42 pm Last Edit: Jul 29, 2018, 10:42 pm by 9acca9
Hi, im trying light on and off two lights at different times... (first one, and when still that light is on, start the second..... then, in the night off the first and after 30 minutes off the second) but dont work...
I think because "if "and "else if" exclude each other........ but dont know how to do this.

this is my code.

Code: [Select]

#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);
  pinMode(3, 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: >3pm  y <24pm
    if(horas>13.5&&horas<14.5)
    {
      digitalWrite(2, HIGH);
      Serial.print("Salida digital 2 = ON");
    }

    //condicion: >3.5pm y <24.5am
     else if (horas>14&&horas<15)
    {
      digitalWrite(3, HIGH);
      Serial.print("Salida digital 3 = ON");
    }
   
    else
    {
      digitalWrite(2, LOW);
      delay(1000);
      digitalWrite(3, LOW);
      Serial.print("Salida digital 2 = 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);
}]

sterretje

Use one if/else for the one light and one if/else for the other light.

Code: [Select]
if(some condition == true)
{
  light 1 on
}
else
{
  light 1 off
}

if(some other condition == true)
{
  light 2 on
}
else
{
  light 2 off
}


PS, you nearly had your code tags right ;)
[code]your code here[/code]
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

9acca9

Hi. Thanks
Finally i change the code for this:

Code: [Select]

#include <Time.h>
#include <TimeAlarms.h>
#include <DS1307RTC.h>

void setup () {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
 
  setSyncProvider(RTC.get);
  if (timeStatus() != timeSet)
    Serial.println("Fallo de RTC");
  else
    Serial.println("Sincronizado con RTC");

    // Crear las alarmas y configurar las funciones correspondientes a cada una
  Alarm.alarmRepeat(15, 0, 0, EventoEnciendeLuz);  // Evento a las 18:00 diario (enciende luz)
  Alarm.alarmRepeat(0, 30, 0, EventoApagaLuz);  // Evento a las 7:05 diario (apaga luz)
  Alarm.alarmRepeat(15, 30, 0, EventoEnciendeLuz2);
  Alarm.alarmRepeat(0, 0, 0, EventoApagaLuz2);
}


void loop() {
  // Mostrar el reloj en el monitor serial
  digitalClockDisplay();
 
  // Esperar 1 segundo y procesar las Alarmas mientras tanto...
  // El metodo Alarm.delay() procesa en el fondo las alarmas y llamara a las funciones indicadas
  Alarm.delay(1000);
}

/**
   Funcion callback que activa el relevador en el pin 3 (enciende la luz)
*/
void EventoEnciendeLuz()
{
  Serial.println("Encendiendo Luz!!!");
  digitalWrite(2, HIGH);
}

/**
   Funcion callback que desactiva el relevador en el pin 3 (apaga la luz)
*/
void EventoApagaLuz()
{
  Serial.println("Apagando Luz!!!");
  digitalWrite(2, LOW);
}

void EventoEnciendeLuz2()
{
  Serial.println("Encendiendo Luz!!!");
  digitalWrite(3, HIGH);
}

void EventoApagaLuz2()
{
  Serial.println("Apagando Luz!!!");
  digitalWrite(3, LOW);
}


/**
   Funciones para la impresion del reloj al monitor serial de arduino
*/
void digitalClockDisplay() {
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}
 
void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


This work fine except that......

What happens is that if the arduino goes off, and I turn it on after the power-up time, the lights do not turn on. As if he did not verify the function. That is, if it has to turn on at 3:00 PM and the Arduino is off at that moment and then turn it on at 5:00 PM, the lights do not turn on.
On the other hand, if the arduino is on when the lights have to turn on, then it does work.

Thanks to all!

Henry_Best

Hi. Thanks
Finally i change the code for this:

Code: [Select]

#include <Time.h>
#include <TimeAlarms.h>
#include <DS1307RTC.h>

void setup () {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
 
  setSyncProvider(RTC.get);
  if (timeStatus() != timeSet)
    Serial.println("Fallo de RTC");
  else
    Serial.println("Sincronizado con RTC");

    // Crear las alarmas y configurar las funciones correspondientes a cada una
  Alarm.alarmRepeat(15, 0, 0, EventoEnciendeLuz);  // Evento a las 18:00 diario (enciende luz)
  Alarm.alarmRepeat(0, 30, 0, EventoApagaLuz);  // Evento a las 7:05 diario (apaga luz)
  Alarm.alarmRepeat(15, 30, 0, EventoEnciendeLuz2);
  Alarm.alarmRepeat(0, 0, 0, EventoApagaLuz2);
}


void loop() {
  // Mostrar el reloj en el monitor serial
  digitalClockDisplay();
 
  // Esperar 1 segundo y procesar las Alarmas mientras tanto...
  // El metodo Alarm.delay() procesa en el fondo las alarmas y llamara a las funciones indicadas
  Alarm.delay(1000);
}

/**
   Funcion callback que activa el relevador en el pin 3 (enciende la luz)
*/
void EventoEnciendeLuz()
{
  Serial.println("Encendiendo Luz!!!");
  digitalWrite(2, HIGH);
}

/**
   Funcion callback que desactiva el relevador en el pin 3 (apaga la luz)
*/
void EventoApagaLuz()
{
  Serial.println("Apagando Luz!!!");
  digitalWrite(2, LOW);
}

void EventoEnciendeLuz2()
{
  Serial.println("Encendiendo Luz!!!");
  digitalWrite(3, HIGH);
}

void EventoApagaLuz2()
{
  Serial.println("Apagando Luz!!!");
  digitalWrite(3, LOW);
}


/**
   Funciones para la impresion del reloj al monitor serial de arduino
*/
void digitalClockDisplay() {
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}
 
void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


This work fine except that......

What happens is that if the arduino goes off, and I turn it on after the power-up time, the lights do not turn on. As if he did not verify the function. That is, if it has to turn on at 3:00 PM and the Arduino is off at that moment and then turn it on at 5:00 PM, the lights do not turn on.
On the other hand, if the arduino is on when the lights have to turn on, then it does work.

Thanks to all!
In setup, you need to get the current time and set the lights accordingly.

wvmarle

What happens is that if the arduino goes off, and I turn it on after the power-up time, the lights do not turn on. As if he did not verify the function. That is, if it has to turn on at 3:00 PM and the Arduino is off at that moment and then turn it on at 5:00 PM, the lights do not turn on.
On the other hand, if the arduino is on when the lights have to turn on, then it does work.
I would only use alarms for this if you need to wake up the Arduino from sleep (usually when using battery power). Otherwise, just have loop() check the time and set lights accordingly, not just when an alarm is received.

Doing this once in setup() also works, of course.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up