Am I trying to use a variable or pass a value by reference or by value?

So if I use the update() function what is leds? Is it the pin I am controlling? Or a value? What am I telling the compiler with leds.update()? write the value of leds in EEPROM?

Downwardflight:
So if I use the update() function what is leds? Is it the pin I am controlling? Or a value? What am I telling the compiler with leds.update()? write the value of leds in EEPROM?

telling the compiler? I don’t know what that means.

A while ago I wrote such a led fading class. It allows you to fade slowly over an interval without blocking. Here it is added to what I did above:

.ino

#include "DailyTimer.h"
#include "Fade.h"

#define CLOUD_INTERVAL 60 * 5 // max time between clouds
#define CLOUD_DURATION 60 * 2 // max cloud duration

void sunrise(void);
void sunset(void);
void cloud(void);
void nocloud(void);

const byte ledPin = 13;
const byte pwmPin = 6;

DailyTimer sunRiseSunSet(true, 6, 0, 18, 0, EVERY_DAY, FIXED, sunrise, sunset);  //call sunrise() at the start of the timer and sunset() at the end.
DailyTimer randomCloud(true, 0, 0, 0, 0, EVERY_DAY, FIXED, cloud, nocloud);  //call cloud() at the start of the timer and nocloud() at the end.

Fade led(pwmPin, 100); // 100 is the interval between changes to PWM output
/*
    So, a 45 min sunrise would take an interval of 45UL * 60 * 1000 / 255 or about 10500
    I used 100 so you can actually see the fade for this demo
*/

void setup() 
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  led.begin();
  setTime(1510563600);
  randomSeed(analogRead(A0));
  sunRiseSunSet.begin();  // syncs the timer, use it here and after calls to setStartTime
  Serial.print("Active days: ");
  Serial.println(sunRiseSunSet.getDays(), BIN);
}

void loop() 
{
  DailyTimer::update();
  led.update();
  static unsigned long lastTime = 0;
  if(millis() - lastTime >= 1000)
  {
    char timeBuffer[32] = "";
    sprintf(timeBuffer, "Time:%2d:%02d:%02d\tDate:%02d/%02d/%4d", hour(), minute(), second(), month(), day(), year());
    Serial.println(timeBuffer);
    lastTime = millis();
  }
}

void sunrise(void)
{
  digitalWrite(13, HIGH);
  setNextCloud();
  led.setTarget(255);
}

void sunset(void)
{
  digitalWrite(13, LOW);
  led.setTarget(0);
}

void cloud(void)
{
  Serial.println(F("Cloud"));
  digitalWrite(13, LOW);
}

void nocloud(void)
{
  Serial.println(F("No Cloud"));
  digitalWrite(13, HIGH);
  setNextCloud();
}

void setNextCloud(void)
{
  if(sunRiseSunSet.isActive())
  {
    Serial.println(F("another cloud coming"));
    time_t now = DailyTimer::tmConvert_t(year(), month(), day(), hour(), minute(), second());
    time_t nextCloudStart = now + random(60, CLOUD_INTERVAL);
    time_t nextCloudEnd = nextCloudStart + random(60, CLOUD_DURATION);
    char buffer[32];
    sprintf(buffer, "Start %02d:%02d End %02d:%02d", hour(nextCloudStart), minute(nextCloudStart), hour(nextCloudEnd) , minute(nextCloudEnd));
    Serial.println(buffer);
    randomCloud.setStartTime(hour(nextCloudStart), minute(nextCloudStart));
    randomCloud.setEndTime(hour(nextCloudEnd) , minute(nextCloudEnd));
    randomCloud.begin();
  }
}

Fade.h

#ifndef Fade_h
#define Fade_h

#include "Arduino.h"

class Fade
{
  public:
    Fade() {};
    Fade(int pin, uint32_t timeStep = 15, uint8_t min = 0, uint8_t max = 255);
    void begin(void);
    void setTarget(int to);
    void update(void);
    void update(uint32_t time);
    uint8_t read(void);
    uint32_t readSpeed(void);
    uint32_t setSpeed(uint32_t time);
    uint8_t getSetpoint(void);
  private:
    uint8_t _min;
    uint8_t _max;
    uint8_t _targetFade;
    uint8_t _pwmRate;
    uint32_t _time;
    uint32_t _last;
    uint8_t _pin;
};

#endif

fade.cpp

#include "Arduino.h"
#include "Fade.h"

