0

I have a project that requires eight DIFFERENT lights to cycle between on and off at random times with random fade in, random fade out and random on/off durations. My strategy is to fade on, leave on for a random time, fade off, leave off for a random time, repeat. While right now I've got a random pin selected before each for loop, I'd like to use a for-loop to randomly choose a pin on which to run the WHOLE on/off cycle.

Here's my pseudocode. Or maybe it IS my code.

void setup() {
    int pin = 0;
    int fadeIn = 0;
    int fadeOut = 0;
    int onDuration = 0;
    int offDuration = 0;
}
void loop() {    
pin = random(2,8)
onDuration = random(2000,15000)
    for (fadeIn=0;fadeIn<255;i++) {
        analogWrite(pin,fadeIn)
    }
delay(onDuration)
pin = random(2,8)
offDuration = random(1000,7000)
    for (fadeOut=254;fadeOut>0;fadeOut--) {
        analogWrite(pin,fadeOut)
    }
delay(offDuration)
} 

The loop (on, then off) would be one instance of a cycle. If I wanted a SECOND instance of the cycle to kick off on another pin WHILE the first cycle was running, is that something I can do programmatically? or would I need eight controllers, each fading the light in and out at the same time?

dwwilson66
  • 6,806
  • 27
  • 72
  • 117

2 Answers2

1

In your code above, the fading in and fading out are not for a random time. Is that what you intended? If so, you'll need to add delays at each iteration of the loops.

Anyway, this is something you can do without 8 separate boards.

Because it's embedded, you can't multithread very easily. You'll need to implement your own task scheduler and each LED has to be thought of as its own task. Then you just keep track of the state each different LED is in (fading in, on, fading out, or off.) As you bounce between the different tasks, control each LED accordingly to the state.

As far as a timing based task scheduler, you have different options. Perhaps the easiest is to implement a periodic timer interrupt. The AVR datasheets explain this pretty well. For Arduino, there's some libraries you can also use. For example: http://playground.arduino.cc/code/timer1

Another option is to do something similar to this: http://arduino.cc/forum/index.php?PHPSESSID=3e72433bc4375ee6c20d56f3998762ca&topic=5686.msg44073#msg44073

Just some suggestions. Sounds like an interesting project. Good luck!

gregbeaty
  • 679
  • 4
  • 4
1

You can manage this sort of thing (multiple lights/threads) using a procedural style of coding with switch/case statements and keeping track of state through some vars. With the limited memory on Arduino this is sometimes the only way to go.

On the other hand, I've had much success doing this sort of lighting control with object-orient approaches using custom classes and custom libraries. Much easier and your loop()then only need to handle higher level logic and service each instance (e.g. tell it to update and the instance handles the logic for that).

The only issue is the limited memory - so it may depend on which specific board you are using. I'd suggest giving it a try though - will likely be fine memory wise and you will learn quite a lot.

Community
  • 1
  • 1
spring
  • 18,009
  • 15
  • 80
  • 160