0

I have a series of eight LEDs that I'm trying to fade in and out randomly, starting from ON. Here's where I am so far.

#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
#define LED6 7
#define LED7 8
#define LED8 9

void setup() {
pinMode(LED1,output);
pinMode(LED2,output);
pinMode(LED3,output);
pinMode(LED4,output);
pinMode(LED5,output);
pinMode(LED6,output);
pinMode(LED7,output);
pinMode(LED8,output);
}

    void cycleLED() {
        timeOn = random(600,1800);
        timeOff = random(600,1800);
        for (fadeOut = 255; fadeOut > 0; fadeOut--) { 
            analogWrite(LED, fadeout);
            delay(timeOff); 
        }

        for (fadeIn = 0;fadeIn < 255; fadeIn++) { 
            analogWrite(LED,fadeIn);      
            delay(timeOn); 
        }
    }

Here's where I'm stuck. I want to do something similar to the following pseudocode.

activeLED = random(2,10);             // choose a random LED pin
LEDtoCycle = pinNumber-activeLED;     // set the active LED to the random LED pin 
cycleLED(pinNumber-activeLED);        // run cycleLED on the active LED

My intention is to select an LED at random, then to run cycleLED on that LED, repeating indefinitely. By choosing random However, I'm having a hard time wrapping my head around a good way to do this. How do I pass the pin variable to cycleLED()? Or should I repeatedly #define a single, random LED? Or should I just hardcode eight separate instances of cycleLED1(), cycleLED2(), cycleLED3(), and so forth.

Tips or suggestions?

1
  • "How do I pass the pin variable to cycleLED()?" - euh, using an argument to that function, perhaps? Commented Dec 31, 2012 at 14:37

1 Answer 1

2

You can make the cycleLED function take a parameter as follows:

void cycleLED(int led)

Then just use the led variable in your analogWrite calls in lieu of the mysterious LED thing you currently have.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.