第一個 Arduino 程式- Blink

材料
Arduino 板 *1
LED *1

接線
Arduino Blink

注意LED長短腳有分,像這樣接到PIN13 ,還有接地,不過LED這樣接是有燒壞的風險,也可以再加一個電阻。

然後使用Arduino內建的blink程式
  1. /*  
  2.   Blink  
  3.   Turns on an LED on for one second, then off for one second, repeatedly.  
  4.    
  5.   This example code is in the public domain.  
  6.  */  
  7.     
  8. // Pin 13 has an LED connected on most Arduino boards.   
  9. // give it a name:   
  10. int led = 13;   
  11.   
  12. // the setup routine runs once when you press reset:   
  13. void setup() {                   
  14.   // initialize the digital pin as an output.   
  15.   pinMode(led, OUTPUT);        
  16. }   
  17.   
  18. // the loop routine runs over and over again forever:   
  19. void loop() {   
  20.   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)   
  21.   delay(1000);               // wait for a second   
  22.   digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW   
  23.   delay(1000);               // wait for a second   
  24. }  

大致解說,要先設定接的pin腳,設定pin mode是output,然後再loop裡寫要不斷執行的程式,這段程式的意思是每隔一秒就明、滅、明、滅。

多多習慣,之後就會很順手了。

留言