Blink and Fade LED's at the same time

Hello, I'm not to crash hot when it comes to coding this up so any help or direction is great...

So I'm aiming to have 6 LED's fading and 2 blinking all at the same time,

I couldn't find this being covered exactly before but I came across this to fade:

void setup(void) { SoftPWMBegin();

// Create on pins 9 and 10 SoftPWMSet(9, 0); SoftPWMSet(10, 0);

// Set full scale fade time of pin 9 (up in 500 ms, down in 1000 ms) SoftPWMSetFadeTime(9, 500, 1000); // Set full scale fade time of pin 10 (up in 200 ms, down in 500 ms) SoftPWMSetFadeTime(10, 200, 500); }

void loop(void) { SoftPWMSet(9, 255); SoftPWMSet(10, 255);

delay(1000); // we could do other things here instead of delay()

SoftPWMSet(9, 0); SoftPWMSet(10, 0);

delay(1000); }

and also this to blink 2 LED's:

void setup(){ pinMode(11,OUTPUT); pinMode(12,OUTPUT); }

void loop(){ digitalWrite(11,LOW); digitalWrite(12,HIGH); delay(500); digitalWrite(11,HIGH); digitalWrite(12,LOW); delay(500); }

But I'm not sure exactly how to combine them both into one sketch to make the LED's keep blinking and the other lights fade as this is happening.

Thanks for any help...

The level of difficulty of this project depends on whether the 6 LEDs fade at the same rate, and whether the 2 LEDs blink at the same rate.

The delay() in each sketch will prevent you from merging them.

If you think about how YOU (not the Arduino) would make this happen, you’d check your watch to see if it was time to turn an LED on. If it was, you’d turn it on, and note when you turned it on.

You’d see if it was time to turn an LED off. If it was, you’d turn it off, and note when you turned it off.

You’d see if it was time to change the PWM level for an LED. If it was, you’d change the value, and note when you changed the value.

Well, the Arduino doesn’t have a watch, but it does have the millis() function that returns “now”. You can make use of that to record when a change occurs, and what time it is now. If it’s time to do something, do it and record when you did it. Look at the Blink Without Delay example to see how it’s done.

Sounds like you might want to use some interrupts here. You could set up an interrupt for blink, and a seperate one for fade. PaulS is correct though, if they blink or fade at different rates, it suddenly gets much more complicated.

Inside your interrupt service routine, you can count the number of overflows, and fade the LEDs based on the number of overflows it counts. That way, you'd be able to fade more than one LED using only a single interrupt. They'd still be linked though, so you're still limited somewhat. If you explain a bit further about how exactly you want them to blink and fade, we might be able to help you out a little better.

Ok, thanks for the reply. It doesn't matter to much about the timing as this would be a visual to show that all the LED's I'm using work, so the easier and shorter code would be better for me.

Essenitally the 2 blinks would be going on and off every sec or so (at the same time or different to each other), while the fades would be a slow pulse over a couple of sec (any order and can vary).

So in the sketches I should try to change the delay's to a millisec count right?

Read through the blink without delay example. Understand what each step is doing. When you understand that code, modify it to have different on and off durations. When that works, modify it to blink 2 LEDs at different rates (one with 1 second on, 3 second off; the other at 2 seconds on 5 seconds off).

Then, add in code to fade a single LED. Then, extend that to fade 6 LEDs all the same. Then, extend it to fade all 6 LEDs with different durations.