用户工具

站点工具


ocrobot:alpha:kitone:tutorial042

LED规律闪烁

这个例程创建了LED引脚数组,根据数组的规定来使多个LED规律闪烁。

硬件

搭建电路

  1. ALPHA 11 LED模块插入并行扩展版1号槽位。
  2. ALPHA MEGA328-U模块插入并行扩展板2号槽位。
  3. USB线连接计算机与ALPHA MEGA328-U。

代码

/*
  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语言的,因此这会变得很复杂,但使用简单的数组是比较简单的。

ocrobot/alpha/kitone/tutorial042.txt · 最后更改: 2023/06/07 04:23 由 127.0.0.1