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