0
我試圖在Arduino Uno上的多個LED上淡出淡出。這裏是我寫的代碼嘗試在多個LED上淡入淡出Arduino的奇怪問題
int led[2] = {9,10}; // the pin that the LED is attached to
int brightness[2] = {0,0}; // how bright the LED is
int fadeAmount[2] = {5,15}; // how many points to fade the LED by
long previousMillis[2] = {0,0}; // will store last time LED was updated
long interval[2] = {30, 50};
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led[0], OUTPUT);
pinMode(led[1], OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pins:
for (int counter = 0; counter < 2; counter++) {
unsigned long currentMillis = millis();
analogWrite(led[counter], brightness[counter]);
Serial.print("LED ");
Serial.print(led[counter]);
Serial.print(": Brightness ");
Serial.println(brightness[counter]);
if (currentMillis - previousMillis[counter] > interval[counter]) {
// change the brightness for next time through the loop:
brightness[counter] = brightness[counter] + fadeAmount[counter];
// reverse the direction of the fading at the ends of the fade:
if (brightness[counter] == 0 || brightness[counter] == 255) {
fadeAmount[counter] = -fadeAmount[counter] ;
}
}
}
}
這是奇怪的事情。如果我註釋掉串行的東西(打印和開始),淡入淡出不起作用。他們只是「閃爍」一點。
任何想法是什麼錯?
代碼太快?嘗試添加睡眠而不是打印語句並查看它是如何工作的......但是您可能正在爲您的眼睛過渡過快。 – neagoegab