I need to figure out how to fade in and out of multiple LEDS in a function with an Arduino. Can't use delay() because other things need to run while the light is fading. This is what it has so far, but does not work.
int value = 0; // variable to keep the actual value
int ledpin = 9; // light connected to digital pin 9
int p1 = 0;
void setup()
{
// nothing for setup
Serial.begin(9600);
}
void loop()
{
inout(50, 30, 9, 0, 0);
//inout(20, 20, 10);
}
void inout(int inDelay, int outDelay, int ledpin, long previousmil, int done)
{
if(millis() - previousmil>inDelay){
if (value <=255){
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
value+=5;
}
previousmil=millis();
}
if(value = 260)
done = 1;
if(millis() - previousmil>outDelay && done == 1){
if (value >0){
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
value-=5;
}
previousmil=millis();
}
}
In order to fade your LED off and on, gradually increase your PWM value from 0 (all the way off) to 255 (all the way on), and then back to 0 once again to complete the cycle. In the sketch below, the PWM value is set using a variable called brightness .
If you want to fade between colours, work in a colourspace which makes it easy and then convert back to RGB at the end. For example, work in HSL colour space, keep S and L constant (say a fully saturated and bright colour) and then "fade" H around the circle - you'll go from red through green, blue and back to red.
Get the effect you want: Fade-in, and fade-out; When you give a 12V trigger to the dimmer (or you can use pretty much any ON/OFF switch or Motion Sensor Switch to trigger the dimmer), your LEDs will slowly start fading in. When the 12V trigger is disconnected, your LEDs will slowly fade out and then turn off.
The only obvious thing is that you've got a state flag for which way to increment value, but you're not testing it in your first if. It would be better to structure your code a bit more. You also may want to track more than one value if you have more than one pin, unless they all should fade in and out at the same time. In that case you'd be best off with an array of struct
s with the parameters for each pine.
One way of using delay with multiple tasks is to have each task work on the time elapsed since the last loop, and adjust the delay at the end of your loop for the time the tasks take. If your loop is something like:
static unsigned long last_time = 0; // set to millis() in setup
static unsigned long refresh_period = 20; // 50Hz
void loop()
{
unsigned long time = millis();
unsigned long delta = time - last_time;
doTask1( delta );
doTask2( delta );
doTask3( delta );
doTask4( delta );
// as tasks may have taken some millis, adjust delay
unsigned long post_time = millis();
if ( post_time - last_time < refresh_period )
delay( refresh_period - ( post_time - last_time ) );
last_time = time;
}
Then each task will be run about once every 20ms, and will be passed 20 or so as the number of milliseconds to update their state for. So you get some delay, but everything has a chance to update.
Speculating, but this seems wrong:
if(value=260)
(reminding me of the worlds last mistake in C)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With