/* 2022-04-16 18:00 201912101045 王雅星
LED灯
根据模拟传感器的值打开一系列 LED 。
这是一种制作条形图显示的简单方法。
LED ,您可以使用任何数字通过改变 LED 计数和引脚在
数组
这种方法可以用来控制任何一系列的数字输出,取决于
在模拟输入端
电路:
- LED 从引脚 2 到引脚 11 接地
*/
// 定义
const int analogPin = A0; // 滑动电阻A0
const int ledCount = 10; // LED的数量
#define SPEAKER_PIN 8 // buzze接口1
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
}; // 数组 led灯
void setup() {
Serial.begin(115200);
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
// 读阻值
int sensorReading = analogRead(analogPin);
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// 根据滑动电阻阻值亮led数量
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
Serial.print(SPEAKER_PIN,HIGH);
}