Go Down

Topic: Turn LED on with RTC (Read 10299 times) previous topic - next topic

cattledog

#45
Dec 09, 2019, 04:41 am Last Edit: Dec 09, 2019, 04:41 am by cattledog
I think I see the issue with the lights not turning off at 2300, if you have been running a pattern. The call for the pattern functions is not within the time conditional block. Add another time conditional line to just before the pattern calls.

Code: [Select]
if ((hour() > 16 && hour() < 23) || led_alwaysOn == 1) { //turn the wreath on each day beetween 5 and 10 pm
  {
    if (led_pattern1 == 1) {
      ledPattern1();
    }
    else if (led_pattern2 == 1) {
      ledPattern2();
    }
    else if (led_pattern3 == 1) {
      ledPattern3();
    }
  }

Keklja

Please post or attach complete code that you are currently running.

Can you still get serial output from your setup?

If so, I think you want to get debug values for turnOnOff and led_alwaysOn.
Also can you verify that the values for day and hour are correct.

Why are you suspecting the power.?

Serial Monitor is a little bit hard to read because the data are showing really fast but I saw that everything is ok.

I also add piece of code that will reset all variables at 2:00 am. Just in case if for example led_alwaysOn stays ON.

I suspect the power because on sutrday I wanted to turn on led manually but nothing happend. On MEGA I saw that LED is blinking and it receives signal from Remote Controler but LEDs were OFF. Then I pluged in my Laptop and there were ON. I didnt do anything.

I also wanted that LCD had extern power and not from MEGA (because if all LEDs are on the LCD losses brightness) but that didnt work. LCD hat only blinked.

After work Im gonna try what you wrote. It is a litle bit tricky to open the box now and connect MEGA to Laptop :D

Keklja

Here is the whole code:

Code: [Select]

#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>                // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define DS3231_I2C_ADDRESS 104

const int ledPins[] = {22, 23, 26, 27, 30, 31, 34, 35, 38, 39, 42, 43, 46, 47, 50, 51, 52, 53};  //defining on which pins are LEDs

//DEFINE REMOTE CONTROL PIN AND VARIABLE
const int recvPin = 7;
IRrecv irrecv(recvPin);
decode_results results;
unsigned long key_value = 0;

int turnOnOff = 0;
int led_alwaysOn = 0;
int led_pattern1 = 0;
int led_pattern2 = 0;
int led_pattern3 = 0;

int daysTillChristmas = 0;
int previousDaysTillChristmas = 0;
boolean dayPinsSet = false;

void setup()  {
 for (int x = 0; x < 18; x++) {        //set all LEDs als OUTPUT
   pinMode(ledPins[x], OUTPUT);
 }

 //REMOTE CONTROL
 irrecv.enableIRIn();
 irrecv.blink13(true);

 //RTC && LCD
 Wire.begin();
 Serial.begin(9600);
 while (!Serial);                    // wait until Arduino Serial Monitor opens
 setSyncProvider(RTC.get);         // the function to get the time from the RTC
 if (timeStatus() != timeSet)
   Serial.println("Unable to sync with the RTC");
 else
   Serial.println("RTC has set the system time");

 //LCD

 lcd.init();
  lcd.backlight();
  lcd.setCursor(1, 0);
  lcd.print("JOS");              
  //lcd.setCursor(5, 0);
  //lcd.print(daysTillChristmas);  
  lcd.setCursor(8, 0);
  lcd.print("DANA");            
  lcd.setCursor(13, 0);
  lcd.print("DO");              
  lcd.setCursor(0, 1);
  lcd.print("*****BOZICA*****");
 /*//Seting up a LCD
 lcd.setCursor(1, 0);
 lcd.print("JOS");               //in Croatian "STILL"
 lcd.setCursor(5, 0);
 lcd.setCursor(8, 0);
 lcd.print("DANA");              //in Croatian "DAYS"
 lcd.setCursor(13, 0);
 lcd.print("DO");                // in Croatian "TILL"
 lcd.setCursor(0, 1);
 lcd.print("*****BOZICA*****");  // in Croatian "CHRISTMAS"
 */
}

void loop()
{
 previousDaysTillChristmas = daysTillChristmas;
daysTillChristmas = 25 - day();
if (daysTillChristmas != previousDaysTillChristmas)//the day()has changed
{
  lcd.setCursor(5, 0);
  lcd.print("  "); //print 2 spaces to clear previous values
  lcd.setCursor(5, 0);
  lcd.print(daysTillChristmas);
}

 //RECIVE NUMBER ON SERIAL PORT FROM ARDUINO NANO
 if (Serial.available()) {
   int state = Serial.parseInt();

   switch (state) {
     case 1:
       if (turnOnOff == 0) {
         turnOnOff = 1;
         Serial.println("LED are OFF");
       } else if (turnOnOff == 1) {
         turnOnOff = 0;
         Serial.print("LED are ON");
         dayPinsSet = false;
       }
       break;
     case 2:
       Serial.println("PAttern1 is aktiv");
       led_pattern1 = 1;
       led_pattern2 = 0;
       led_pattern3 = 0;
       led_alwaysOn = 0;
       dayPinsSet = false;
       break;
     case 3:
       Serial.println("PAttern2 is aktiv");
       led_pattern1 = 0;
       led_pattern2 = 1;
       led_pattern3 = 0;
       led_alwaysOn = 0;
       dayPinsSet = false;
       break;
     case 4:
       Serial.println("PAttern3 is aktiv");
       led_pattern1 = 0;
       led_pattern2 = 0;
       led_pattern3 = 1;
       led_alwaysOn = 0;
       dayPinsSet = false;
       break;
     case 5:
       Serial.println("allwaysOn is aktiv");
       led_pattern1 = 0;
       led_pattern2 = 0;
       led_pattern3 = 0;
       led_alwaysOn = 1;
       dayPinsSet = false;
       break;
   }
 }
 //DEFINING WHAT REMOTE CONTROL BUTTONS DO AND HOW DO THEY CHANGE THE LED PATTERNS
 if (irrecv.decode(&results)) {
   switch (results.value) {

     case 0xFF6897:            //Master Button. It can turn off all LEDs
       if (turnOnOff == 0) {
         turnOnOff = 1;
         Serial.println("LEDs are OFF");
       } else if (turnOnOff == 1) {
         turnOnOff = 0;
         dayPinsSet = false;
         Serial.println("LEDs are ON");
         Serial.println(turnOnOff);
       }
       break;
     case 0xFF30CF:
       Serial.println("pattern 1");
       led_pattern1 = 1;
       led_pattern2 = 0;
       led_pattern3 = 0;
       led_alwaysOn = 0;
       dayPinsSet = false;
       break;
     case 0xFF18E7:
       Serial.println("pattern 2");
       led_pattern1 = 0;
       led_pattern2 = 1;
       led_pattern3 = 0;
       led_alwaysOn = 0;
       dayPinsSet = false;
       break;
     case 0xFF7a85:
       Serial.println("pattern 3");
       led_pattern1 = 0;
       led_pattern2 = 0;
       led_pattern3 = 1;
       led_alwaysOn = 0;
       dayPinsSet = false;
       break;
     case 0xFF10EF:
       Serial.println("LED are permanent ON");
       led_pattern1 = 0;
       led_pattern2 = 0;
       led_pattern3 = 0;
       led_alwaysOn = 1;
       dayPinsSet = false;
   }
   irrecv.resume();
 }
 //Only the changing part of LCD is in the loop
 /*lcd.setCursor(1, 0);
 lcd.print("JOS");               //in Croatian "STILL"
 lcd.setCursor(5, 0);
 
 lcd.print(daysTillChristmas);   //number of days
 
 lcd.setCursor(8, 0);
 lcd.print("DANA");              //in Croatian "DAYS"
 lcd.setCursor(13, 0);
 lcd.print("DO");                // in Croatian "TILL"
 lcd.setCursor(0, 1);
 lcd.print("*****BOZICA*****");  // in Croatian "CHRISTMAS"
*/
 if (timeStatus() == timeSet) {
   digitalClockDisplay();
 } else {
   Serial.println("The time has not been set.  Please run the Time");
   Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");
   Serial.println();
   delay(4000);
 }

 // delay(1000);
  
 if (turnOnOff == 0) {
   if ((hour() > 16 && hour() < 23) || led_alwaysOn == 1) { //turn the wreath on each day beetween 5 and 10 pm
     if (dayPinsSet == false)
     {
       if (day() > 1 && day() < 8) {                             //turning on LEDs for each day in first week
         for (int x = 0; x <= day() - 2; x++) {                   //because program starts on Monday 2.12. We need to ignore first sunday. But if i put day()-1 then on the 2.12. we have day() = 1; day()-1 = 1. So it will ignore first pin in array( x[0]) and it will turn one LED more.
           digitalWrite(ledPins[x], HIGH);
         }
       }
       else if (day() > 8 && day() < 15) {
         for (int x = 0; x <= day() - 2; x++) {                //here is day() - 2 because we had 2 sundays that we need to ignore
           digitalWrite(ledPins[x], HIGH);
         }
       }
       else if (day() > 15 && day() < 22) {
          for (int x = 0; x <= day() - 3; x++) {                // day() - 3 because we had 3 sundays
           digitalWrite(ledPins[x], HIGH);
         }
       }
       else if (day() == 8){
          for (int x = 0; x < 6; x++){
            digitalWrite(ledPins[x], HIGH);
          }
       }
        else if (day() == 15){
          for (int x = 0; x < 12; x++){
            digitalWrite(ledPins[x], HIGH);
          }
        }
        else if (day() == 22){
          for (int x = 0; x < 18; x++){
            digitalWrite(ledPins[x], HIGH);
        }
       }
       dayPinsSet = true;
     }
   }
  
   if (led_pattern1 == 1) {
     ledPattern1();
   }
   else if (led_pattern2 == 1) {
     ledPattern2();
   }
   else if (led_pattern3 == 1) {
     ledPattern3();

   }
 }
 else if (hour() == 02){
  turnOnOff = 0;
  led_pattern1 = 0;
  led_pattern2 = 0;
  led_pattern3 = 0;
  led_alwaysOn = 0;
  dayPinsSet = false;
 }
 else {
   for (int x = 0; x < 18; x++) {
     digitalWrite(ledPins[x], LOW);
   }
 }
}

/

Keklja

The rest of the code:

Code: [Select]

//PATTERNS

void ledPattern1() {
 const unsigned long blinkInterval = 100;
 static byte x = 0;//led index
 static boolean timing = false;
 static unsigned long startTime = millis();
 if (digitalRead(ledPins[x]) == HIGH) //is led on?
 {
   startTime = millis();
   timing = true;
   digitalWrite(ledPins[x], LOW); //turn it off
 }
 if (timing && millis() - startTime >= blinkInterval)
 {
   digitalWrite(ledPins[x], HIGH);
   timing = false;
 }

 if (timing == false)//increment when blink complete or pin not on
 {
   x++;
 }
 if (x == 18) x = 0; //reset at max
}

void ledPattern2() {
 const unsigned long blinkInterval = 100;
 static byte x = random(0, 18); //0-17
 static byte lastX = 255;
 static boolean timing = false;
 static unsigned long startTime = millis();

 if (digitalRead(ledPins[x]) == HIGH && x != lastX)
 {
   startTime = millis();
   timing = true;
   digitalWrite(ledPins[x], LOW); //turn it off
   lastX = x;
 }

 if (timing && millis() - startTime >= blinkInterval)
 {
   digitalWrite(ledPins[x], HIGH);
   timing = false;
 }

 if (timing == false)//increment when blink complete or pin not on
 {
   x = random(0, 18);
   //check pick and see if active pin
   //if not active pick again
   while (digitalRead(ledPins[x]) == LOW)
   {
     x = random(0, 18);
   }
 }
}
void ledPattern3()
{
 const unsigned long blinkInterval = 500;
 static byte x = 0;//led index
 static unsigned long startTime = millis();
 if (millis() - startTime >= blinkInterval)
 {
   startTime = millis();

   if (day() > 1 && day() < 8)
   {
     for ( x = 0; x <= day() - 2; x++)
     {
       digitalWrite(ledPins[x], !digitalRead(ledPins[x]));
     }
   }
   else if (day() > 8 && day() < 15)
   {
     for (x = 0; x <= day() - 3; x++)
     {
       digitalWrite(ledPins[x], !digitalRead(ledPins[x]));
     }
   }
   else if (day() > 15 && day() < 22)
   {
     for (x = 0; x <= day() - 4; x++)
     {
       digitalWrite(ledPins[x], !digitalRead(ledPins[x]));
     }
   }
 }
}




//Fuktion for RTC
void digitalClockDisplay() {
 // digital clock display of the time
 Serial.print(hour());
 printDigits(minute());
 printDigits(second());
 Serial.print(" ");
 Serial.print(day());
 Serial.print(" ");
 Serial.print(month());
 Serial.print(" ");
 Serial.print(year());
 Serial.println();
}
//Funktion for RTC
void printDigits(int digits) {
 // utility function for digital clock display: prints preceding colon and leading 0
 Serial.print(":");
 if (digits < 10)
   Serial.print('0');
 Serial.print(digits);
}

Keklja

So I  found the error.

Else for turning off LEDs was on the wrong place

Here is where is shoud be:

Code: [Select]

 if (turnOnOff == 0) {
   if ((hour() > 16 && hour() < 23) || (led_alwaysOn == 1)) { //turn the wreath on each day beetween 5 and 10 pm

 
     if (dayPinsSet == false)
     {
       if (day() > 1 && day() < 8) {                             //turning on LEDs for each day in first week
         for (int x = 0; x <= day() - 2; x++) {                   //because program starts on Monday 2.12. We need to ignore first sunday. But if i put day()-1 then on the 2.12. we have day() = 1; day()-1 = 1. So it will ignore first pin in array( x[0]) and it will turn one LED more.
           digitalWrite(ledPins[x], HIGH);
         }
       }
       else if (day() > 8 && day() < 15) {
         for (int x = 0; x <= day() - 3; x++) {                //here is day() - 3 because we had 2 sundays that we need to ignore
           digitalWrite(ledPins[x], HIGH);
         }
       }
       else if (day() > 15 && day() < 22) {
          for (int x = 0; x <= day() - 4; x++) {                // day() - 4 because we had 3 sundays
           digitalWrite(ledPins[x], HIGH);
         }
       }
       else if (day() == 8){
          for (int x = 0; x < 6; x++){
            digitalWrite(ledPins[x], HIGH);
          }
       }
        else if (day() == 15){
          for (int x = 0; x < 12; x++){
            digitalWrite(ledPins[x], HIGH);
          }
        }
        else if (day() == 22){
          for (int x = 0; x < 18; x++){
            digitalWrite(ledPins[x], HIGH);
        }
       }
       dayPinsSet = true;
     }
   }
   
   if (led_pattern1 == 1) {
     ledPattern1();
   }
   else if (led_pattern2 == 1) {
     ledPattern2();
   }
   else if (led_pattern3 == 1) {
     ledPattern3();

   }
   else {
      for (int x = 0; x < 18; x++) {
        digitalWrite(ledPins[x], LOW);
      }
   }
 }



Now evrthing is working

cattledog

Quote
Now evrthing is working
I'm not so sure. If you do not use a pattern, but just leave the lights for the day constantly on, I don't think they will go off. You are correct that the patterns need to be within the hour conditions but not with the day pin setting.

The followng code makes more sense to me. Both the day pins and the patterns are within the hour conditions, and the turn of is in the else from the hour conditions.

Code: [Select]

if (turnOnOff == 0) {
  if ((hour() > 16 && hour() < 23) || (led_alwaysOn == 1)) { //turn the wreath on each day beetween 5 and 10 pm
    if (dayPinsSet == false)
    {
      if (day() > 1 && day() < 8) {                             //turning on LEDs for each day in first week
        for (int x = 0; x <= day() - 2; x++) {                   //because program starts on Monday 2.12. We need to ignore first sunday. But if i put day()-1 then on the 2.12. we have day() = 1; day()-1 = 1. So it will ignore first pin in array( x[0]) and it will turn one LED more.
          digitalWrite(ledPins[x], HIGH);
        }
      }
      else if (day() > 8 && day() < 15) {
        for (int x = 0; x <= day() - 3; x++) {                //here is day() - 3 because we had 2 sundays that we need to ignore
          digitalWrite(ledPins[x], HIGH);
        }
      }
      else if (day() > 15 && day() < 22) {
        for (int x = 0; x <= day() - 4; x++) {                // day() - 4 because we had 3 sundays
          digitalWrite(ledPins[x], HIGH);
        }
      }
      else if (day() == 8) {
        for (int x = 0; x < 6; x++) {
          digitalWrite(ledPins[x], HIGH);
        }
      }
      else if (day() == 15) {
        for (int x = 0; x < 12; x++) {
          digitalWrite(ledPins[x], HIGH);
        }
      }
      else if (day() == 22) {
        for (int x = 0; x < 18; x++) {
          digitalWrite(ledPins[x], HIGH);
        }
      }
      dayPinsSet = true;
    }

    if (led_pattern1 == 1) {
      ledPattern1();
    }
    else if (led_pattern2 == 1) {
      ledPattern2();
    }
    else if (led_pattern3 == 1) {
      ledPattern3();
    }
  }
  else {
    for (int x = 0; x < 18; x++) {
      digitalWrite(ledPins[x], LOW);
    }
  }
}

Go Up