Arduino项目04.Arduino LED条形图

此示例展示如何读取模拟引脚0处的模拟输入,将analogRead()中的值转换为电压,并将其输出到Arduino软件(IDE)的串口监视器。

必需的组件

你将需要以下组件:

  • 1 × Breadboard 面包板
  • 1 × Arduino Uno R3
  • 1 × 5k欧姆可变电阻(电位器)
  • 2 × 跳线
  • 8 × LED(LED条形图显示如下图所示)

程序

按照电路图连接面包板上的组件,如下图所示。

连接面包板
电路图

草图

在计算机上打开Arduino IDE软件。使用Arduino语言进行编码控制你的电路。通过单击“New”打开一个新的草图文件。

Sketch

10段LED条形图

LED条形图

这10段条形图LED有许多用途。紧凑的占用空间,简单的连接,它们易用于原型或成品。实质上,它们是10个独立的蓝色LED,每个都有独立的阳极和阴极连接。

它们也有黄色,红色和绿色。

注意 – 这些条形图上的引脚可能与数据表中列出的内容不同。将设备旋转180度将纠正变化,使得引脚11成为第一引脚。

Arduino代码

/*
   LED bar graph
   Turns on a series of LEDs based on the value of an analog sensor. 
   This is a simple way to make a bar graph display. 
   Though this graph uses 8LEDs, you can use any number by
      changing the LED count and the pins in the array.
   This method can be used to control any series of digital
      outputs that depends on an analog input.
*/

// these constants won't change:
const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 8; // the number of LEDs in the bar graph
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // an array of pin numbers to which LEDs are attached

void setup() {
   // loop over the pin array and set them all to output:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
      pinMode(ledPins[thisLed], OUTPUT);
   }
}

void loop() {
   // read the potentiometer:
   int sensorReading = analogRead(analogPin);
   // map the result to a range from 0 to the number of LEDs:
   int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
   // loop over the LED array:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
      // if the array element's index is less than ledLevel,
      // turn the pin for this element on:
      if (thisLed < ledLevel) {
         digitalWrite(ledPins[thisLed], HIGH);
      }else { // turn off all pins higher than the ledLevel:
         digitalWrite(ledPins[thisLed], LOW);
      }
   }
} 

代码说明

草图的工作方式是这样的:首先,你阅读输入。将输入值映射到输出范围,在这种情况下为十个LED。然后,你设置一个 for-loop 以迭代输出。如果系列中的输出数量低于映射的输入范围,则将其打开。如果没有,则将其关闭。

结果

当模拟读数的值增加时,你将看到LED逐个打开,而当读数减少时,LED逐个关闭。

发布者:suiyublg,转转请注明出处:https://huibian.net/1404-2/

(0)
suiyublg的头像suiyublg
上一篇 2017年4月14日 16:45
下一篇 2017年4月16日 16:48

相关推荐

  • Arduino进阶04.Arduino 中断

    中断(interrupt)停止Arduino的当前工作,以便可以完成一些其他工作。 假设你坐在家里和别人聊天。突然电话响了。你停止聊天,拿起电话与来电者通话。当你完成电话交谈后,你回去和电话响之前的那个人聊天。 同样,你可以把主程序想象成是与某人聊天,电话铃声使你停止聊天。中断服务程序是在电话上通话的过程。当通话结束后,你回到你聊天的主程序。这个例子准确地解…

    2017年4月8日
    24901
  • Arduino声音02.Arduino 无线通信

    无线发射器和接收器模块工作在315 Mhz。它们可以轻松地装入面包板,并可很好的与微控制器配合使用,创建一个非常简单的无线数据链路。使用一对发射器和接收器,模块将只能单向传输数据,因此,你将需要两对(不同频率)作为发射器/接收器对。 注意 – 这些模块是任意的,并会接收相当大量的噪音。发射器和接收器都在共同的频率下工作,并且没有ID。 …

    2017年4月30日
    29500
  • Arduino教程29.Arduino 无限循环

    它是没有终止条件的循环,因此循环变为无限。 无限循环语句语法 使用for循环 for (;;) { // statement block } 使用while循环 while(1) { // statement block } 使用do … while循环 do { Block of statements; } while(1);

    2017年3月25日
    15400
  • Arduino传感器05.Arduino 超声波传感器

    HC-SR04超声波传感器使用声纳来确定物体的距离,就像蝙蝠一样。它提供了非常好的非接触范围检测,准确度高,读数稳定,易于使用,尺寸从2厘米到400厘米或1英寸到13英尺不等。 其操作不受阳光或黑色材料的影响,尽管在声学上,柔软的材料(如布料等)可能难以检测到。它配有超声波发射器和接收器模块。 技术规格 电源 – + 5V DC静态电流 &#82…

    2017年4月24日
    31200
  • Arduino教程17.Arduino 布尔运算符

    假设变量A为10,变量B为20,则: 运算符名称 运算符简写 描述 例子 and(与) && 称为逻辑运算符与。如果两个操作数都是非零,那么条件为真。 (A && B)为真 or(或) || 称为逻辑OR运算符。如果两个操作数中的任何一个非零,则条件变为真。 (A || B)为真 not(非) ! 称为逻辑运算符非。用于反转其…

    2017年3月23日
    17400

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
欢迎大家来到大雄学编程!