Fade::Fade(int pin, uint32_t timeStep, uint8_t min, uint8_t max)
{
  _pin = pin;
  _time = timeStep;
  _min = min;
  _max = max;
}

void Fade::begin(void)
{
  pinMode(_pin, OUTPUT);
  analogWrite(_pin, _min);
  _pwmRate = _min;
}

void Fade::setTarget(int to)
{
  _targetFade = (uint8_t) constrain(to, _min, _max);

  update();
}

void Fade::update()
{
  update(millis());
}

void Fade::update(uint32_t time)
{
  if (time - _time > _last)
  {
    _last = time;
    if (_pwmRate > _targetFade) analogWrite(_pin, --_pwmRate);
    if (_pwmRate < _targetFade) analogWrite(_pin, ++_pwmRate);
  }
}

uint8_t Fade::getSetpoint()
{
  return _targetFade;
}

uint8_t Fade::read()
{
  return _pwmRate;
}

uint32_t Fade::readSpeed()
{
  return _time;
}

uint32_t Fade::setSpeed(uint32_t time)
{
  _time = time;
}

I think I meant what are you telling the processor when you type led.update(). You use led.begin() led.update() led.target() So what does led denote? Is it referring to ledpin in the line const byte ledPin=13; ?

Downwardflight: [...] led.target() So what does led denote? Is it referring to ledpin in the line const byte ledPin=13; ?

No, pwmpin is assigned to the led object when it is created:

Fade led(pwmPin, 100); // 100 is the interval between changes to PWM output

aarg: No, pwmpin is assigned to the led object when it is created:

k++

Downwardflight: I think I meant what are you telling the processor when you type led.update(). You use led.begin() led.update() led.target()

I don't speak when I type! ;)

led is a Fade object

begin(), update() and setTarget() are each member functions of the Fade class.

update() seeks to always move the fading towards the current set point but without any calls to delay. It is agnostic to the direction of the change in set point; rather, you merely need to indicate the PWM output you wish to output next.

Did you try it?

Ok I am starting to follow you now.

I don't speak when I type! ;)

lol ok, what verb am I looking for here? What are you writing to the processor?

Did you try it?

I haven't had a chance as I am at work, hoping to get time to on my lunch break

I am getting compilation errors

