Go Down

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

Keklja

#30
Dec 02, 2019, 06:06 pm Last Edit: Dec 02, 2019, 06:07 pm by Keklja
So I'm back.
Here is 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;
//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();
}

void loop()
{
  daysTillChristmas = 25 - day(); //for LCD count down


  //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");
        }
   
      break;
      case 2:

        Serial.println("PAttern1 is aktiv");
        led_pattern1 = 1;
        led_pattern2 = 0;
        led_pattern3 = 0;
        led_alwaysOn = 0;
 
        break;
      case 3:
        Serial.println("PAttern2 is aktiv");
        led_pattern1 = 0;
        led_pattern2 = 1;
        led_pattern3 = 0;
        led_alwaysOn = 0;
        break;
      case 4:
        Serial.println("PAttern3 is aktiv");
        led_pattern1 = 0;
        led_pattern2 = 0;
        led_pattern3 = 1;
        led_alwaysOn = 0;
        break;
      case 5:
        Serial.println("allwaysOn is aktiv");
        led_pattern1 = 0;
        led_pattern2 = 0;
        led_pattern3 = 0;
        led_alwaysOn = 1;
        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;
          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;
        break;

      case 0xFF18E7:
        Serial.println("pattern 2");
        led_pattern1 = 0;
        led_pattern2 = 1;
        led_pattern3 = 0;
        led_alwaysOn = 0;
        break;
   
      case 0xFF7a85:
        Serial.println("pattern 3");
        led_pattern1 = 0;
        led_pattern2 = 0;
        led_pattern3 = 1;
        led_alwaysOn = 0;
        break;

      case 0xFF10EF:
        Serial.println("LED are permanent ON");
        led_pattern1 = 0;
        led_pattern2 = 0;
        led_pattern3 = 0;
        led_alwaysOn = 1;
    }
    irrecv.resume();
 
  }

    //Seting up a LCD
  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);
          }
        }
 //      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);
    }
  }
}

//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);
}


I need to wait 5 minutes then I will post what is working and what is not

Keklja

So, if I leave evrything like you wrote, pattern1 and 2 are not working correcty. That means the LED are
only blink once (sometimes not even that) and then it remains permanent on or off. Only pattern 3 is OK. Then sometimes if I click on pattern 2, the RTC freeze and nothing happens. If I choose pattern 3 then the wreath is blinkinh and I can turn on/off without a problem.
Also I can turn the wreath off but not back on.

If I disable the dayPinsSet() and everything that goes with it, I dont have a problem with RTC. All commends are going through the Serial Monitor. I can turn On/Off without a problem but then the all 3 patterns are not working.

One more thing what I noticed. If pattern 3 is aktiv and the LED is blinking and if I turn the wreath off and back on, the LED is continue to blink. I didnt imagine like that. I would like to have default state that means if I turn wreath back on all LEDs shoud be permanent ON.

Keklja

#32
Dec 02, 2019, 06:35 pm Last Edit: Dec 02, 2019, 06:36 pm by Keklja
And here is the code for Blynk:

Code: [Select]

/*
Change ssid, password and token
*/

char ssid[] = "Keky";
char password[] = "";
char token[] = "";




//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_SoftSer.h>
#include <BlynkSimpleShieldEsp8266_SoftSer.h>

// Set NanoESP Serial object
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(11, 12); // RX, TX

int ON_OFF = 2;
int PATTERN1 = 3;
int PATTERN2 = 4;
int PATTERN3 = 5;
int ALLWAYSON = 6;

ESP8266 wifi(EspSerial);

void setup()
{
  // Set console baud rate
  Serial.begin(9600);
  EspSerial.begin(19200);
  pinMode(ON_OFF, OUTPUT);
  pinMode(PATTERN1, OUTPUT);
  pinMode(PATTERN2, OUTPUT);
  pinMode(PATTERN3, OUTPUT);
  pinMode(ALLWAYSON, OUTPUT);
  Blynk.begin(token, wifi, ssid, password);
 
}

void loop()
{
  Blynk.run();
}

