当所有 LED 都应该闪烁时,一些 LED 会褪色,而其他 LED 会闪烁 Arduino Nano
Some LED fade and others will flash when all LED should flash Adrunio Nano
我的 Arduino 代码和电路有问题。目标是让每个 LED 一个接一个地变暗。这没有正确发生。一些 LED 会正确地淡出和淡出,然后一些会闪烁。我一直在尝试排除故障,这是我已经完成但尚未解决的问题。
- 使用了不同的板
- 交换 LED
- 使用了不同的电阻
- 交换了闪烁的引脚到褪色的引脚,闪烁将逐渐消失
- 将电路移至不同的面包板
- 检查代码是否通过串口监视器发送正确的光照水平
这是我的板子的照片
代码如下:
const int BUTTON = 2; // Naming switch button pin
const int LED1 = 3; // Namin LED pin
const int LED2 = 4;
const int LED3 = 5;
const int LED4 = 6;
const int LED5 = 7;
const int LED6 = 8;
const int LED7 =9;
const int LED8 = 10;
const int LED9 = 11;
int BUTTONstate = 0; // A variable to store Button Status / Input
int brightness = 0;
int fadeAmount =5;
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);
pinMode(LED9, OUTPUT);
pinMode (BUTTON, INPUT);
Serial.begin(9600);
}
void loop() {
BUTTONstate = digitalRead(BUTTON); // Reading button status / input
if (BUTTONstate == HIGH) // Condition to check button input
{
FlashingLight();
}
else
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
digitalWrite(LED8, LOW);
digitalWrite(LED9, LOW);
}
}
void FlashingLight()
{
for ( int i = 0; i<=4;i++){
//digitalWrite(LED, LOW);
fading(LED1); //Fades
fading(LED2); // blinks
fading(LED3); //fades
fading(LED4); //fades
fading(LED5); //blinks
fading(LED6); //blinks
fading(LED7); //fades
fading(LED8); //fades
fading(LED9); //fades
delay(1000);
}
}
void fading(int val) {
//brightness =0;
//digitalWrite(LED, LOW);
analogWrite(val,brightness);
for (brightness = 0; brightness <= 150; brightness += 5) {
analogWrite(val,brightness);
delay(30);
Serial.println(brightness);
}
for (brightness = 150; brightness >= 0; brightness -= 5) {
analogWrite(val,brightness);
delay(30);
Serial.println(brightness);
}
delay(100);
//brightness =0;
}
感谢您的帮助,如果您有任何问题,请告诉我,
根据this Arduino.cc reference,并非Arduino Nano的所有引脚都适合PWM控制(使用analogWrite()
)。在 Nano 上,只有引脚 3, 5, 6, 9, 10, 11
可用于输出 PWM 信号。
我的 Arduino 代码和电路有问题。目标是让每个 LED 一个接一个地变暗。这没有正确发生。一些 LED 会正确地淡出和淡出,然后一些会闪烁。我一直在尝试排除故障,这是我已经完成但尚未解决的问题。
- 使用了不同的板
- 交换 LED
- 使用了不同的电阻
- 交换了闪烁的引脚到褪色的引脚,闪烁将逐渐消失
- 将电路移至不同的面包板
- 检查代码是否通过串口监视器发送正确的光照水平
这是我的板子的照片
代码如下:
const int BUTTON = 2; // Naming switch button pin
const int LED1 = 3; // Namin LED pin
const int LED2 = 4;
const int LED3 = 5;
const int LED4 = 6;
const int LED5 = 7;
const int LED6 = 8;
const int LED7 =9;
const int LED8 = 10;
const int LED9 = 11;
int BUTTONstate = 0; // A variable to store Button Status / Input
int brightness = 0;
int fadeAmount =5;
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);
pinMode(LED9, OUTPUT);
pinMode (BUTTON, INPUT);
Serial.begin(9600);
}
void loop() {
BUTTONstate = digitalRead(BUTTON); // Reading button status / input
if (BUTTONstate == HIGH) // Condition to check button input
{
FlashingLight();
}
else
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
digitalWrite(LED8, LOW);
digitalWrite(LED9, LOW);
}
}
void FlashingLight()
{
for ( int i = 0; i<=4;i++){
//digitalWrite(LED, LOW);
fading(LED1); //Fades
fading(LED2); // blinks
fading(LED3); //fades
fading(LED4); //fades
fading(LED5); //blinks
fading(LED6); //blinks
fading(LED7); //fades
fading(LED8); //fades
fading(LED9); //fades
delay(1000);
}
}
void fading(int val) {
//brightness =0;
//digitalWrite(LED, LOW);
analogWrite(val,brightness);
for (brightness = 0; brightness <= 150; brightness += 5) {
analogWrite(val,brightness);
delay(30);
Serial.println(brightness);
}
for (brightness = 150; brightness >= 0; brightness -= 5) {
analogWrite(val,brightness);
delay(30);
Serial.println(brightness);
}
delay(100);
//brightness =0;
}
感谢您的帮助,如果您有任何问题,请告诉我,
根据this Arduino.cc reference,并非Arduino Nano的所有引脚都适合PWM控制(使用analogWrite()
)。在 Nano 上,只有引脚 3, 5, 6, 9, 10, 11
可用于输出 PWM 信号。