Arduino: 1.0.6 (Windows NT (unknown)), Board: "Arduino Nano w/ ATmega328"
In file included from sketch_nov14a.ino:1:
C:\Users\Master\Documents\Arduino\libraries\DailyTimer/DailyTimer.h:80: error: 'time_t' does not name a type
sketch_nov14a.ino: In function 'void setup()':
sketch_nov14a:29: error: 'setTime' was not declared in this scope
sketch_nov14a.ino: In function 'void loop()':
sketch_nov14a:44: error: 'hour' was not declared in this scope
sketch_nov14a:44: error: 'minute' was not declared in this scope
sketch_nov14a:44: error: 'second' was not declared in this scope
sketch_nov14a:44: error: 'month' was not declared in this scope
sketch_nov14a:44: error: 'day' was not declared in this scope
sketch_nov14a:44: error: 'year' was not declared in this scope
C:\Users\Master\Documents\Arduino\libraries\DailyTimer/DailyTimer.h: In function 'void setNextCloud()':
C:\Users\Master\Documents\Arduino\libraries\DailyTimer/DailyTimer.h:79: error: 'static bool DailyTimer::isActive(DailyTimer*)' is protected
sketch_nov14a:78: error: within this context
sketch_nov14a:78: error: no matching function for call to 'DailyTimer::isActive()'
C:\Users\Master\Documents\Arduino\libraries\DailyTimer/DailyTimer.h:79: note: candidates are: static bool DailyTimer::isActive(DailyTimer*)
sketch_nov14a:81: error: 'time_t' was not declared in this scope
sketch_nov14a:81: error: expected `;' before 'now'
sketch_nov14a:82: error: expected `;' before 'nextCloudStart'
sketch_nov14a:83: error: expected `;' before 'nextCloudEnd'
sketch_nov14a:85: error: 'nextCloudStart' was not declared in this scope
sketch_nov14a:85: error: 'hour' was not declared in this scope
sketch_nov14a:85: error: 'minute' was not declared in this scope
sketch_nov14a:85: error: 'nextCloudEnd' was not declared in this scope

I copied and pasted the Fade.h and fade.cpp to notepad and saved them separately in the dailytimer folder I already have in libraries

Downwardflight: I am getting compilation errors

Arduino: 1.0.6 (Windows NT (unknown)), Board: "Arduino Nano w/ ATmega328"
In file included from sketch_nov14a.ino:1:
C:\Users\Master\Documents\Arduino\libraries\DailyTimer/DailyTimer.h:80: error: 'time_t' does not name a type
sketch_nov14a.ino: In function 'void setup()':
sketch_nov14a:29: error: 'setTime' was not declared in this scope
sketch_nov14a.ino: In function 'void loop()':
sketch_nov14a:44: error: 'hour' was not declared in this scope
sketch_nov14a:44: error: 'minute' was not declared in this scope
sketch_nov14a:44: error: 'second' was not declared in this scope
sketch_nov14a:44: error: 'month' was not declared in this scope
sketch_nov14a:44: error: 'day' was not declared in this scope
sketch_nov14a:44: error: 'year' was not declared in this scope
C:\Users\Master\Documents\Arduino\libraries\DailyTimer/DailyTimer.h: In function 'void setNextCloud()':
C:\Users\Master\Documents\Arduino\libraries\DailyTimer/DailyTimer.h:79: error: 'static bool DailyTimer::isActive(DailyTimer*)' is protected
sketch_nov14a:78: error: within this context
sketch_nov14a:78: error: no matching function for call to 'DailyTimer::isActive()'
C:\Users\Master\Documents\Arduino\libraries\DailyTimer/DailyTimer.h:79: note: candidates are: static bool DailyTimer::isActive(DailyTimer*)
sketch_nov14a:81: error: 'time_t' was not declared in this scope
sketch_nov14a:81: error: expected `;' before 'now'
sketch_nov14a:82: error: expected `;' before 'nextCloudStart'
sketch_nov14a:83: error: expected `;' before 'nextCloudEnd'
sketch_nov14a:85: error: 'nextCloudStart' was not declared in this scope
sketch_nov14a:85: error: 'hour' was not declared in this scope
sketch_nov14a:85: error: 'minute' was not declared in this scope
sketch_nov14a:85: error: 'nextCloudEnd' was not declared in this scope

I copied and pasted the Fade.h and fade.cpp to notepad and saved them separately in the dailytimer folder I already have in libraries

all four files need to be in the folder with the ino file.

oh ok, I thought they had to be in the libraries folder. So I named the sketch led_example and moved the files like the attachment shows, now I get

  enabled in File > Preferences.
