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传感器02.Arduino 温度传感器

    温度传感器LM35系列是精密集成电路温度器件,输出电压与摄氏温度成线性比例。 LM35器件优于以开尔文校准的线性温度传感器,因为用户不需要从输出中减去大的恒定电压以获得便利的摄氏缩放。LM35器件不需要任何外部校准或调整,即可在室温下提供±1/4°C的典型精度,在-55°C至150°C的温度范围内提供±3°C的典型精度。 技术规格 必需的组件 你将需要以下组…

    2017年4月21日
    32100
  • Arduino项目02.Arduino 渐变LED

    这个例子演示了使用analogWrite()函数来渐变LED的功能。AnalogWrite使用脉冲宽度调制(PWM),以开和关之间的不同比率非常快速地打开和关闭数字引脚,以产生渐变效应。 必需的组件 你将需要以下组件: 程序 按照电路图连接面包板上的组件,如下图所示。 注意 − 要了解LED的极性,请仔细查看。两个腿中较短的,朝向灯泡的平坦边缘表示…

    2017年4月13日
    27700
  • Arduino电机控制03.Arduino 步进电机

    步进电机是无刷同步电机,它将完整的旋转分成多个步骤。与无刷直流电机不同,当向其施加固定的直流电压时,它将连续旋转,步进电机以不连续的步进角旋转。 因此,步进电机被制造成具有每转12,24,72,144,180和200的步长,从而产生每步30°,15°,5°,2.5°,2°和1.8°的步进角。步进电机可以有或没有反馈控制。 想象一下在RC飞机上的电机。电机在一…

    2017年4月28日
    46300
  • Arduino教程02.Arduino 板的说明

    在本章中,我们将了解 Arduino 板上的不同组件。将学习 Arduino UNO 板,因为它是 Arduino 板系列中最受欢迎的。此外,它是开始使用电子和编码的最佳板。有些板看起来与下面给出的有些不同,但多数 Arduino 中的这些组件大部分是共同的。

    2017年3月18日
    30702
  • Arduino进阶07.Arduino 串行外设接口

    串行外设接口(SPI)总线是用于串行通信的系统,最多可使用四个导体,通常为三个。一个导体用于数据接收,一个导体用于数据发送,一个导体用于同步,另一个导体用于选择与之通信的设备。它是一个全双工连接,这意味着数据是同时发送和接收的。最大波特率高于I2C通信系统中的波特率。

    2017年4月11日
    47466

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

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