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 ...
随机推荐
- 代码生成引擎之T4模版
在学校三年.公司里呆了快一年了,作用ASP.NET开发的我,居然从来没听过T4模版,公司里也没有人使用,它就是这样不为世人所熟知,却又默默的奉献着!这...........tm还是我吗?什么时候会说这 ...
- .net通用权限框架B/S(一)
一直做软件实施,用过一些二次开发平台,最近看了一些大神写的框架,于是参考写了一个B/S通用权限框架,项目使用MVC4+EF5+EASYUI(.net framework4),开发环境vs2010+sq ...
- APP安全测评checklist
leader不要打我啊,我要借用一下我组app的安全测评检查方案,这些最基本的安全防范措施应该是每个app都要注意的吧: 对了,首先,你的app得先混淆啊~:AndroidStudio 混淆打包 先来 ...
- 在Activity中动态设置TextView的隐藏属性
if (true) { //显示 viewHolder.tvLine.setVisibility(View.INVISIBLE);} else { //不显示 viewHolder.tvLine.se ...
- SQL性能优化的思路建议
如何在 Oracle数据库里写出高质量的SQL语句,如何在Oracle数据库里对有性能问题的SQL做诊断和调整,这是DBA们在ORACLE数据库实践中不可避免的难题.下面就让我们来分析一下拿到一条问题 ...
- php基础_2
php可变变量: $a = "hello"; $$a = "world"; echo $a . $$a; 输出:hello world; current — 返 ...
- Handle 消息机制
android中Handle类的主要作用: 1.在新启动的线程中发送给消息 2.在主线程获取.处理消息 为什么要用Handle这样的一个机制: 因为在Android系统中UI操作并不是线程安全的,如果 ...
- android 快速创建一个新的线程
要给一个activity做成子线程的模式 第一种:直接创建子线程并启动 private Thread newThread; //声明一个子线程 new Thread() { @Override pub ...
- Elevator
问题陈述: 杭州电子科技大学 HANGZHOU DIANZI UNIVERSITY Online Judge Problem - 1008 问题分析: 简单题. 代码详解: #include < ...
- Oracle EBS-SQL (PO-3):检查期间手工下达的采购订单记录数.sql
SELECT DECODE(pda.req_distribution_id,'','手工','自动创建') 下达方式, --pda.req_distribution_id ...