1. 按键控制
1.1 认识按键
- 目的:控制电动机或其他电气设备运行,常用来
连通或断开
,控制电路
1.2 工作原理
- 原理:K1、K2、K3、K4【开关量】与数字引脚相连,通过对按键操作,展现为电平的高低
- 不按的情况,K1连接R1,与VCC相连,表现为高电平
- 按下键时,K1直接与GND相连,表现为低电平
1.3 Arduino代码展示
//定义按键
int k1 = 8;
int k2 = 9;
int k3 = 10;
int k4 = 11;
void setup(){
Serial.begin(9600);
pinMode(k1, INPUT);
pinMode(k2, INPUT);
pinMode(k3, INPUT);
pinMode(k4, INPUT);
}
void loop(){
if(!digitalRead(k1)) //按下k1,digitalRead返回值为低电平【即返回为0】,取反为1,打印出“k1被按下”
Serial.println("K1 is pressed");
if(!digitalRead(k2))
Serial.println("K2 is pressed");
if(!digitalRead(k3))
Serial.println("K3 is pressed");
if(!digitalRead(k4))
Serial.println("K4 is pressed");
delay(100);
}
1.4 原理图
- k1被按下时,看窗口监视器输出什么
- 一下输出3个的“K1 is pressed”的原因是程序的抖动
1.5 实现去抖【消抖动延时】
int k1 = 8, k2 = 9, k3 = 10, k4 = 11; //定义按键
int key = 0; //定义键值
int key1 = 0;
void setup(){
Serial.begin(9600);
pinMode(k1, INPUT_PULLUP);
pinMode(k2, INPUT_PULLUP);
pinMode(k3, INPUT_PULLUP);
pinMode(k4, INPUT_PULLUP);
}
void read_key(){
if(!digitalRead(k1) || !digitalRead(k2) || !digitalRead(k3) || !digitalRead(k4)){ //同时读取4个按键值,只要有一个按键被按下,则显示为低电平,取反之后为1,条件为真,执行程序
delay(25); //消抖动延时
if(!digitalRead(k1) || !digitalRead(k2) || !digitalRead(k3) || !digitalRead(k4)){ //再次读取键值,确认是否有按键
if(!digitalRead(k1)) key = 1; //记录按下的键值
if(!digitalRead(k2)) key = 2;
if(!digitalRead(k3)) key = 3;
if(!digitalRead(k4)) key = 4;
}
else key = 0;
}
if(key1 != key) //只要这个键一直按着不松开,则认为按一个键。屏蔽标准键盘输入的联机问题,不会认为按许多键
key1 = key;
else
key = 0;
}
void loop(){ //主函数,先查询有无按键,有按键的话在屏幕显示结果
read_key(); //调用读取按键的函数
if(!key){
Serial.print("K");
Serial.print(key);
Serial.println(" is pressed");
key = 0; //再初始化为0,方面下次循环
}
}
2. 蜂鸣器控制
2.1 认识蜂鸣器
- 蜂鸣器是一种一体化结构的电子讯响器,采用直流电压供电,主要用于电子产品中作发生器件。
2.2 分类
- 按结构
- 压电蜂鸣器
- 电磁蜂鸣器
- 按有无振荡源
- 有源蜂鸣器
- 无源蜂鸣器
a为有源蜂鸣器,b为无源蜂鸣器文章来源:https://www.toymoban.com/news/detail-421682.html
2.3 工作原理
- 有源蜂鸣器
- 有源蜂鸣器内部含有振荡电路,主要是通过供应直流电,将
恒定直流电
转化为一定频率的脉冲信号
,从而实现磁场交变,带动钼片振动发声。
- 无源蜂鸣器
- 内部不带震荡源,所以加直流电,磁路恒定,钼片不能振动发声。只有加
方波
的理想信号,频率不同,震荡出来声音也不同。
2.4 连线
- 蜂鸣器共连两条线,正极连接到数字8口,负极连接到GND插口中。
2.5 Arduino代码展示
int buzzer = 8; //定义控制蜂鸣器的数字IO脚,另一脚连接到GND上
void setup(){
pinMode(buzzer, OUTPUT); //设置数字IO模式,OUTPUT为输出
//Arduino输送信号控制蜂鸣器响不响,所以定义为OUTPUT
}
void loop(){
unsigned char i, j; //定义变量
while(1){
for(i = 0; i < 80; i ++){ //输出一个频率的声音
digitalWrite(buzzer, HIGH); //发声音
delay(1); //延迟1ms
digitalWrite(buzzer, LOW); //不发声音
delay(1); //延迟1ms
}
for(i = 0; i < 100; i ++){ //输出另一个频率的声音
digitalWrite(buzzer, HIGH);
delay(2); //延迟2ms
digitalWrite(buzzer, LOW);
delay(2);
}
}
}
3. PWM模拟量输出
亮度发生渐变的三色灯——“呼吸灯”,实现灯的变化
文章来源地址https://www.toymoban.com/news/detail-421682.html
3.2 Arduino代码展示
const int red = 3;
const int green = 4;
const int blue = 5;
//3、4、5属于PWM引脚
void setup(){
set pins 2 through 13 as outputs:
{
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}
}
void loop(){
//低--->高变化【白色形成】
for(int brightness = 0; brightness < 255; brightness ++){ //0 ~ 255,红绿蓝同步逐渐发生变化
analogWrite(red, brightness);
analogWrite(green, brightness);
analogWrite(blue, brightness);
delay(3);
}
//高--->低变化【白色褪色】
for(int brightness = 255; brightness >= 0; brightness --){
analogWrite(red, brightness);
analogWrite(green, brightness);
analogWrite(blue, brightness);
delay(3);
}
//显示红色
for(int brightness = 255; brightness >= 0; brightness --){
analogWrite(red, brightness);
//analogWrite(green, brightness);
//analogWrite(blue, brightness);
delay(3);
}
//显示绿色
for(int brightness = 255; brightness >= 0; brightness --){
//analogWrite(red, brightness);
analogWrite(green, brightness);
//analogWrite(blue, brightness);
delay(3);
}
//显示蓝色
for(int brightness = 255; brightness >= 0; brightness --){
//analogWrite(red, brightness);
//analogWrite(green, brightness);
analogWrite(blue, brightness);
delay(3);
}
//显示红绿的混色
for(int brightness = 255; brightness >= 0; brightness --){
analogWrite(red, brightness);
analogWrite(green, brightness);
//analogWrite(blue, brightness);
delay(3);
}
//显示红蓝混色
for(int brightness = 255; brightness >= 0; brightness --){
analogWrite(red, brightness);
//analogWrite(green, brightness);
analogWrite(blue, brightness);
delay(3);
}
//显示绿蓝混色
for(int brightness = 255; brightness >= 0; brightness --){
//analogWrite(red, brightness);
analogWrite(green, brightness);
analogWrite(blue, brightness);
delay(3);
}
}
4. 湿温度测量
4.1 认识器件
- 用来粗略测试现场环境的湿温度,多为DHT11湿温度传感器。温湿度是模拟信号,但模块经过处理转换成数字信号通过单总线输出。
4.2 传感器接口
- VCC — 电源引脚5V
- GND — 电源引脚GND
- DAT — 数字引脚 2
4.3 Arduino代码展示【封装库】
#include "dht11.h" //封装库的头文件
dht11 DHT11; //构造函数封装实例化
#define DHT11PIN 2 //定义引脚
void setup(){
Serial.begin(9600); //串口通信
Serial.println("DHT11 TEST PROGRAM");
Serial.print("LIBRARY VERSION");
Serial.println(DHT11LIB_VERSION);
Serial.println();
}
void loop(){
Serial.println("\n");
int chk = DHT11.read(DHT11PIN); //返回值为chk
Serial.print("Read sensor: ");
switch(chk){
case DHTLIB_OK
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM
Serial.println("Checksum error");
break;
case DHTBIL_ERROR_TIMEOUT
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("Humidity(%): "); //打印湿度
Serial.print((float)DHT11.humidity, 2); //显示两位小数
Serial.print("Temperature(oC): "); //打印温度
Serial.print((float)DHT11.temperature, 2);
Serial.print("Temperature(oF): "); //打印温度
Serial.print(Fahrenheit(DHT11.temperature), 2);
Serial.print("Temperature(K): "); //打印温度
Serial.print(Kelvin(DHT11.temperature), 2);
Serial.print("Dew Point(oC): "); //打印
Serial.print(dewPoint(DHT11.temperature, DHT11.humidity));
Serial.print("Dew PointFast(oC): "); //打印
Serial.print(dewPoint(DHT11.temperature, DHT11.humidity));
delay(2000);
}
}
到了这里,关于【嵌入式系统与入门】Day02 Arduino 按键、蜂鸣器与湿温度传感器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!