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;}设定小灯I
史上最全的arduino实验集锦
最新推荐文章于 2024-06-10 16:58:37 发布