1、Arduino开发从入门到实战程序源代码程序源码第五章 炫酷LED灯5.4.2 程序源码/项目一LED闪烁灯int LEDPin=13;void setup()pinMode(LEDPin,OUTPUT); /13脚设置为输出void loop()digitalWrite(LEDPin,HIGH); /设定PIN13脚为HIGH = 5V左右delay(1000); /设定延时时间,1000 = 1sdigitalWrite(LEDPin,LOW); /设定PIN13脚为LOW = 0V delay(1000); /设定延时时间,1000 = 1s5.7 拓展实验广告灯拓展实验参考程序为: /
2、设置控制LED的数字I/O脚int LED1 = 1; int LED2 = 2; int LED3 = 3; int LED4 = 4; int LED5 = 5; int LED6 = 6; /LED灯花样显示样式1子程序void style_1(void) unsigned char j; for(j=1;j=6;j+) /每隔200ms依次点亮16引脚相连的LED灯 digitalWrite(j,HIGH); /点亮与j引脚相连的LED灯delay(200); /延时200ms for(j=6;j=1;j-) /每隔200ms依次熄灭与61引脚相连的LED灯 digitalWrite(
3、j,LOW); /熄灭与j引脚相连的LED灯delay(200); /延时200ms /灯闪烁子程序void flash(void) unsigned char j,k; for(k=0;k=1;k+) /闪烁两次for(j=1;j=6;j+) /点亮与16引脚相连的LED灯digitalWrite(j,HIGH); /点亮与j引脚相连的LED灯delay(200); /延时200ms for(j=1;j=6;j+) /熄灭与16引脚相连的LED灯digitalWrite(j,LOW); /熄灭与j引脚相连的LED灯delay(200); /延时200ms /LED灯花样显示样式2子程序voi
4、d style_2(void) unsigned char j,k; k=1; /设置k的初值为1 for(j=3;j=1;j-) digitalWrite(j,HIGH); /点亮灯digitalWrite(j+k,HIGH); /点亮灯delay(400); /延时400ms k +=2; /k值加2 k=5; /设置k值为5 for(j=1;j=3;j+) digitalWrite(j,LOW); /熄灭灯digitalWrite(j+k,LOW); /熄灭灯delay(400); /延时400ms k -=2; /k值减2 /LED灯花样显示样式3子程序void style_3(voi
5、d) unsigned char j,k; /LED灯花样显示样式3子程序k=5; /设置k值为5 for(j=1;j=3;j+) digitalWrite(j,HIGH); /点亮灯digitalWrite(j+k,HIGH); /点亮灯delay(400); /延时400ms digitalWrite(j,LOW); /熄灭灯digitalWrite(j+k,LOW); /熄灭灯k -=2; /k值减2 k=3; /设置k值为3 for(j=2;j=1;j-) digitalWrite(j,HIGH); /点亮灯digitalWrite(j+k,HIGH); /点亮灯delay(400);
6、 /延时400ms digitalWrite(j,LOW); /熄灭灯digitalWrite(j+k,LOW); /熄灭灯k +=2; /k值加2 void setup() unsigned char i; for(i=1;i=6;i+) /依次设置16个数字引脚为输出模式pinMode(i,OUTPUT); /设置第i个引脚为输出模式 void loop() style_1(); /样式1 flash(); /闪烁style_2(); /样式2 flash(); /闪烁style_3(); /样式3 flash(); /闪烁第六章 按键开关输入642程序源码int LED = 13; /设
7、置控制LED的数字I/O脚void setup() pinMode(LED,OUTPUT); /设置数字I/O引脚为输出模式 void loop() int i;while(1) i=analogRead(A0); /读取模拟0 口电压值if(i1000) /如果电压值大于1000(即4.88V)digitalWrite(LED,HIGH); /设置第13引脚为高电平,点亮LED灯elsedigitalWrite(LED,LOW); /设置第13引脚为低电平,熄灭LED灯 66拓展实验#define LED 13#define sw 7int val=0;int old_val=0;int s
8、tate=0; /定义状态位void setup()pinMode(LED,OUTPUT);pinMode(sw,INPUT);void loop()val=digitalRead(sw);if(val=HIGH)&(old_val=LOW)state=1-state; /状态位取反delay(10);old_val=val;if(state=1)digitalWrite(LED,HIGH);elsedigitalWrite(LED,LOW); 第七章 触 摸 开 关742程序源码 #define LED 13 /设置控制LED的数字I/O脚 void setup() pinMode(LED,
9、OUTPUT); /设置数字I/O引脚为输出模式 void loop() int i;while(1) i=digitalRead(10); /读取电压值 if(i=LOW) /如果为低电平 digitalWrite(LED,HIGH); /设置第13引脚为高电平,点亮LED灯 else digitalWrite(LED,LOW); /设置第13引脚为低电平,熄灭LED灯 76拓展实验#define LED 13 /设置控制LED的数字I/O脚void setup() pinMode(LED,OUTPUT); /设置数字I/O引脚为输出模式 void loop() int i;digitalW
10、rite(LED,LOW);i=digitalRead(10); /读取电压值if(i=LOW) /如果为低电平while(1) digitalWrite(LED,HIGH); /设置第13引脚为高电平,点亮LED灯 delay(10000); break; 第8章 温度与湿度监测842程序源码#define DHT11_PIN 0byte read_dht11_dat() /读取DHT11数据(每次获取8位)byte i = 0;byte result = 0;for(i=0;i8;i+)while(!(PINC&_BV(DHT11_PIN);delayMicroseconds(30);if
11、(PINC&_BV(DHT11_PIN)result|=(1(7-i);while(PINC&_BV(DHT11_PIN);return result; /返回获取的结果void setup()DDRC|=_BV(DHT11_PIN);PORTC|=_BV(DHT11_PIN);Serial.begin(19200);Serial.println(Ready); /串口输出提示信息void loop()byte dht11_dat5;byte dht11_in;byte i;PORTC &=_BV(DHT11_PIN);delay(18);PORTC|=_BV(DHT11_PIN);delay
12、Microseconds(40);DDRC &=_BV(DHT11_PIN);delayMicroseconds(40);dht11_in = PINC & _BV(DHT11_PIN);if(dht11_in)Serial.println(dht11 start condition 1 not met);return;delayMicroseconds(80);dht11_in=PINC & _BV(DHT11_PIN);if(!dht11_in)Serial.println(dht11 start condition 2 not met);return;delayMicroseconds(
13、80);for(i=0;i5;i+)dht11_dati=read_dht11_dat(); /获取40位温度与湿度数据DDRC|=_BV(DHT11_PIN);PORTC|=_BV(DHT11_PIN);byte dht11_check_sum = dht11_dat0+dht11_dat1+dht11_dat2+dht11_dat3;if(dht11_dat4!=dht11_check_sum)/校验Serial.println(DHT11 checksum error);Serial.print(Current humdity= );Serial.print(dht11_dat0,DEC
14、); /显示湿度整数部分Serial.print(.);Serial.print(dht11_dat1,DEC); /显示湿度小数部分Serial.print(%);Serial.print(temperature = );Serial.print(dht11_dat2,DEC); /显示温度整数部分Serial.print(.);Serial.print(dht11_dat3,DEC); /显示温度小数部分Serial.println(C);86拓展实验#define DHT11_PIN 0int LED=8;int Buzzer=7;byte read_dht11_dat()byte i
15、= 0;byte result = 0;for(i=0;i8;i+)while(!(PINC&_BV(DHT11_PIN);delayMicroseconds(30);if(PINC&_BV(DHT11_PIN)result|=(1(7-i);while(PINC&_BV(DHT11_PIN);return result;void setup()DDRC|=_BV(DHT11_PIN);PORTC|=_BV(DHT11_PIN);pinMode(LED,OUTPUT);pinMode(Buzzer,OUTPUT);Serial.begin(19200);Serial.println(Ready
16、);void loop()byte dht11_dat5;byte dht11_in;byte i;PORTC &=_BV(DHT11_PIN);delay(18);PORTC|=_BV(DHT11_PIN);delayMicroseconds(40);DDRC &=_BV(DHT11_PIN);delayMicroseconds(40);dht11_in = PINC & _BV(DHT11_PIN);if(dht11_in)Serial.println(dht11 start condition 1 not met);return;delayMicroseconds(80);dht11_i
17、n=PINC & _BV(DHT11_PIN);if(!dht11_in)Serial.println(dht11 start condition 2 not met);return;delayMicroseconds(80);for(i=0;i5;i+) /获取40位温度与湿度数据dht11_dati=read_dht11_dat();DDRC|=_BV(DHT11_PIN);PORTC|=_BV(DHT11_PIN);byte dht11_check_sum = dht11_dat0+dht11_dat1+dht11_dat2+dht11_dat3;if(dht11_dat4!=dht11
18、_check_sum) /校验Serial.println(DHT11 checksum error);Serial.print(Current humdity= );Serial.print(dht11_dat0,DEC); /显示湿度整数部分Serial.print(.);Serial.print(dht11_dat1,DEC); /显示湿度小数部分Serial.print(%);Serial.print(temperature = );Serial.print(dht11_dat2,DEC); /显示温度整数部分Serial.print(.);Serial.print(dht11_dat
19、3,DEC); /显示温度小数部分Serial.println(C);if(dht11_dat0=25)digitalWrite(LED,HIGH);elsedigitalWrite(LED,LOW);if(dht11_dat2=28)digitalWrite(Buzzer,HIGH);elsedigitalWrite(Buzzer,LOW);delay(2000);第9章 气 体 监 测942程序源码 void setup()Serial.begin(9600);void loop()int val;val=analogRead(0); /获取当前气体浓度Serial.println(val
20、,DEC); /十进制串口输出delay(1000);第十章 LCD显示104程序设计2) 实验程序LCD显示实验参考程序源代码为: int DI=12;int RW=11; int DB=3,4,5,6,7,8,9,10; /使用数组来定义总线需要的引脚int Enable=2; void LcdCommandWrite(int value) /定义所有引脚int i = 0; for (i=DB0;i = 1; digitalWrite(Enable,LOW); delayMicroseconds(1); digitalWrite(Enable,HIGH); delayMicrosecon
21、ds(1); /延时1ms digitalWrite(Enable,LOW); delayMicroseconds(1); /延时1ms void LcdDataWrite(int value) /定义所有引脚int i = 0; digitalWrite(DI,HIGH); digitalWrite(RW,LOW); for (i=DB0;i = 1; digitalWrite(Enable,LOW); delayMicroseconds(1); digitalWrite(Enable,HIGH); delayMicroseconds(1); digitalWrite(Enable,LOW)
22、; delayMicroseconds(1); /延时1ms void setup (void) int i = 0; for (i=Enable;i= DI;i+) pinMode(i,OUTPUT); delay(100); /短暂的停顿后初始化LCD用于LCD控制需要LcdCommandWrite(0x38); /设置为8位接口,2行显示,57文字大小delay(64); LcdCommandWrite(0x38); /设置为8位接口,2行显示,57文字大小delay(50); LcdCommandWrite(0x38); /设置为8位接口,2行显示,57文字大小delay(20); L
23、cdCommandWrite(0x06); /输入方式设定/自动增量,没有显示移位delay(20); LcdCommandWrite(0x0E); /显示设置/开启显示屏,光标显示,无闪烁delay(20); LcdCommandWrite(0x01); /屏幕清空,光标位置归零delay(100); LcdCommandWrite(0x80); /显示设置/开启显示屏,光标显示,无闪烁delay(20); void loop (void) LcdCommandWrite(0x01); /屏幕清空,光标位置归零delay(10); LcdCommandWrite(0x80+4); delay
24、(10); /写入欢迎信息LcdDataWrite(W); LcdDataWrite(e); LcdDataWrite(l); LcdDataWrite(c); LcdDataWrite(o); LcdDataWrite(m); LcdDataWrite(e); LcdDataWrite(!); delay(10); LcdCommandWrite(0xc0); /定义光标位置为第2行第1个位置delay(10); LcdDataWrite(I); LcdDataWrite( ); LcdDataWrite(L); LcdDataWrite(o); LcdDataWrite(v); LcdDa
25、taWrite(e); LcdDataWrite( ); LcdDataWrite(A); LcdDataWrite(r); LcdDataWrite(d); LcdDataWrite(u); LcdDataWrite(i); LcdDataWrite(n); LcdDataWrite(o); LcdDataWrite(!);delay(5000);第11章 电 机 控 制11.42) 步进电机实验程序#include /这里设置步进电机旋转一圈是多少步#define STEPS 100/attached to设置步进电机的步数和引脚Stepper stepper(STEPS,8,9,10,1
26、1);/定义变量用来存储历史读数int previous = 0;void setup()/设置电机每分钟的转速为90步stepper.setSpeed(90);void loop()int val = analogRead(0);/移动步数为当前读数减去历史读数stepper.step(val-previous);/保存历史读数previous = val;11.6void setup() /初始化数字的引脚为输出pinMode(13,OUTPUT); pinMode(12,OUTPUT); void loop() digitalWrite(13,HIGH);digitalWrite(12,
27、HIGH); delay(50); digitalWrite(13,LOW);digitalWrite(12,LOW); delay(200);第12章 第12章 蓝 牙 通 信124程序设计void setup()Serial.begin(9600);void loop()while(Serial.available()char c=Serial.read();if(c=A)Serial.println(Hello I am amarino);第13章 Wi-Fi无线数据传输134 程序设计void setup()Serial.begin(9600);void loop() Serial.println(Hello World!);delay(1000); 第14章 第14章 ZigBee无线数据传输144程序设计ZigBee无线通信实验参考程序源代码分为主机程序和从机程序两部分。1) 主机程序int button_open =4; /开灯按钮
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1