这个例程创建了LED引脚数组,根据数组的规定来使多个LED规律闪烁。
/* Arrays,创建LED引脚数组,根据引脚位置在数组中的不同,来使多个LED规律闪烁 */ int timer = 100; // 数值越大,计时器越慢 int ledPins[] = { 2, 7, 4, 6, 5, 3 , 8 , 11 , 12 , 9 , 10 // 连接LED的引脚号的数组 }; int pinCount = 10; // 引脚个数 void setup() { for (int thisPin = 0; thisPin < pinCount; thisPin++) { // 数组从0到pinCount - 1 pinMode(ledPins[thisPin], OUTPUT); // 使用for循环来初始化引脚为输出 } } void loop() { for (int thisPin = 0; thisPin < pinCount; thisPin++) { // 最小引脚号到最大引脚号循环 digitalWrite(ledPins[thisPin], HIGH); // 打开: delay(timer); digitalWrite(ledPins[thisPin], LOW); // 关闭: } for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { // 最大引脚号到最小引脚号循环: digitalWrite(ledPins[thisPin], HIGH); // 打开: delay(timer); digitalWrite(ledPins[thisPin], LOW); // 关闭: } }
这个例子中我们使用了一个新的数据类型:数组
数组是一种可访问的变量的集合。ocrobot的数组是基于C语言的,因此这会变得很复杂,但使用简单的数组是比较简单的。