玩转ARDUINO有图版.docx

上传人:b****5 文档编号:3424696 上传时间:2022-11-22 格式:DOCX 页数:98 大小:1.62MB
下载 相关 举报
玩转ARDUINO有图版.docx_第1页
第1页 / 共98页
玩转ARDUINO有图版.docx_第2页
第2页 / 共98页
玩转ARDUINO有图版.docx_第3页
第3页 / 共98页
玩转ARDUINO有图版.docx_第4页
第4页 / 共98页
玩转ARDUINO有图版.docx_第5页
第5页 / 共98页
点击查看更多>>
下载资源
资源描述

玩转ARDUINO有图版.docx

《玩转ARDUINO有图版.docx》由会员分享,可在线阅读,更多相关《玩转ARDUINO有图版.docx(98页珍藏版)》请在冰豆网上搜索。

玩转ARDUINO有图版.docx

玩转ARDUINO有图版

玩转ARDUINO

项目一

点亮你的LED

材料清单

●ARDUINO

●LED

●330Ω电阻

代码

/*

Blink

TurnsonanLEDonforonesecond,thenoffforonesecond,repeatedly.

*/

//Pin13hasanLEDconnectedonmostArduinoboards.

//giveitaname:

intled=13;

//thesetuproutinerunsoncewhenyoupressreset:

voidsetup(){

//initializethedigitalpinasanoutput.

pinMode(led,OUTPUT);

}

//thelooproutinerunsoverandoveragainforever:

voidloop(){

digitalWrite(led,HIGH);//turntheLEDon(HIGHisthevoltagelevel)

delay(1000);//waitforasecond

digitalWrite(led,LOW);//turntheLEDoffbymakingthevoltageLOW

delay(1000);//waitforasecond

}

线路图

项目二

读取数字信号

材料清单

●ARDUINO

●按键

●10KΩ电阻

●面包板

●面包线(杜邦线)

代码

/*

DigitalReadSerial

Readsadigitalinputonpin2,printstheresulttotheserialmonitor

*/

//digitalpin2hasapushbuttonattachedtoit.Giveitaname:

intpushButton=2;

//thesetuproutinerunsoncewhenyoupressreset:

voidsetup(){

//initializeserialcommunicationat9600bitspersecond:

Serial.begin(9600);

//makethepushbutton'spinaninput:

pinMode(pushButton,INPUT);

}

//thelooproutinerunsoverandoveragainforever:

voidloop(){

//readtheinputpin:

intbuttonState=digitalRead(pushButton);

//printoutthestateofthebutton:

Serial.println(buttonState);

delay

(1);//delayinbetweenreadsforstability

}

线路图

项目三

读取模拟信号

材料清单

●ARDUINO

●10kΩ电位计

●杜邦线

代码

/*

AnalogReadSerial

Readsananaloginputonpin0,printstheresulttotheserialmonitor.

Attachthecenterpinofapotentiometer(电位计)topinA0,andtheoutsidepinsto+5Vandground.

*/

//thesetuproutinerunsoncewhenyoupressreset:

voidsetup(){

//initializeserialcommunicationat9600bitspersecond:

Serial.begin(9600);

}

//thelooproutinerunsoverandoveragainforever:

voidloop(){

//readtheinputonanalogpin0:

intsensorValue=analogRead(A0);

//printoutthevalueyouread:

Serial.println(sensorValue);

delay

(1);//delayinbetweenreadsforstability

}

线路图

项目四

呼吸灯

材料清单

●ARDUINO

●LED

●220Ω电阻

●面包板

代码

/*

Fade

ThisexampleshowshowtofadeanLEDonpin9

usingtheanalogWrite()function.

*/

intled=9;//thepinthattheLEDisattachedto

intbrightness=0;//howbrighttheLEDis

intfadeAmount=5;//howmanypointstofadetheLEDby

//thesetuproutinerunsoncewhenyoupressreset:

voidsetup(){

//declarepin9tobeanoutput:

pinMode(led,OUTPUT);

}

//thelooproutinerunsoverandoveragainforever:

voidloop(){

//setthebrightnessofpin9:

analogWrite(led,brightness);

//changethebrightnessfornexttimethroughtheloop:

//控制占空比DUTYCYCLE/DUTYRATIO

brightness=brightness+fadeAmount;

//reversethedirectionofthefadingattheendsofthefade:

if(brightness==0||brightness==255){

fadeAmount=-fadeAmount;

}

//waitfor30millisecondstoseethedimmingeffect

delay(30);

}

补充

pulsewidthmodulation(PWM)

脉冲调宽

线路图

项目五

读取模拟信号口的电压

材料清单

●ARDUINO

●10KΩ电位计

代码

/*

ReadAnalogVoltage

Readsananaloginputonpin0,convertsittovoltage,andprintstheresulttotheserialmonitor.

AttachthecenterpinofapotentiometertopinA0,andtheoutsidepinsto+5Vandground.

*/

//thesetuproutinerunsoncewhenyoupressreset:

voidsetup(){

//initializeserialcommunicationat9600bitspersecond:

Serial.begin(9600);

}

//thelooproutinerunsoverandoveragainforever:

voidloop(){

//readtheinputonanalogpin0:

intsensorValue=analogRead(A0);

//Converttheanalogreading(whichgoesfrom0-1023)toavoltage(0-5V):

floatvoltage=sensorValue*(5.0/1023.0);

//printoutthevalueyouread:

Serial.println(voltage);

}

线路图

项目六

Millis()

材料清单

●ARDUINO

●LED

●220Ω电阻

●面包线

代码

/*BlinkwithoutDelay

Turnsonandoffalightemittingdiode(LED)connectedtoadigitalpin,withoutusingthedelay()function.ThismeansthatothercodecanrunatthesametimewithoutbeinginterruptedbytheLEDcode.

Thecircuit:

*LEDattachedfrompin13toground.

*Note:

onmostArduinos,thereisalreadyanLEDontheboardthat'sattachedtopin13,sonohardwareisneededforthisexample.

*/

//constantswon'tchange.Usedhereto

//setpinnumbers:

constintledPin=13;//thenumberoftheLEDpin

//Variableswillchange:

intledState=LOW;//ledStateusedtosettheLED

longpreviousMillis=0;//willstorelasttimeLEDwasupdated

//thefollowvariablesisalongbecausethetime,measuredinmiliseconds,

//willquicklybecomeabiggernumberthancanbestoredinanint.

longinterval=1000;//interval(间隔)atwhichtoblink(milliseconds)

voidsetup(){

//setthedigitalpinasoutput:

pinMode(ledPin,OUTPUT);

}

voidloop()

{

//hereiswhereyou'dputcodethatneedstoberunningallthetime.

//checktoseeifit'stimetoblinktheLED;thatis,ifthe

//differencebetweenthecurrenttimeandlasttimeyoublinked

//theLEDisbiggerthantheintervalatwhichyouwantto

//blinktheLED.

unsignedlongcurrentMillis=millis();

if(currentMillis-previousMillis>interval){

//savethelasttimeyoublinkedtheLED

previousMillis=currentMillis;

//iftheLEDisoffturnitonandvice-versa:

if(ledState==LOW)

ledState=HIGH;

else

ledState=LOW;

//settheLEDwiththeledStateofthevariable:

digitalWrite(ledPin,ledState);

}

}

线路图

项目七

用按键点亮LED

材料清单

●ARDUINO

●按键

●10kΩ电阻

●面包板

●面包线

代码

/*

Button

Turnsonandoffalightemittingdiode(LED)connectedtodigital

pin13,whenpressingapushbuttonattachedtopin2.

Thecircuit:

*LEDattachedfrompin13toground

*pushbuttonattachedtopin2from+5V

*10Kresistorattachedtopin2fromground

*Note:

onmostArduinosthereisalreadyanLEDontheboard

attachedtopin13.

*/

//constants常量,这里指的是const语句won'tchange.They'reused

//hereto

//setpinnumbers:

constintbuttonPin=2;//thenumberofthepushbuttonpin

constintledPin=13;//thenumberoftheLEDpin

//variableswillchange:

intbuttonState=0;//variableforreadingthepushbuttonstatus

voidsetup(){

//initializetheLEDpinasanoutput:

pinMode(ledPin,OUTPUT);

//initializethepushbuttonpinasaninput:

pinMode(buttonPin,INPUT);

}

voidloop(){

//readthestateofthepushbuttonvalue:

buttonState=digitalRead(buttonPin);

//checkifthepushbuttonispressed.

//ifitis,thebuttonStateisHIGH:

if(buttonState==HIGH){

//turnLEDon:

digitalWrite(ledPin,HIGH);

}

else{

//turnLEDoff:

digitalWrite(ledPin,LOW);

}

}

线路图

项目八

电灯开关

材料清单

●ARDUINO

●按键

●10KΩ电阻

代码

/*

Debounce中文:

防反跳

EachtimetheinputpingoesfromLOWtoHIGH(e.g.becauseofapush-buttonpress),theoutputpinistoggled切换fromLOWtoHIGHorHIGHtoLOW.There'saminimumdelaybetweentogglestodebouncethecircuit(i.e.toignorenoise).

Thecircuit:

*LEDattachedfrompin13toground

*pushbuttonattachedfrompin2to+5V

*10Kresistorattachedfrompin2toground

*Note:

OnmostArduinoboards,thereisalreadyanLEDontheboardconnectedtopin13,soyoudon'tneedanyextracomponentsforthisexample.

*/

//constantswon'tchange.They'reusedhereto

//setpinnumbers:

constintbuttonPin=2;//thenumberofthepushbuttonpin

constintledPin=13;//thenumberoftheLEDpin

//Variableswillchange:

intledState=HIGH;//thecurrentstateoftheoutputpin

intbuttonState;//thecurrentreadingfromtheinputpin

intlastButtonState=LOW;//thepreviousreadingfromtheinputpin

//thefollowingvariablesarelong'sbecausethetime,measuredinmiliseconds,

//willquicklybecomeabiggernumberthancanbestoredinanint.

longlastDebounceTime=0;//thelasttimetheoutputpinwastoggled

longdebounceDelay=50;//thedebouncetime;increaseiftheoutputflickers

voidsetup(){

pinMode(buttonPin,INPUT);

pinMode(ledPin,OUTPUT);

//setinitialLEDstate

digitalWrite(ledPin,ledState);

}

voidloop(){

//readthestateoftheswitchintoalocalvariable:

intreading=digitalRead(buttonPin);

//checktoseeifyoujustpressedthebutton

//(i.e.theinputwentfromLOWtoHIGH),andyou'vewaited

//longenoughsincethelastpresstoignoreanynoise:

//Iftheswitchchanged,duetonoiseorpressing:

if(reading!

=lastButtonState){

//resetthedebouncingtimer

lastDebounceTime=millis();

}

if((millis()-lastDebounceTime)>debounceDelay){

//whateverthereadingisat,it'sbeenthereforlonger

//thanthedebouncedelay,sotakeitastheactualcurrentstate:

//ifthebuttonstatehaschanged:

if(reading!

=buttonState){

buttonState=reading;

//onlytoggletheLEDifthenewbuttonstateisHIGH

if(buttonState==HIGH){

ledState=!

ledState;

}

}

}

//settheLED:

digitalWrite(ledPin,ledState);

//savethereading.Nexttimethroughtheloop,

//it'llbethelastButtonState:

lastButtonState=reading;

}

线路图

项目九

按三下按键点亮LED

材料清单

●ARDUINO

●按键

●10KΩ电阻

代码

/*

Statechangedetection(edgedetection边缘检测)

Often,youdon'tneedtoknowthestateofadigitalinputallthetime,butyoujustneedtoknowwhentheinputchangesfromonestatetoanother.

Forexample,youwanttoknowwhenabuttongoesfromOFFtoON.Thisiscalledstatechangedetection,oredgedetection.

Thisexampleshowshowtodetectwhenabuttonorbuttonchangesfromofftoonandontooff.

Thecircuit:

*pushbuttonattachedtopin2from+5V

*10K

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 学科竞赛

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1