int ledCount = 2;
int ledPins [ ] = {11,12 };
int brightness = 0;
int delayTime = 10;
void setup() {
pinMode(ledPins, OUTPUT);
}
void loop() {
while(brightness < 255)
{
analogWrite(ledPins, brightness);
delay(delayTime);
brightness = brightness + 1;
}
while(brightness > 0)
{
analogWrite(ledPins, brightness);
delay(delayTime);
brightness = brightness - 1;
}
}
答案 0 :(得分:1)
您正在将数组传递给analogWrite
或pinMode
,期待uint8_t
。
Arduino引脚操作功能一次只能处理一个引脚。通过直接操作AVR / ARM GPIO寄存器可以解决这个问题,但这些可能很挑剔(除非你 真的需要 速度,否则不建议使用。)
它说int *
的原因是因为在引擎盖下,C / C ++中的数组被表示为指针。
如果您想要analogWrite
或pinMode
两个LED,则必须为每个LED调用该功能一次。例如:
analogWrite(ledPins[0], brightness);
analogWrite(ledPins[1], brightness);
或者
for(int currentLED = 0;currentLED < ledCount;i++){
analogWrite(ledPins[currentLED], brightness);
}
在您的计划中:
int ledCount = 2;
int ledPins [] = {11, 12};
int brightness = 0;
int delayTime = 10;
#define INCREASE 1
#define DECREASE 2
int brightness_change = INCREASE;
void setup(){
for(int i = 0;i < ledCount;i++){
pinMode(i, OUTPUT);
}
}
void loop(){
while(brightness < 255 && brightness_change == INCREASE){
brightness = brightness + 1;
}
while(brightness > 0 && brightness_change == DECREASE){
brightness = brightness - 1;
}
if(brightness == 255){
brightness_change = DECREASE;
}
if(brightness == 0){
brightness_change = INCREASE;
}
for(int current_led = 0;current_led < ledCount;current_led++){
analogWrite(current_led, brightness;
}
delay(delayTime;
}
未经测试,但应该可以使用。