赞
踩
连通或断开
,控制电路//定义按键 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); }
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,方面下次循环 } }
a为有源蜂鸣器,b为无源蜂鸣器
恒定直流电
转化为一定频率的脉冲信号
,从而实现磁场交变,带动钼片振动发声。方波
的理想信号,频率不同,震荡出来声音也不同。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); } } }
亮度发生渐变的三色灯——“呼吸灯”,实现灯的变化
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); } }
#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); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。