I managed to create some frames, or PWM steps, the code is probably a bit messy and inelegant, but i don't know how to make it look better, however it works quite well, effectively creating a half stepping for led PWM
it defines a time frame, in the first half it simply loops with the same PWM output, in the second half of the frame it flickers between a PWM value and PWM-1 (if it does not get to -1)
here is that part of the code.
suggestions and comments are welcome!
void fadeDown(int values[]) //values[] is an array of int H,S,B
{
int pause=30; //"substeps" lasting time
unsigned long time_delay=400; //duration of each macro PWM step
unsigned long target; // clock time to reach for each PWM step
while(values[2]>=0) //values[] is H,S,B values[2] is the brightness, it starts at 99 so do the loop untill it's black(0)
{
target=millis()+time_delay; //calculate when the current macro PWM step ends
while(millis()<target) //this is the main loop for the PWM step
{
while(millis()<target-(time_delay/2)) //while we are in the first half of the macro PWM step...
{
H2R_HSBtoRGB( values[0], values[1], values[2], color); //set the rgb values for that color
ledsOn(); //write the rgb values to the leds
Serial.println(values[2]); //print for debugging
delay(pause);
}
// if we are here, the first half of the frame was done, now we flicker between the current brightness and the next (-1)
H2R_HSBtoRGB( values[0], values[1], values[2], color); //set the rgb values for that color
ledsOn(); //write the rgb values to the leds
Serial.print(values[2]); //print for debugging
Serial.print(" "); //print for debugging
delay(pause);
if(values[2]>0) //if the brightness is more than 0 we can continue
{
H2R_HSBtoRGB( values[0], values[1], values[2]-1, color); // temporary set "values[2]-1" and calculate color so it dims
ledsOn();//write the rgb values to the leds
Serial.println(values[2]-1); //print for debugging
delay(pause);
}
}
--values[2]; //the loop ended so we can subtract 1 and continue the loop
}
}
void ledsOn()
{
analogWrite(ledr, color[0]);
analogWrite(ledg, color[1]);
analogWrite(ledb, color[2]);
ledstatus=1;
}