史上最全的arduino实验集锦

Ardinuo知识点与项目实战
(1)简易流水灯void setup()
{ int i=10; for(i;i<=13;i++)
pinMode(i,OUTPUT);//循环一下而已}
void loop() { digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(200);
digitalWrite(11,HIGH);
digitalWrite(12,LOW);
delay(200);
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
delay(200);
digitalWrite(13,HIGH);
digitalWrite(10,LOW);
delay(200);}
(2)按键控制小灯的开关const int LED=13; //声明变量 LED灯接在13针脚;
const int BUTTON=7; //声明变量,按钮接在第7针脚;
int val=0; //声明变量,用来存储按钮返回值HIGH或者LOW;
int val_old=0;//声明变量,存储上一次按钮返回值;比对val和val_old的值,来判断按钮是按下还是抬起 //valLOW && val_oldHIGHT 抬起状态;
valHIGH && val_oldLOW 按下状态;
int state=0;//0 off,1 on 声明变量,起到中转的作用,小技巧;
void setup() //初始化;{ pinMode(LED,OUTPUT); //定义LED灯输入模式;
pinMode(BUTTON,INPUT);//定义BUTTON为输出模式;
} void loop() { val=digitalRead(BUTTON);//读取数字针脚的值;
if((valHIGH) && (val_oldLOW))//比较前一次和现在的按钮返回的值, //只有现在按下按钮这个状态下,才改变state的值
{ state=1-state; } if((valLOW && val_oldHIGH)) //去抖的作用,一般10-15ms;
{ delay(10); } val_old=val;//把现在按钮返回的值赋予val_old;
if(state==1)//控制灯开关;
{ digitalWrite(LED,HIGH); }
else { digitalWrite(LED,LOW); } }
第二种方法
int buttonState = 0;
void setup() {pinMode(13, OUTPUT);
pinMode(2, INPUT);}
void loop(){buttonState = digitalRead(2);
if (buttonState == HIGH) {digitalWrite(13, HIGH);}
else {digitalWrite(13, LOW);}
蜂鸣器制作电子琴
int buzzer=8;int buzzer2=9;int buzzer3=10;
void setup(){pinMode(buzzer,INPUT);
pinMode(buzzer2,INPUT);
pinMode(buzzer3,INPUT);
pinMode(3,OUTPUT);
digitalWrite(buzzer,HIGH);
digitalWrite(buzzer2,HIGH);
digitalWrite(buzzer3,HIGH);}
void loop(){ if(digitalRead(buzzer)==LOW)
{ tone(3,2349);
delay(100);
noTone(3);}
else if(digitalRead(buzzer2)==LOW)
{ tone(3,2093); delay(100);
noTone(3);}
else if(digitalRead(buzzer3)==LOW)
{ tone(3,2637);
delay(100);
noTone(3);}
else noTone(3);}
数字控制呼吸灯
void setup() { pinMode(11,OUTPUT);}
void loop()
{for(int i=0;i<=30;i++)
{analogWrite(11,i);
delay(10);}
for(int i=30;i>=0;i–)
{analogWrite(11,i);delay(10); }}
呼吸流水灯
void setup() { pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);}
int level =0;
int ports[6]={3,5,6,9,10,11}
;int idx=0;
void loop()
{delay(10);
analogWrite(ports[idx],level);
++level %=7;++idx %=6;}设定小灯IO数字管脚为5,闪烁
int ledPin = 5;//设定控制LED的数字IO管脚
void setup() {pinMode(ledPin,OUTPUT);}//设定数字IO口的模式,output为输出
void loop(){digitalWrite(ledPin,HIGH);//设定PIN5脚为HIGH=5v左右
delay(200);digitalWrite(ledPin,LOW);//设定PIN5脚为LOW=0v
delay(200);}
利用串口输入字符控制小灯闪烁
int ledPin = 13;//设定控制LED的数字IO管脚
int val;void setup()
{pinMode(ledPin,OUTPUT);//设定数字IO口的模式,
output为输出Serial.begin(9600);}//设定波特率为9600
void loop(){val=Serial.read();
if(-1!=val){if('H’val)
{digitalWrite(ledPin,HIGH);//设定PIN5脚为HIGH=5v左右
delay(500);
digitalWrite(ledPin,LOW);//设定PIN5脚为LOW=0v
Serial.print(“Available:”);
Serial.println(Serial.available(),DEC);//返回串口缓冲区中当前剩下的字符个数。最多能够缓冲128个字节。 }}}
(四)旋转按钮模拟输出控制小灯的亮与暗
int ledPin =3;int analogPin =A5;//一个5v,中间A5,一个GND
int val= 0;
void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(9600);}
void loop(){val = analogRead(analogPin);
analogWrite(ledPin, );
Serial.println(val);delay(200);}
(五)打印hello world
void setup()
{Serial.begin(9600);}
void loop(){Serial.println(“Hello world”);delay(1000);}
(六)数码管流动数字制作
int pinDigitron = 2; //管脚D2连接到数码管的A脚,D3连B… D9连h
void setup() { for(int x=0; x<8; x++)
pinMode(pinDigitron+x, OUTPUT); //设置各脚为输出状态
} //
在数码管中显示数字的函数
void displayDigit(unsigned char digit)
{ //定义一个数组表:不同数字的abcdefgh各段的取值
unsigned char abcdefgh[][8] = {
{1,1,1,1,1,1,0,0}, //0
{0,1,1,0,0,0,0,0}, //1
{1,1,0,1,1,0,1,0}, //2
{1,1,1,1,0,0,1,0}, //3
{0,1,1,0,0,1,1,0}, //4
{1,0,1,1,0,1,1,0}, //5
{1,0,1,1,1,1,1,0}, //6
{1,1,1,0,0,0,0,0}, //7
{1,1,1,1,1,1,1,0}, //8
{1,1,1,1,0,1,1,0}, //9
{1,1,1,0,1,1,1,0}, //A
{0,0,1,1,1,1,1,0}, //b
{1,0,0,1,1,1,0,0}, //C
{0,1,1,1,1,0,1,0}, //d
{1,0,0,1,1,1,1,0}, //E
{1,0,0,0,1,1,1,0}, //F };
if ( digit >= 16 ) return;
for (unsigned char x=0; x<8; x++)
digitalWrite( pinDigitron + x, abcdefgh[digit][x] );}
void loop() { //在数码管中显示0-9,A-F各个数字
for (int x=0; x<16; x++) {
displayDigit(x); //调用displayDigit()子函数,显示数字
delay(1000); //等待1000毫秒
}}
(七)蜂鸣器唱小星星
int d=262;//1
int re=294;//
2int mi=330;//3
int fa=349;//4
int sol=392;//5
int la=440;//6
int si=494;//7
int buzzerPin=7;
int ol[43]={d,d,sol,sol,la,la,sol, fa,fa,mi,mi,re,re,d, sol,sol,fa,fa,mi,mi,re, sol,sol,fa,fa,mi,mi,re, d,d,sol,sol,la,la,sol, fa,fa,mi,mi,re,re,d};
void setup()
{ pinMode(buzzerPin,OUTPUT); }
void loop(){
for(int i=0;i<=42;i++)
{ tone(buzzerPin,ol[i]);
delay(100);
noTone(buzzerPin);
delay(100);} }
(七)使用alm35制作温度计平面对人,左脚为正极,右脚负极,中间为模拟输入管脚
void setup(){
Serial.begin(9600); }
void loop(){
Serial.println(analogRead(0)0.48828125);//输出为摄氏度:5/10241000/10
delay(100); }
(八)光敏电阻一脚接5v,一脚负极并需与普通电阻相接,中间插模拟输出管脚
void setup(){
Serial.begin(9600); }
void loop()
{ Serial.println(analogRead(0)); delay(100); }
(九)反馈装置,基本功能同上,只是加了一个小灯
void setup()
{ Serial.begin(9600);
pinMode(9,OUTPUT); }
void loop(){
analogWrite(9,map(analogRead(0),400,700,0,255));//map 映射
Serial.println(analogRead(0));
delay(100); }
(十)红外热人体测距 中间插管脚,一边插gnd
void setup(){ pinMode(11,OUTPUT);
pinMode(7,INPUT); }
void loop(){ if(digitalRead(7)HIGH)
digitalWrite(11,HIGH);
else digitalWrite(11,LOW); }
(十一)检测按钮
int pushButton =7;
int buttonState=0;
int beforeState =0;
void setup(){ Serial.begin(9600);
pinMode(pushButton,INPUT); }
void loop(){
buttonState=digitalRead(pushButton);
if(buttonState
1 and beforeState
1)
{ Serial.print(“hi~~”);
Serial.print(buttonState); }
beforeState=buttonState; delay(1);}
(十二)电子琴加呼吸灯加电位器
int a=0;
int analogPin =A5;
int b=0;
int buzzer=7;
int buzzer2=2;
int buzzer3=8;
int buzzer4=4;
int buzzer5=12;
void setup()
{pinMode(5,OUTPUT);//LED1
pinMode(6,OUTPUT);//LED2
pinMode(9,OUTPUT);//LED3
pinMode(10,OUTPUT);//LED4
pinMode(11,OUTPUT);//LED5
pinMode(buzzer,INPUT);
pinMode(buzzer2,INPUT);
pinMode(buzzer3,INPUT);
pinMode(buzzer4,INPUT);
pinMode(buzzer5,INPUT);
pinMode(3,OUTPUT);
digitalWrite(buzzer,HIGH);
digitalWrite(buzzer2,HIGH);
digitalWrite(buzzer3,HIGH);
digitalWrite(buzzer4,HIGH);
digitalWrite(buzzer5,HIGH);
digitalWrite(11,LOW);//LED1
digitalWrite(5,LOW);//LED2
digitalWrite(6,LOW);//LED3
digitalWrite(9,LOW);//LED4
digitalWrite(10,LOW);//LED5}
void loop(){
b = analogRead(analogPin);
if(digitalRead(buzzer)LOW)
{ tone(3,262);delay(100);noTone(3);
for(int i=a;i<=b;i++) {analogWrite(5,i);
delay(10);}
for(int i=b;i>=a;i–) {analogWrite(5,i); delay(10);} } else if(digitalRead(buzzer2)LOW) { tone(3,294);delay(100);noTone(3); {for(int i=a;i<=b;i++) {analogWrite(6,i); delay(10);}} for(int i=b;i>=a;i–) {analogWrite(6,i); delay(10);} } else if(digitalRead(buzzer3)LOW) { tone(3,330);delay(100);noTone(3); {for(int i=a;i<=b;i++) {analogWrite(9,i); delay(10);}} for(int i=b;i>=a;i–) {analogWrite(9,i); delay(10);} } else if(digitalRead(buzzer4)LOW) { tone(3,349); delay(100);noTone(3); {for(int i=a;i<=b;i++) {analogWrite(10,i); delay(10);}} for(int i=b;i>=a;i–) {analogWrite(10,i); delay(10);} } else if(digitalRead(buzzer5)LOW) { tone(3,392);delay(100);noTone(3); {for(int i=a;i<=b;i++) {analogWrite(11,i); delay(10);} for(int i=b;i>=a;i–) {analogWrite(11,i); delay(10);} } } else noTone(3);}
(十三)电位器控制流水灯与电子琴
int analogPin =A5;
int ledpin[]={5,6,9,10,11};
int i=5;
void setup(){for(int i=7;i>=3;i–){pinMode(ledpin[i],OUTPUT);
digitalWrite(ledpin[i],HIGH);}//make the led from
pinMode(3,OUTPUT);}
void loop(){
int a=analogRead(analogPin);
int ledlevel=map(a,0,1023,0,i);
for(int b=0;b {if(b {digitalWrite(ledpin[b],HIGH);}
else
{digitalWrite(ledpin[b],LOW);} }
int c=map(a,0,1023,0,5);
if(c
1)
{ tone(3,262);delay(100);noTone(3);}
else if(c
2){tone(3,294);delay(100);noTone(3); }
else if(c
3) {tone(3,330);delay(100);noTone(3); }
else if(c
4) {tone(3,349);delay(100);noTone(3); }
else if(c
5) {tone(3,392);delay(100);noTone(3); }
else noTone(3);}
(十四)超声波测距
const int TrigPin = 2;
const int EchoPin = 3;
float cm;
void setup() {Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(8,OUTPUT);}
void loop() {digitalWrite(8, LOW);
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(10);
digitalWrite(TrigPin, HIGH)
;delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm = (int(cm * 100.0)) / 100.0; //保留两位小数
Serial.println( pulseIn(EchoPin, HIGH) / 58.0);
if (cm>=2 && cm<=10)tone(8, HIGH);delay(100);noTone(8);}
(十五)
#include//
LiquidCrystal lcd(1,2,4,5,6,7);//RS,e,寄存器D4,D5,D6,D7
void setup(){lcd.begin(16,2);//定义lcd的尺寸,2行16格
}void loop(){lcd.print(“arduino”) ;
delay(3000);lcd.setCursor(2,1);//(列,行)
lcd.print(“LCD Tutorial”);
delay(3000);
lcd.clear();
lcd.blink();//设置闪烁光标
delay(4000);l
cd.setCursor(7,1);
delay(3000)
;lcd.noBlink();
lcd.cursor();//设置为下划线的闪烁光标
lcd.noCursor()
;lcd.clear();}1.gnd2.vcc //5v3.vo
可以加一个电位器来控制显示器的对比度4.rs 选择是否将发送命令或数据到液晶显示器,引脚放在低电位或0v则可以发送命令到液晶显示屏将光标位置放在特定位置,清屏等。高电位或5v,就向LCD发送数据或字符5.r/w 选择模式6.e 写入寄存器,或接下来的数据引脚从D0到D77.D0到D7 date pins|0|0|0|0|0|0|0|,发送这些ascall码到寄存器,有特定表参照 8.a(正极)9.k(负极背光)(十六)lcd闪烁指标#include//byte slash[8]={B00001,B00010,B00100,B01000,B10000,B00000,B00000,B00000 };
LiquidCrystal lcd(1,2,4,5,6,7)
;void setup(){lcd.begin(16,2);//定义lcd的尺寸,2行16格
lcd.createChar(7,slash);}void loop(){for(int i=0;i<=15;i++) {lcd.setCursor(i,0);lcd.clear();lcd.write(7);delay(1000);}}
(十七)火焰探测器,ao口接模拟接口
int fame=A0;int Beep=8;int val=0;
void setup()
{pinMode(fame,INPUT);
Serial.begin(9600);}
void loop(){val=analogRead(fame);
Serial.println(val); delay(500); }
(十八)土壤湿度传感器
#define Moisture A0 //定义AO 引脚 为 IO-A0
#define DO 7 //定义DO 引脚 为 IO-7
void setup() { pinMode(Moisture, INPUT);//定义A0为输入模式
pinMode(DO, INPUT);
Serial.begin(9600);}
void loop() { //串口返回测量数据
Serial.print(“Moisture=”);
Serial.print(analogRead(Moisture));//读取AO的数值
Serial.print("|DO=");
Serial.println(digitalRead(DO));//读取DO的数值
delay(1000);}
(十九)烟雾传感器
const int gasSensor =0;
void setup(){ Serial.begin(9600); // sets the serial port to 9600
}void loop(){ float voltage;
voltage = getVoltage(gasSensor);
Serial.println(voltage);
delay(1000);}
float getVoltage(int pin){
return (analogRead(pin) * 0.004882814);}
(二十)红外传感器
int LEDpin = 2
;int light = 3;
void setup()
{pinMode(LEDpin,OUTPUT);
pinMode(light,INPUT);
Serial.begin(9600);}
void loop(){
int zhongbest = digitalRead(light);
Serial.println(zhongbest);
if (zhongbest == 0)
{digitalWrite(LEDpin,HIGH)
;delay(1000);
digitalWrite(LEDpin,LOW);}
else{digitalWrite(LEDpin,LOW);}}
(二十一)红外遥控小灯简易版
#include
int RECV_PIN = 11
;int LED_PIN = 13;
IRrecv irrecv(RECV_PIN);//创建实例
decode_results results;//解码
void setup(){ Serial.begin(9600);
irrecv.enableIRIn(); // 开始接收红外线
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);}
void loop() {
if (irrecv.decode(&results))
{//如果有接收到信息
Serial.println(results.value, HEX);//16进制输出,BIH就是2进制
if (results.value == 0xFFA25D) //开灯的值
{ digitalWrite(LED_PIN, LOW); }
else if (results.value == 0xFF629D) //关灯的值
{
digitalWrite(LED_PIN, HIGH); }
irrecv.resume(); //接收下一个信息 } delay(100);}
(二十二)舵机
#include
Servo myservo;
int pos=0;
void setup()
{myservo.attach(9);
}void loop(){
for(pos=0;pos<=180;pos++)
{myservo.write(pos);delay(5);}
for(pos=180;pos>=0;pos–) {myservo.write(pos);delay(5);}}
(二十三)声控灯
int val;void setup() {
Serial.begin(9600);
pinMode(8,OUTPUT);
digitalWrite(8,LOW);}
void loop(){
val = analogRead(A0);
Serial.println(val);
digitalWrite(8,HIGH);
if (val > 80) // no voice
{ digitalWrite(8,HIGH); // clear led
delay(3000); }
else { digitalWrite(8,LOW); // set led }}
(二十四)

你可能感兴趣的:(实验)