BLYNK_WRITE(V1) {
 int pinValue = param.asInt(); // Assigning incoming value from pin V1 to a variable

 if (pinValue == 1) {
   
    Serial.write("1");
   
  }
}
BLYNK_WRITE(V2) {
 int pinValue = param.asInt(); // Assigning incoming value from pin V5 to a variable

 if (pinValue == 1) {
   
    Serial.write("2");
}
}
BLYNK_WRITE(V3) {
 int pinValue = param.asInt(); // Assigning incoming value from pin V5 to a variable

 if (pinValue == 1) {
   
    Serial.write("3");
}
}
BLYNK_WRITE(V4) {
 int pinValue = param.asInt(); // Assigning incoming value from pin V5 to a variable

 if (pinValue == 1) {
   
    Serial.write("4");
}
}
BLYNK_WRITE(V5) {
  int pinValue = param.asInt(); // Assigning incoming value from pin V5 to a variable
  if (pinValue == 1) {
    Serial.write("5");
}
}

cattledog

#33
Dec 02, 2019, 07:05 pm Last Edit: Dec 02, 2019, 07:06 pm by cattledog
There is an issue with the section which turns on the pins by day, overriding the patterns. That is why the dayPinsSet was introduced.

You can not have delay() in the code and expect the blink patterns to work properly. If we solve the first issue of the global pin setting and the patterns it should not be needed.

Rather than restructuring things, I am trying to make the minimal changes to your code in order to move quickly. I think is you leave the dayPinsSet true and false in the code, but set it back to false with every serial or ir input, things will work. I am posting the code in two parts, because it is over the character limit. I have separated the last little rtc code from the main body.

Code: [Select]
#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>                
#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};  

//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;
boolean dayPinsSet = false;


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


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

 //RTC && LCD
 Wire.begin();
 Serial.begin(9600);
 while (!Serial);                    
 setSyncProvider(RTC.get);        
 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();
}

void loop()
{
 daysTillChristmas = 25 - day(); //for LCD count down


 //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();

 }

 //Seting up a LCD
 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);
         }
       }
       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);
   }
 }
}

//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]));
     }
   }
 }
}






cattledog

Last piece of rtc code


Code: [Select]
//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);
}

cattledog

Fits one post without commentary

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;
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();
}

void loop()
{
 daysTillChristmas = 25 - day(); //for LCD count down

 //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();
 }
 //Seting up a LCD
 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);
         }
       }
       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);
   }
 }
}

//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

Ok. I understand now. So I tested. I turned on 2 LEDs and I noticed if i select Pattern1 first (Pattern 1 is working) and then switch it to patter 3 (all leds should blink), only 1 LED is blinking (ledPins[0]) and other one is permanent on. Is it possible, after we select the pattern, first to turn all leds on (like a default state) and then start with the pattern?

Random pattern is also seems to work

cattledog

#37
Dec 02, 2019, 08:17 pm Last Edit: Dec 02, 2019, 08:17 pm by cattledog
Quote
then switch it to patter 3 (all leds should blink), only 1 LED is blinking (ledPins[0]) and other one is permanent on.
I think the problem with pattern3 may be in the definition of what lights are on which was picked up from the basic setup of the lights. If its Dec2, then you can see how only ledPins[0] will be toggled. You may want to change to day() - 1 for this section.

Code: [Select]
if (day() > 1 && day() < 8)
    {
      for ( x = 0; x <= day() - 2; x++)
      {
        digitalWrite(ledPins[x], !digitalRead(ledPins[x]));
      }
    }


There are some important refinements to make if things are close to working properly. With delay(1000) removed, we need to put the serial print on a millis() timer as previously suggested, or else leave it out of the code. Is it necessary?

Also, the led display is poorly managed. The unchanging information should be written in setup only. Only the changing pieces should be in loop(), and they should be either on a timer or only written when they change.

Keklja

#38
Dec 02, 2019, 08:43 pm Last Edit: Dec 02, 2019, 08:48 pm by Keklja
I think the problem with pattern3 may be in the definition of what lights are on which was picked up from the basic setup of the lights. If its Dec2, then you can see how only ledPins[0] will be toggled. You may want to change to day() - 1 for this section.

Code: [Select]
if (day() > 1 && day() < 8)
    {
      for ( x = 0; x <= day() - 2; x++)
      {
        digitalWrite(ledPins[x], !digitalRead(ledPins[x]));
      }
    }


