PWM, delays and time settings?

The "Eleven" is our Uno-equivalent Arduino-compatible board, but with a number of improvements including prototyping area, a mini-USB connector, LEDs mounted near the edge, and the D13 LED isolated using a FET. [Product page]
Post Reply
brendonh
Posts: 4
Joined: Wed Dec 23, 2015 4:33 am

PWM, delays and time settings?

Post by brendonh » Wed Dec 23, 2015 5:27 am

Hi All

New to Arduino and freetronics, hoping someone can take a look at my code, any pointers would be much appreciated.
I have used code from various sources (you may notice some from the experimenter tutorials) and using an ELEVEN, MOSFETS and RTC to have two automated light channels for my white and blue aquarium lights. It does work when the delays are set to 1000. When I increase the delays to 2000 the program seems to lock up on the 2nd and 3rd light transitions.

For testing I have shortened the start/stop times to run a little longer than the delays. The idea is to start the blue light in the morning for a couple of hours. As the blue light ramps down the white light ramps up and in the afternoon the white light ramps down and the blue up, then the blue ramps down at night. To get close to 30 min ramps the delays need to be a bit over 2000 and I cant get it happening. I think it pushes the poor eleven to its max.

If I can get this working I plan to work in "lightning storms" and restore state if power is lost. I know I can purchase a controller that will do all this and probably more but where's the fun in that?

Code: Select all

#include "Wire.h" 
#include "RTClib.h"

RTC_DS1307 RTC;
int ledPinWHITE = 11;    // WHITE LED's PWM channel - pin 11
int ledPinBLUE = 5;      // BLUE LED's PWM channel - pin 5
int brightness1 = 0;
int brightness2 = 0;
int brightness3 = 0;
int delayTime1 = 1500;
int delayTime2 = 1500;
int delayTime3 = 1500;

//Time for Blue Light AM       Set the starting and end times
byte startHour1 = 15;       // No leading zero before minute - 7, not 07
byte startMinute1 = 25;      // Use 24h format for the hour, without leading zero - 7, not 07
byte endHour1 = 15;      	//Times shortened for testing
byte endMinute1 = 45;

//Time for White Light
byte startHour2 = 15;    
byte startMinute2 = 35;  
byte endHour2 = 16;      
byte endMinute2 = 0;


//Time for Blue Light PM
byte startHour3 = 15;    
byte startMinute3 = 55;  
byte endHour3 = 16;      
byte endMinute3 = 15;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  
    if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));   
}
  pinMode(ledPinWHITE, OUTPUT);   // Set the WHITE LED output pin
  pinMode(ledPinBLUE, OUTPUT);   // Set the BLUE LED output pin
 
  digitalWrite(ledPinWHITE, LOW );   // Set the WHITE LED OFF
  digitalWrite(ledPinBLUE, LOW );    // Set the BLUE LED OFF
}
// Ramp UP and Ramp DOWN light channels when turning ON and OFF
void WhiteLightON() {
  while(brightness1 < 255){
      analogWrite(ledPinWHITE, brightness1);
      delay(delayTime1);
      brightness1 = brightness1 + 1;
  }
}
void WhiteLightOFF() {
  while(brightness1 > 0){
      analogWrite(ledPinWHITE, brightness1);
      delay(delayTime1);
      brightness1 = brightness1 - 1;     
  }
 digitalWrite(ledPinWHITE, LOW);   // Set the WHITE LIGHT OFF
}
void BlueLightONAM() {
  while(brightness2 < 255){
      analogWrite(ledPinBLUE, brightness2);
      delay(delayTime2);
      brightness2 = brightness2 + 1;
  } 
}
void BlueLightOFFAM() {
  while(brightness2 > 0){
      analogWrite(ledPinBLUE, brightness2);
      delay(delayTime2);
      brightness2 = brightness2 - 1;     
  } 
 digitalWrite(ledPinBLUE, LOW);   // Set the BLUE LIGHT OFF
}
void BlueLightONPM() {
  while(brightness3 < 255){
      analogWrite(ledPinBLUE, brightness3);
      delay(delayTime3);
      brightness3 = brightness3 + 1;
  } 
}
void BlueLightOFFPM() {
  while(brightness3 > 0){
      analogWrite(ledPinBLUE, brightness3);
      delay(delayTime3);
      brightness3 = brightness3 - 1;     
  } 
 digitalWrite(ledPinBLUE, LOW);   // Set the BLUE LIGHT OFF
}                             
void loop() {
 DateTime now = RTC.now();  // Read the current datestamp from RTC to turn on blue light morning
   if (now.hour() == startHour1 && now.minute() == startMinute1) {
    BlueLightONAM();
 }
{
 DateTime now = RTC.now();  // Read the current datestamp from RTC to turn on white light
   if (now.hour() == startHour2 && now.minute() == startMinute2) {
    WhiteLightON();
  }
 }
{
 DateTime now = RTC.now();  // Read the current datestamp from RTC to turn on blue light evening
   if (now.hour() == startHour3 && now.minute() == startMinute3) {
    BlueLightONPM();
  } 
 }
 {
 DateTime now = RTC.now();  // Read the current datestamp from RTC to turn off blue light morning
   if (now.hour() == endHour1 && now.minute() == endMinute1) {
    BlueLightOFFAM();
  }
 }
{
 DateTime now = RTC.now();  // Read the current datestamp from RTC to turn off white light
   if (now.hour() == endHour2 && now.minute() == endMinute2) {
    WhiteLightOFF();
  }
 }
{
 DateTime now = RTC.now();  // Read the current datestamp from RTCto turn off blue light evening
   if (now.hour() == endHour3 && now.minute() == endMinute3) {
    BlueLightOFFPM();
  } 
 } 
}
Merry Xmas
Regards
Brendon

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: PWM, delays and time settings?

