1 / 17

Arduino week2

Arduino week2. 語法: Variables & Functions 實作: Digital out ( Button+LED ) 簡報:廖崇義. pinMode (). pinMode (pin , mode) pin 欲指定的腳位 數位腳位 D0~D13 m ode ( INPUT ,  OUTPUT , or  INPUT_PULLUP ) INPUT 將腳位設定為輸入使用 OUTPUT 將 腳位設定為 輸出使用 INPUT_PULLUP 將 腳位設定為輸入 使用,並且使用內部提升電阻 (see next page)

orly
Download Presentation

Arduino week2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arduino week2 語法:Variables & Functions 實作:Digital out (Button+LED) 簡報:廖崇義

  2. pinMode() • pinMode(pin, mode) • pin 欲指定的腳位 數位腳位 D0~D13 • mode (INPUT, OUTPUT, or INPUT_PULLUP) INPUT 將腳位設定為輸入使用 OUTPUT 將腳位設定為輸出使用 INPUT_PULLUP將腳位設定為輸入使用,並且使用內部提升電阻(see next page) EX1. pinMode ( 13 , OUTPUT ) EX2. intledpin = 13 ; pinMode( ledpin, OUTPUT )

  3. INPUT_PULLUP • PULLUP內部提升電阻 (深紅色部份) EX.pinMode(10, INPUT_PULLUP) 當SWOFF時D10為HIGH 當SWON時D10為LOW 實作1.

  4. digitalRead() • digitalRead(pin) • pin D0~D13 • 回傳值HIGH或LOW • 必需配合變數讀取回傳值,變數可為boolean或int形態 • booleantrue or false(1、0或HIGH 、LOW) • INT -32,768 to 32,767 ,使用digitalRead()回傳值為HIGH時為1 , LOW時為0

  5. digitalWrite() • digitalWrite(pin, value) • pin D0~D13 • value為HIGH或LOW • I/O最大限制40mA 實作2.

  6. digitalWrite() • 實作3. • 輸出腳改到pin10,並使用300Ω限流電阻

  7. delay(ms) delayMicroseconds(us) • delay(ms) • ms :設定程式欲暫停執行的時間,單位為millisecond (0.001秒) • delayMicroseconds(us) • us: 設定程式欲暫停執行的時間,單位為microsecond (0.001ms) Ex.

  8. 變數型態

  9. millis() • 回傳Arduino晶片的時間計數值 • 按Reset或程式重載時歸零 • 回傳值為unsigned long型別 • 約50天後溢位歸零重數 • Ex. unsigned long timex ; timex =millis() ;

  10. If判斷式 • if (conditional)//判斷式 { // do something here } 判斷成立執行大括號內程式碼 • 判段式運算符 ==, !=, <, > • ==ex.if( val== HIGH ) • != ex.if( val!=HIGH ) • < ex.if( val< 5 ) • > ex.if( val>5 )

  11. 按鍵計數實作4-1

  12. 按鍵計數去抖動part1實作4-2 intledPin = 13; //LED intbuttonPin = 10; //buttonPin intval = 0; //按鍵計數值 booleanled_status =1;//LED狀態變數 booleanbtn_status;//按鍵狀態 void setup() { pinMode(ledPin,OUTPUT); pinMode(buttonPin,INPUT_PULLUP); } void loop() { btn_status=digitalRead(buttonPin);//讀取按鍵狀態 if(btn_status == LOW)//當按鍵被按下時 { delay(500);//暫停0.5秒避開抖動雜訊 val=val+1;//計數加1 if(val == 3)//當按下button3次時LED燈動作 { digitalWrite(ledPin,led_status);//LED輸出變換 val=0;//計數歸零 led_status = !led_status;//下次LED輸出狀態反相 } } }

  13. 按鍵計數去抖動part2實作5 • 取樣分析(加分題) 10~100次 ∞次 10~100次 5V 0V 雜訊區 >100ms 密集區 雜訊區 >100ms

  14. 7段顯示器(顯示0~9程式碼)實作6

  15. 顯示0~9程式碼實作6 10 6 8 7 9 1 4 2 3 10912467

  16. 顯示0~9程式碼實作6 void loop() { digitalWrite( 2 , seven_disp[i][0] ) ; digitalWrite( 3 , seven_disp[i][1] ) ; digitalWrite( 4 , seven_disp[i][2] ) ; digitalWrite( 5 , seven_disp[i][3] ) ; digitalWrite( 6 , seven_disp[i][4] ) ; digitalWrite( 7 , seven_disp[i][5] ) ; digitalWrite( 8 , seven_disp[i][6] ) ; delay(500); i=i+1; if(i==10) i=0; } byte seven_disp[10][7] = { { 1,1,1,1,1,1,0 }, // = 0 { 0,1,1,0,0,0,0 }, // = 1 { 1,1,0,1,1,0,1 }, // = 2 { 1,1,1,1,0,0,1 }, // = 3 { 0,1,1,0,0,1,1 }, // = 4 { 1,0,1,1,0,1,1 }, // = 5 { 1,0,1,1,1,1,1 }, // = 6 { 1,1,1,0,0,0,0 }, // = 7 { 1,1,1,1,1,1,1 }, // = 8 { 1,1,1,0,0,1,1 } // = 9 }; inti=0; void setup() { pinMode( 2, OUTPUT ); pinMode( 3, OUTPUT ); pinMode( 4, OUTPUT ); pinMode( 5, OUTPUT ); pinMode( 6, OUTPUT ); pinMode( 7, OUTPUT ); pinMode( 8, OUTPUT ); }

  17. 按鍵加一顯示0~9實作7 • 加分題 • 承上題七段顯示器,按一次按鍵數字加1

More Related