You are right. I forget to change that in pattern3 function.

Quote
There are some important refinements to make if things are close to working properly. With delay(1000) removed, we need to put the serial print on a millis() timer as previously suggested, or else leave it out of the code. Is it necessary?

Not any more.

Quote
Also, the led display is poorly managed. The unchanging information should be written in setup only. Only the changing pieces should be in loop(), and they should be either on a timer or only written when they change.
Ok. I will look into that. Because the data on lcd are changing only once in a day so they can only be written when they change.

I cannot thank you enough. But still I have a favor to ask. Can you comment the changes which you did in the code? Then I can have more understanding what excaly is it happening. Especially in the functions. Like I sad, it is a little bit too much for me right now but with the commects I bealive it will be easier to understand it...

cattledog

#39
Dec 02, 2019, 09:01 pm Last Edit: Dec 02, 2019, 09:13 pm by cattledog
Quote
Can you comment the changes which you did in the code? Then I can have more understanding what exactly is it happening. Especially in the functions.
Please ask a question about any thing you don't understand.

Code: [Select]

#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>
#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;
boolean dayPinsSet = false;//control variable to manage when static pin pattern is set

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);                   
  setSyncProvider(RTC.get);     
  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();
}

void loop()
{
  daysTillChristmas = 25 - day(); //for LCD count down

  //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;//enables reset of static pin pattern after any pattern changes from inputs.
        }
        break;
      case 2:
        Serial.println("PAttern1 is aktiv");
        led_pattern1 = 1;
        led_pattern2 = 0;
        led_pattern3 = 0;
        led_alwaysOn = 0;
        dayPinsSet = false;//enables reset of static pin pattern after any pattern changes from inputs.
        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();
  }
  //Seting up a LCD
  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*****"); 

  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); //can not use delay and be responsive to inputs and blink patterns

  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) //set static pins only once or else this section of code interferes with patterns
      {
        if (day() > 1 && day() < 8) { 
          for (int x = 0; x <= day() - 2; x++) {
            digitalWrite(ledPins[x], HIGH);
          }
        }
        else if (day() > 8 && day() < 15) {
          for (int x = 0; x <= day() - 2; x++) {
            digitalWrite(ledPins[x], HIGH);
          }
        }
        else if (day() > 15 && day() < 22) {
          for (int x = 0; x <= day() - 3; x++) {
            digitalWrite(ledPins[x], HIGH);
          }
        }
        dayPinsSet = true;//set only once
      }
    }
   
    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);
    }
  }
}

//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]));//toggle any led which is on
      }
    }
    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]));
      }
    }
  }
}

//removed rtc and print out code to get it to fit in one post

}

Keklja

So I have a problem with LCD. It is showing 22 all the time (on both) rows. I belive it has something to do with your changes but I cannot find what do I need to change.

It is showing 22 because there are 22 days till Christmas.
After I turn on the wreath I can see other information on LCD but only for like ms and then it is showing only 22 (It shows 32 time number 2)

cattledog

I think this is all the code for the lcd display. If there is more, please let me know.

Code: [Select]
//Setting up a LCD
  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*****");


It is best to put the unchanging parts in setup(). They will be persistent in the display's internal memory. Then, only write the changing parts in loop().

Place this is setup() after the existing lcd stuff in setup()

 
Code: [Select]
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*****");


Now to deal with the changing part, define a new variable previousDaysTillChristmas where you declare daysTillChristmas.

Code: [Select]
int daysTillChristmas = 0;
int previousDaysTillChristmas = 0;


Then in loop() place at the very top where daysTillChristmas is located

Code: [Select]
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);


Quote
So I have a problem with LCD. It is showing 22 all the time (on both) rows. I believe it has something to do with your changes but I cannot find what do I need to change.

It is showing 22 because there are 22 days till Christmas.
After I turn on the wreath I can see other information on LCD but only for like ms and then it is showing only 22 (It shows 32 time number 2)
This problem description does not make sense to me given the existing code.  "*****BOZICA*****" should be on the second row.

If the suggested code does not fix the problem, can you please attach a photo of how the display looks.









Go Up