Hello
I want to have a LED strip go from 0 to 254 over a period of time. In the sketch below the time is 10 minutes. I have made ms by multiplying 10 with 60000. I then divide it again in 254 steps so each step gets the same amount of 'delay' before increasing the brightness.
The 'fake number' integer in the sketch is to trigger the 'sunrise' function. This wil be replaced by and RTC alarm later on. I'm using the 'sunUPorDOWN' to escape the function loop.
Now the problem is that there is no delay between increments. Is this not possible in a function? I have checked and modified it a few times but it's just not delaying. When I use the 'blink without delay' it does work... I'm confused. Can someone please point me in the right direction?
Thanks!
char sunRisePin =11; // control sunRisePin
unsigned long sunRiseTime = 10; // Tijd uit de settings in minuten!
unsigned long sunRisePrevious = 0; // will store last time LED was updated
int g_sunriseMax = 255;
long sunRiseInterval = (sunRiseTime*60000)/g_sunriseMax; // minutes to ms, devide by 255 steps. example: 10*60000=600000/255=2352ms per step
int sunUPorDOWN=0; // sun up now or not?
int FakeNumber = 1; // TESTING, of het werkt! 1 is sunrise, 0 is sunset.
// dus als het dag wordt sunrise aan en na nachtmodes sunset
void setup() {
pinMode(sunRisePin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (FakeNumber == 1 && sunUPorDOWN == 0) // sunrise
{ f_sunrise();}
if (FakeNumber == 0 && sunUPorDOWN == 1) // sunset bij eindtijd.
{ f_sunset();}
}
void f_sunrise(){
unsigned long sunRiseNow = millis();
if (sunRiseNow - sunRisePrevious >= sunRiseInterval){
for(int i=0; i<g_sunriseMax+1; i++)
{
analogWrite(sunRisePin, i);
Serial.println(i);
sunRisePrevious = sunRiseNow;
if (i == g_sunriseMax){sunUPorDOWN = 1;Serial.println("sun is up");}
}
}
}// einde f_sunrise
void f_sunset(){
unsigned long sunRiseNow = millis();
if (sunRiseNow - sunRisePrevious >= sunRiseInterval){
for(int i=g_sunriseMax; i>-1; i--)
{
analogWrite(sunRisePin,i);
Serial.println(i);
sunRisePrevious = sunRiseNow;
if (i == 0){sunUPorDOWN = 0;Serial.println("sun is down");}
}
}
}// einde f_sunset