Arduino周边模块:LED部件
Arduino周边模块:LED部件
Arduino周边模块:LED部件
1. LED的使用
LED的原理:
LED是会发光的二极管,它具有单向导电性。两端加上正向电压,即能将电能转化为光能。
正向电压就是正极加高电压,负极加低电压
对于LED的正负极判断:
- 一般长引脚的是正极,短引脚的是负极。
- 观察LED的头部,里面有一宽一窄两个金属块,一般窄的金属块连接的引脚是正极,宽的金属块连接的引脚是负极。
数字电平:
电压的另一种解读方式
高电平对应数字逻辑的1,低电平对应数字逻辑的0
Arduino的数字端口
(在数字端口不够用的情况下,模拟端口也能够充当数字端口使用)
Arduino的数字端口有两种模式:输入、输出
电路连接
我们将LED的负极连接到Arduino的八号端口,然后通过一个限流电阻,将LED的负极连接到Arduino的GND端口
面包板
面包板能够提供一个宽松的实验环境。元件直接插拔,无需焊接。很适合电子电路的组装、调试和训练。
面包板分为三个区域,上下两个区域是横向贯通的,中间的区域是纵向贯通的
Arduino控制程序的一般结构
1.void setup(){
2. //在此做一些准备工作
3.}
4.
5.void loop(){
6. //在此实现应用程序的功能
7.}
进入Arduino IDE,正式编写代码
1.void setup(){
2. pinMode(8,OUTPUT);
3.}
4.
5.void loop(){
6. digitalWrite(8,HIGH);
7. delay(1000);
8. digitalWrite(8,LOW);
9. delay(1000);
10.}
LED以每秒一次的频率闪烁着,delay()函数的单位是毫秒。
优化:
1.int led=8;
2.void setup(){
3. pinMode(led,OUTPUT);
4.}
5.
6.void loop(){
7. digitalWrite(led,HIGH);
8. delay(1000);
9. digitalWrite(led,LOW);
10. delay(1000);
11.}
Arduino数字IO相关库函数
| 函数原型 | 函数说明 |
|---|---|
| pinMode(pin,mode) | 配置特定引脚的工作模式 |
| digitalWrite(pin,value) | 向特定引脚输出数字电平 |
| delay(ms) | 产生一段固定时长的延时 |
2. LED点阵的使用
我们在街上看到的LED广告屏就是使用多个LED点阵拼接起来的
LED点阵原理图
LED点阵显示图形
关键字:
- 行扫描
- 视觉暂留
而计算机显示字符的原理,其实就是计算机中存储的字形码,字形码的作用就是记录字符的点阵数据
那么如何在Arduino程序中存储字库?
使用数组
数组是同一类型数据的集合
硬件连接
程序的框架图
1.//2-dimensional array of row pin numbers:
2.const int row[8]={
3.17,16,2,3,4,5,6,7};
4.
5.//2-dimensional array of column pin numbers:
6.const int col[8]={
7.8,9,10,11,12,13,19,18};
8.
9.//2-dimensional array of pixels:
10.const byte character_bank[2][8]={
11. {B00111100,
12. B01000010,
13. B01000010,
14. B01000010,
15. B01000010,
16. B01000010,
17. B01000010,
18. B00111100},
19.
20. {B00010000,
21. B00110000,
22. B00010000,
23. B00010000,
24. B00010000,
25. B00010000,
26. B00010000,
27. B00111000}
28.};
29.
30.int pixels[8][8];
31.
32.void setup(){
33. //initialize the I/O pins as output
34. for (int thisPin=0;thisPin<8;thisPin++){
35. //initialize the output pins:
36. pinMode(col[thisPin],OUTPUT);
37. pinMode(row[thisPin],OUTPUT);
38. //take the col pins(i.e. the cathodes) high to ensure that
39. //the LEDS are off:
40. digitalWrite(col[thisPin],HIGH);
41. }
42. //initialize the pixel matrix:
43. for (int x=0;x<8;x++){
44. for (int y=0;y<8;y++){
45. pixels[x][y]=HIGH;
46. }
47. }
48.}
49.
50.void loop(){
51. //draw the screen:
52. displayNum(1);
53. refreshScreen();
54.}
55.
56.void displayNum(int num)
57.{
58. for (int rowindex=0;rowindex<8;rowindex++)
59. {
60. for (int colindex=0;colindex<8;colindex++){
61. pixels[rowindex][colindex]=((character_bank[num][rowindex]>>(7-colindex))&0x01)==07HIGH:LOW;
62. }
63. }
64.}
65.
66.void refreshScreen(){
67. //iterate over the rows (anodes):
68. for (int thisRow=0;this Row<8;thisRow++){
69. //take the row pin (anode) high:
70. digitalWrite(row[thisRow],HIGH);
71. //iterate over the cols (cathodes):
72. for (int thisCol=0;thisCol<8;thisCol++){
73. //get the state of the current pixel;
74. int thisPixel=pixels[thisRow][thisCol];
75. //when the row is HIGH and the col is LOW,
76. //the LED where they meet turns on:
77. digitalWrite(col[thisCol],thisPixel);
78. //turn the pixel off:
79. if (thisPixel==LOW){
80. digitalWrite(col[thisCol],HIGH);
81. }
82. }
83. //take the row pin low to turn off the whole row:
84. digitalWrite(row[thisRow],LOW);
85. }
86.}
3. 三色LED的使用
加到各个引脚上的电压大小决定了相应分量的亮度,进而决定了混合而成的光的颜色
那么用什么来输出模拟电压呢?
PWM :PWM是用占空比(duty cycle)不同的方波,来模拟“模拟输出”的一种方式
Arduino中端口标号前有’~’的均可以输出PWM波
Arduino控制程序中PWM波的输出
analogWrite(pin,value)
第一个参数pin代表引脚标号
第二个参数value代表占空比的值。取值范围:0~255
总共有三个LED,因此我们可以有255*255*255=16581375个颜色
硬件连接
程序
1.//端口配置
2.int redPin=3;
3.int greenPin=5;
4.int bluePin=6;
5.
6.void setup()
7.{
8. pinMode(redPin,OUTPUT);
9. pinMode(greenPin,OUTPUT);
10. pinMode(bluePin,OUTPUT);
11.}
12.
13.void loop()
14.{
15. int red,green,blue;
16. blue=0;
17. for(blue=0,red=255;blue<256;blue++)
18. {
19. setColor(red,green,blue);
20. delay(4);
21. red--;
22. }
23. delay(100); //绿色向红色渐变
24.
25. green=0;
26. for(red=0,green=255;red<256;red++)
27. {
28. setColor(red,green,blue);
29. delay(4);
30. green--;
31. }
32. delay(100); //红色向篮色渐变
33.
34. red=0;
35. for(green=0,blue=255;green<256;green++)
36. {
37. setColor(red,green,blue);
38. delay(4);
39. blue--;
40. }
41. delay(100); //篮色向绿色渐变
42.}
43.
44.void setColor(int red,int green,int blue)
45.{
46. analogWrite(redPin,red);
47. analogWrite(greenPin,green);
48. analogWrite(bluePin,blue);
49.}
Arduino周边模块:LED部件的更多相关文章
- Arduino周边模块:传感器部件(温敏、光敏、湿敏)
Arduino周边模块:传感器部件(温敏.光敏.湿敏) Arduino周边模块:传感器部件(温敏.光敏.湿敏) Arduino的模数转换 对于Arduino来说,它只认识数字量,模拟量对其来说就是一门 ...
- Arduino周边模块:执行部件(舵机、直流电机、步进电机)
Arduino周边模块:执行部件 Arduino周边模块:执行部件 嵌入式系统的构成 如今已经有各种各样的基于Arduino的嵌入式系统, 比如:智能小车.3D打印机.机器人,甚至还有基于Arduin ...
- Arduino周边模块:LCD与数码管
Arduino周边模块:LCD与数码管 Arduino周边模块:LCD与数码管 数码管的介绍 数码管一般是用来显示数字和字符的 数码管原理 一位数码管 该图是一个8段数码管,该数码管中包含了8个LED ...
- 【Arduino】开发入门【十】Arduino蓝牙模块与Android实现通信
[Arduino]开发入门[十]蓝牙模块 首先show一下新入手的蓝牙模块 蓝牙参数特点 1.蓝牙核心模块使用HC-06从模块,引出接口包括VCC,GND,TXD,RXD,预留LED状态输出脚,单片机 ...
- Arduino 翻译系列 - LED 灯闪烁
原文地址 - https://www.arduino.cc/en/Tutorial/Blink 闪烁 这个例子展示了你能拿 Arduino / Genuino 板子来干的最简单的事:使开发板上的 LE ...
- Arduino 各种模块篇 蓝牙模块 手机蓝牙控制Arduino LED灯
解决方案. 条件: 1.手机android 商店下载 blueTerm 2.向arduino中载入如下代码: char val; ; void setup() { Serial.begin(); pi ...
- Arduino 各种模块篇 RGB LED灯
示例代码: 类似与这样的led,共阴rgb led,通过调节不同的亮度,组合成不同的颜色. 示例代码: /* 作者:极客工坊 时间:2012年12月18日 IDE版本号:1.0.1 发布地址:www. ...
- Arduino蓝牙模块实现通信
蓝牙参数特点 1.蓝牙核心模块使用HC-06从模块,引出接口包括VCC,GND,TXD,RXD,预留LED状态输出脚,单片机可通过该脚状态判断蓝牙是否已经连接 2.led指示蓝牙连接状态,闪烁表示没有 ...
- Arduino 各种模块篇 摇杆模块
Arduino的另外几种模块,我们常见的joystick摇杆模块. 用起来很爽,摇杆 有X,Y轴可调 这里有一篇非常想尽的示例代码: http://www.geek-workshop.com/foru ...
随机推荐
- C#运用实例.读取csv里面的词条,对每一个词条抓取百度百科相关资料,然后存取到数据库
第一步:首先需要将csv先装换成datatable,这样我们就容易进行对datatable进行遍历: /// 将CSV文件的数据读取到DataTable中 /// CSV文件路径 /// 返回读取了C ...
- C#整理3——运算符和语句
运算符: 一.算术运算符:+ - * / % ——取余运算 取余运算的应用场景:1.奇偶数的区分. 2.把数变化到某个范围之内.——彩票生成. 3.判断能否整除.——闰年.平年. using Syst ...
- 根据获取Enum名获取对应的值通用方法(仅限值为int的)
/// <summary> /// 获取枚举对应的值 /// </summary> /// <typeparam name="T">枚举类型&l ...
- iOS设计模式解析(三)适配器模式
适配器模式:将一个类的借口转换成客户端希望的另一个接口 有一个很直观的图: 例如 :电源适配器(将110V电压转换成220V电压,其中Traget是220V电压,adaptee就是110V电 ...
- web.config详解
在开发中经常会遇到这样的情况,在部署程序时为了保密起见并不将源代码随项目一同发布,而我们开发时的环境与部署环境可能不一致(比如数据库不一样),如果在代码中保存这些配置这些信息部署时需要到用户那里更改代 ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- IE9下Ajax缓存问题
使用jQuery的getJSON从后台定时获取数据并刷新界面,使用以下方法时,在Chrome,Firefox下没问题,但在IE9下却无法刷新数据 $.getJSON(webApp + "/G ...
- 新手讲树:证明任意二叉树度为零的节点n0,永远比度为2的节点n2多1个
证明: 设度为1的节点个数为n1,因为二叉树的所有节点的度都小于等于2, 所以n=n0+n1+n2; 又因为二叉树中,除了根节点所有的节点都有一个进入节点的分支,假设B为所有的分支,那么n=B+1 ...
- setTimeout()与setInterval() 问题
提示:setTimeout() 只执行 code 一次.如果要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeout(). 1. setInterval(c ...
- 迪杰斯特拉(Dijkstra)算法
# include <stdio.h> # define MAX_VERTEXES //最大顶点数 # define INFINITY ;//代表∞ typedef struct {/* ...