PPPlus -- 《Arduino程序设计基础》:记录贴
2.流水灯实验更新为:由右往左,由左往右,往复循环。#define LED_NUM 6 // led灯的数量
#define DELAY_TIME_MS 300 // 亮与灭的时间(单位:毫秒)
// led灯对应的数字管脚
const int LEDS = {
2, 3, 4, 5, 6, 7
};
void setup() {
unsigned int uiIndex = 0;
while (uiIndex < LED_NUM) {
pinMode(LEDS, OUTPUT);
++uiIndex;
}
}
void loop() {
static unsigned int uiIndex = 0;
static boolean bIsLeftOrRight = 1;
digitalWrite(LEDS, HIGH);
delay(DELAY_TIME_MS);
digitalWrite(LEDS, LOW);
if (bIsLeftOrRight)
++uiIndex;
else
--uiIndex;
if (uiIndex == LED_NUM) {
bIsLeftOrRight = !bIsLeftOrRight;
uiIndex = LED_NUM - 2;
} else if (uiIndex == 0xffff) {
bIsLeftOrRight = !bIsLeftOrRight;
uiIndex = 1;
}
}
页:
[1]