Post by angusgr » Wed Dec 30, 2015 6:05 am

Hi Brendon,

The code you posted looks OK to me. Are you saying that what you posted works fine, but if you change delayTime1,2,3 to 2000 then it suddenly stops working at all?

Angus

brendonh
Posts: 4
Joined: Wed Dec 23, 2015 4:33 am

Re: PWM, delays and time settings?

Post by brendonh » Sat Jan 16, 2016 8:50 pm

Hi Angus

Thanks for replying. I have been on holidays and haven't had the opportunity to check my post for a couple of weeks,.

Exactly right it works fine with delays of 1000, when the delays are changed to (1200, 1500 or 2000) it seems to lock up on the afternoon transitions. With the higher values (above 1000) it does start as expected but fails to change when the blue light comes on for the afternoon, both lights remain on till the board is reset.

I wanted to have one light ramping down as the other ramps up and vice versa, having the timers running consecutively till the final levels are achieved. I thought having 2 timers running was causing the problem but changing the timers so the ramping of one light is finished before the next one begins didn't solve the issue.

Regards
Brendon Hartill-Law

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: PWM, delays and time settings?

Post by angusgr » Tue Jan 19, 2016 10:53 pm

Hi Brendon,

A possible problem might be that if the code spends too long inside one of the loops (ramping the brightness up or down), checks like this:

Code: Select all

now.minute() == endMinute
... might fail because that minute comes and goes, while it's still finishing the previous transition.

You loop through 255 steps, so with delay(1000) that's approximately 255 seconds (4.25 minutes). But with delay(2000) that's 8.5 minutes. There's only 5 minutes gap between endHour2 and startHour3, which means that the previous transition starts at 15:55 but won't finish until 16:03 - so the check for startHour3's timestamp of 16:00 has already been missed.

The easiest way to fix this in the code you have is to change the gaps between each transition so they take at least as many minutes as the transitions themselves, maybe plus a small margin for error.

Regards,


Angus

brendonh
Posts: 4
Joined: Wed Dec 23, 2015 4:33 am

Re: PWM, delays and time settings?

Post by brendonh » Wed Jan 20, 2016 7:19 am

Hi Angus

Thanks again for your reply. I came to the same conclusion that I have to wait for each step in the loop to complete with several minutes extra and changed the code accordingly. I still can't seem to get it to work properly with any other delay than 1000 millis. At 2000 millis I'm giving it 15 minutes before starting the next step in the loop. Still fails when blue light comes on the second time, both lights remain on (full I think) and that's it until the board is reset. Got some work to do to get even close to the 8500 millis I want to use. At 1000 millis works perfectly!!

Might try plugging the pc into it and add serial print lines of values to try and figure out where it's failing. I've been thinking about doing it for a while but wonder if this will only use up more valuable memory.

Regards
Brendon

brendonh
Posts: 4
Joined: Wed Dec 23, 2015 4:33 am

Re: PWM, delays and time settings?

Post by brendonh » Fri Jan 22, 2016 6:05 am

Hi Angus

Got it working. I checked the code and found I hadn't allowed enough time for each step to finish. Very annoyed at myself as I should have picked this up.

Thanks again for your help.

Regards
Brendon :oops:

Post Reply