0
我有一系列八个LED,我试图随机淡入和淡出,从ON开始。这是我到目前为止的地方。如何随机选择Arduino引脚输出?
#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);
}
}
这里是我卡住的地方。我想做类似于下面的伪代码的东西。
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
我的目的是选择的LED在随机的,那么运行在该LED cycleLED
,无限期地重复。通过选择随机然而,我很难把我的头围绕一个很好的方式来做到这一点。如何将引脚变量传递给cycleLED()?或者我应该重复#define
单个随机LED?或者我应该只是硬编码8个不同的实例cycleLED1()
,cycleLED2()
,cycleLED3()
等等。
提示或建议?
“我如何将pin变量传递给cycleLED()?” - 欧盟,使用该功能的参数,也许? – 2012-12-31 14:37:24