Arduino: 1.0.6 (Windows NT (unknown)), Board: "Arduino Nano w/ ATmega328"
In file included from DailyTimer.cpp:29:
DailyTimer.h:80: error: 'time_t' does not name a type
DailyTimer.cpp:32: error: 'nullptr' was not declared in this scope
DailyTimer.cpp: In member function 'void DailyTimer::setDaysActive(EventDays)':
DailyTimer.cpp:79: error: 'time_t' was not declared in this scope
DailyTimer.cpp:79: error: expected `;' before 'now_time'
DailyTimer.cpp:80: error: 'now_time' was not declared in this scope
DailyTimer.cpp:80: error: 'year' was not declared in this scope
DailyTimer.cpp:80: error: 'month' was not declared in this scope
DailyTimer.cpp:80: error: 'day' was not declared in this scope
DailyTimer.cpp:80: error: 'tmConvert_t' was not declared in this scope
DailyTimer.cpp: In member function 'void DailyTimer::setDaysActive(byte)':
DailyTimer.cpp:94: error: 'time_t' was not declared in this scope
DailyTimer.cpp:94: error: expected `;' before 'now_time'
DailyTimer.cpp:95: error: 'now_time' was not declared in this scope
DailyTimer.cpp:95: error: 'year' was not declared in this scope
DailyTimer.cpp:95: error: 'month' was not declared in this scope
DailyTimer.cpp:95: error: 'day' was not declared in this scope
DailyTimer.cpp:95: error: 'tmConvert_t' was not declared in this scope
DailyTimer.cpp: In member function 'uint8_t DailyTimer::setRandomDays(uint8_t)':
DailyTimer.cpp:138: error: 'now' was not declared in this scope
DailyTimer.cpp: In static member function 'static bool DailyTimer::isActive(DailyTimer*)':
DailyTimer.cpp:198: error: 'weekday' was not declared in this scope
DailyTimer.cpp:200: error: 'now' was not declared in this scope
DailyTimer.cpp:217: error: 'time_t' was not declared in this scope
DailyTimer.cpp:217: error: expected `;' before 'now_time'
DailyTimer.cpp:218: error: expected `;' before 'on_time'
DailyTimer.cpp:219: error: expected `;' before 'off_time'
DailyTimer.cpp:220: error: 'now_time' was not declared in this scope
DailyTimer.cpp:220: error: 'weekday' was not declared in this scope
DailyTimer.cpp:228: error: 'on_time' was not declared in this scope
DailyTimer.cpp:228: error: 'off_time' was not declared in this scope
DailyTimer.cpp:251: error: 'on_time' was not declared in this scope
DailyTimer.cpp:251: error: 'off_time' was not declared in this scope
DailyTimer.cpp:262: error: 'off_time' was not declared in this scope
DailyTimer.cpp: At global scope:
DailyTimer.cpp:270: error: 'time_t' does not name a type

Check to make sure the spelling and capitalization are correct. Also, make sure you are using the code I posted on this topic, not the GitHub link.

Screen Shot 2017-11-14 at 13.09.23.png

Ok thanks for your patience, I will try your suggestions when I get home from work

So I think the initial problem was that I was using an outdated version of the Arduino IDE, I updated to 1.8.5 and now the only error I have is

In file included from C:\Users\Master\Documents\Arduino\led_example\led_example.ino:1:0:

sketch\DailyTimer.h:20:21: fatal error: TimeLib.h: No such file or directory

 #include <TimeLib.h>

Looking at DailyTimer.h there are two lines:
#include <Time.h>
#include <TimeLib.h>
I did not have either of these files, so I downloaded, extracted, and moved them to the led_example folder the .ino of that sketch is in. This is the latest error message:

Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"

In file included from C:\Users\Master\Documents\Arduino\led_example\led_example.ino:1:0:

sketch\DailyTimer.h: In function 'void setNextCloud()':

DailyTimer.h:79: error: 'static bool DailyTimer::isActive(DailyTimer*)' is protected

     static bool isActive(DailyTimer* instance);

                 ^

led_example:78: error: within this context

   if(sunRiseSunSet.isActive())

                    ^

led_example:78: error: no matching function for call to 'DailyTimer::isActive()'

   if(sunRiseSunSet.isActive())

                             ^

C:\Users\Master\Documents\Arduino\led_example\led_example.ino:78:29: note: candidate is:

In file included from C:\Users\Master\Documents\Arduino\led_example\led_example.ino:1:0:

sketch\DailyTimer.h:79:17: note: static bool DailyTimer::isActive(DailyTimer*)

     static bool isActive(DailyTimer* instance);

                 ^

sketch\DailyTimer.h:79:17: note:   candidate expects 1 argument, 0 provided

DailyTimer.h:80: error: 'static time_t DailyTimer::tmConvert_t(int, byte, byte, byte, byte, byte)' is protected

     static time_t tmConvert_t(int YYYY, byte MM, byte DD, byte hh, byte mm, byte ss);

                   ^

led_example:81: error: within this context

     time_t now = DailyTimer::tmConvert_t(year(), month(), day(), hour(), minute(), second());

                                                                                            ^

exit status 1
'static bool DailyTimer::isActive(DailyTimer*)' is protected

I commented out the "private:" line in the code to see what would happen and latest error is

Arduino: 1.8.2 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\Master\Documents\Arduino\led_example\led_example.ino: In function 'void setNextCloud()':

led_example:78: error: no matching function for call to 'DailyTimer::isActive()'

   if(sunRiseSunSet.isActive())

                             ^

C:\Users\Master\Documents\Arduino\led_example\led_example.ino:78:29: note: candidate is:

In file included from C:\Users\Master\Documents\Arduino\led_example\led_example.ino:1:0:

sketch\DailyTimer.h:79:17: note: static bool DailyTimer::isActive(DailyTimer*)

     static bool isActive(DailyTimer* instance);

                 ^

sketch\DailyTimer.h:79:17: note:   candidate expects 1 argument, 0 provided

exit status 1
no matching function for call to 'DailyTimer::isActive()'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I'm assuming the code compiles fine for you, what am I doing wrong here?

check the files are correct:

I’ve attache all 5

SunSim.zip (7.37 KB)

The new files you sent work great, thank you, I'm not sure what the difference was with the files I had. :confused: Also, I have a few questions about this code, if you don't mind;

On the line: "Serial.println(F("another cloud coming"));"

Is "another cloud coming" supposed to print to the serial monitor? I've been running it for a few hours uninterrupted after playing with the sun rise time and haven't seen anything other than the time and date printing to the serial monitor.

How would I print the intensity to the serial monitor as well as a screen? Im assuming I will also have to make a class struct for the intensity in order to display an image on a screen, like a cloud or sunrise etc.

What determines the start PWM amount on the led? Or the steps between changes?

Currently I'm only using a 3mm low power LED and it seems to jump on then fade up and I'm wondering if more resolution or a gentler step will be needed once I incorporate the Meanwell drivers and 3w LED arrays. It would be great if this was something I could fine tune.

On this line: DailyTimer sunRiseSunSet(true, 9, 00 , 22, 0, EVERY_DAY, FIXED, sunrise, sunset);

How to I make the Start and End times into something I can set with buttons or a TFT?

Thanks.

Downwardflight: The new files you sent work great, thank you, I'm not sure what the difference was with the files I had. :confused: Also, I have a few questions about this code, if you don't mind;

On the line: "Serial.println(F("another cloud coming"));"

Is "another cloud coming" supposed to print to the serial monitor? I've been running it for a few hours uninterrupted after playing with the sun rise time and haven't seen anything other than the time and date printing to the serial monitor.

yes, if we were to implement a method for clouds, which we have not yet.

Downwardflight: How would I print the intensity to the serial monitor as well as a screen?

I suppose you'd add some more Serial print commands. Look at the read() function in the Fade library, it returns the current PWM value.

Downwardflight: What determines the start PWM amount on the led? Or the steps between changes?

that would be up to you to determine those values and program them in.

Downwardflight: How to I make the Start and End times into something I can set with buttons or a TFT?

they are something you can set... look at the setStartTime() and setEndTime() functions in the DailyTimer library!

yes, if we were to implement a method for clouds, which we have not yet.

Oh, ok, I was under the impression This:

if(sunRiseSunSet.isActive())
  {
    Serial.println(F("another cloud coming"));
    time_t now = DailyTimer::tmConvert_t(year(), month(), day(), hour(), minute(), second());
    time_t nextCloudStart = now + random(60, CLOUD_INTERVAL);
    time_t nextCloudEnd = nextCloudStart + random(60, CLOUD_DURATION);
    char buffer[32];
    sprintf(buffer, "Start %02d:%02d End %02d:%02d", hour(nextCloudStart), minute(nextCloudStart), hour(nextCloudEnd) , minute(nextCloudEnd));
    Serial.println(buffer);
    randomCloud.setStartTime(hour(nextCloudStart), minute(nextCloudStart));
    randomCloud.setEndTime(hour(nextCloudEnd) , minute(nextCloudEnd));
    randomCloud.begin();
  }

…was implementing the clouds.

Downwardflight: Oh, ok, I was under the impression This: ... ...was implementing the clouds.

yes, but we haven't yet affected the LEDs.

you could try something like this, which modifies the current led setting during the cloud event...

void cloud(void)
{
  led.setTarget(255 - random(50, 100)); // some random but noticeable amount
  Serial.println(F("Cloud"));
  digitalWrite(13, LOW);
}

void nocloud(void)
{
  led.setTarget(255);  // return to full brightness
  Serial.println(F("No Cloud"));
  digitalWrite(13, HIGH);
  setNextCloud();
}

void setNextCloud(void)
{
  if(sunRiseSunSet.isActive() and led.read() == 255) . // to avoid clouds during sunrise and sunset
  {
    Serial.println(F("another cloud coming"));
    time_t now = DailyTimer::tmConvert_t(year(), month(), day(), hour(), minute(), second());
    time_t nextCloudStart = now + random(60, CLOUD_INTERVAL);
    time_t nextCloudEnd = nextCloudStart + random(60, CLOUD_DURATION);
    char buffer[32];
    sprintf(buffer, "Start %02d:%02d End %02d:%02d", hour(nextCloudStart), minute(nextCloudStart), hour(nextCloudEnd) , minute(nextCloudEnd));
    Serial.println(buffer);
    randomCloud.setStartTime(hour(nextCloudStart), minute(nextCloudStart));
    randomCloud.setEndTime(hour(nextCloudEnd) , minute(nextCloudEnd));
    randomCloud.begin();
  }
}