[Arduino 教程] Blink-小燈閃閃

來源:互聯網
上載者:User

電路
把一個220歐的電阻連到pin 13上. 然後把 LED的長腿(正腿,又稱陽極)連在電阻上。把短腿(負腿,又稱陰極)接地。然後串連Arduino 板和電腦,啟動 Arduino 程式,輸入下面的代碼。
大多數Arduino 開發板的pin 13已經接上了LED。不需要額外的硬體串連,運行程式就可以看到LED閃爍。

點擊圖片放大

圖片用 Fritzing製作. 更多電路範例, 參見Fritzing project page
原理圖
點擊圖片放大

電路
把一個220歐的電阻連到pin 13上. 然後把 LED的長腿(正腿,又稱陽極)連在電阻上。把短腿(負腿,又稱陰極)接地。然後串連Arduino 板和電腦,啟動 Arduino 程式,輸入下面的代碼。
大多數Arduino 開發板的pin 13已經接上了LED。不需要額外的硬體串連,運行程式就可以看到LED閃爍。

點擊圖片放大

圖片用 Fritzing製作. 更多電路範例, 參見Fritzing project page
原理圖
點擊圖片放大

代碼
在以下代碼中,首先你初始化 pin 13 作為輸出
pinMode(13, OUTPUT);
在主迴圈中, 然後開啟LED燈:
digitalWrite(13, HIGH);
這條語句把5 伏的電壓送至 pin 13.導致2個引腳之間產生電壓差從而點亮 LED, 下面用這行代碼關閉LED:
digitalWrite(13, LOW);
 pin 13 迴歸0伏, LED就關了. 在開與關之間,你希望有足夠的時間讓人看到變化,所以 delay()命令讓Arduino 什麼在1000微秒(=1秒)中什麼都不要做。當你使用delay() 命令,這段時間裡面其他事都不會發生。理解了基本執行個體之後,可以從 BlinkWithoutDelay 執行個體中學習如何一邊做別的事一邊製造delay(延遲)。
理解了這個例子以後,從 DigitalReadSerial 執行個體中學習如何把開關的值讀取到 Arduino.
/*
  閃爍
  開啟LED1秒,再關上1秒,如此往複
 
  這個常式可以公開
 */
 
// 大多數Arduino 的Pin 13 上已經有一個 LED
// 起個名字:
int led = 13;

// setup過程只運行一次:
void setup() {               
  // 把這個數字引腳初始化為輸出引腳
  pinMode(led, OUTPUT);    
}

// loop過程永遠迴圈
void loop() {
  digitalWrite(led, HIGH);   // 開啟LED (電壓為HIGH)
  delay(1000);               // 等1秒
  digitalWrite(led, LOW);    // 降低電壓關上LED
  delay(1000);               // 等一秒
}
[取得代碼]
See Also
setup()
loop()
pinMode()
digitalWrite()
delay()
BareMinimum: The bare minimum of code needed to start an Arduino sketch.
Blink: Turn an LED on and off.
DigitalReadSerial: Read a switch, print the state out to the Arduino Serial Monitor.
AnalogReadSerial: Read a potentiometer, print it's state out to the Arduino Serial Monitor.
Fade: Demonstrates the use of analog output to fade an LED.
ReadAnalogVoltage : Reads an analog input and prints the voltage to the serial monitor

電路
把一個220歐的電阻連到pin 13上. 然後把 LED的長腿(正腿,又稱陽極)連在電阻上。把短腿(負腿,又稱陰極)接地。然後串連Arduino 板和電腦,啟動 Arduino 程式,輸入下面的代碼。
大多數Arduino 開發板的pin 13已經接上了LED。不需要額外的硬體串連,運行程式就可以看到LED閃爍。

點擊圖片放大

圖片用 Fritzing製作. 更多電路範例, 參見Fritzing project page
原理圖
點擊圖片放大

代碼
在以下代碼中,首先你初始化 pin 13 作為輸出
pinMode(13, OUTPUT);
在主迴圈中, 然後開啟LED燈:
digitalWrite(13, HIGH);
這條語句把5 伏的電壓送至 pin 13.導致2個引腳之間產生電壓差從而點亮 LED, 下面用這行代碼關閉LED:
digitalWrite(13, LOW);
 pin 13 迴歸0伏, LED就關了. 在開與關之間,你希望有足夠的時間讓人看到變化,所以 delay()命令讓Arduino 什麼在1000微秒(=1秒)中什麼都不要做。當你使用delay() 命令,這段時間裡面其他事都不會發生。理解了基本執行個體之後,可以從 BlinkWithoutDelay 執行個體中學習如何一邊做別的事一邊製造delay(延遲)。
理解了這個例子以後,從 DigitalReadSerial 執行個體中學習如何把開關的值讀取到 Arduino.
/*
  閃爍
  開啟LED1秒,再關上1秒,如此往複
 
  這個常式可以公開
 */
 
// 大多數Arduino 的Pin 13 上已經有一個 LED
// 起個名字:
int led = 13;

// setup過程只運行一次:
void setup() {               
  // 把這個數字引腳初始化為輸出引腳
  pinMode(led, OUTPUT);    
}

// loop過程永遠迴圈
void loop() {
  digitalWrite(led, HIGH);   // 開啟LED (電壓為HIGH)
  delay(1000);               // 等1秒
  digitalWrite(led, LOW);    // 降低電壓關上LED
  delay(1000);               // 等一秒
}
[取得代碼]
See Also
setup()
loop()
pinMode()
digitalWrite()
delay()
BareMinimum: The bare minimum of code needed to start an Arduino sketch.
Blink: Turn an LED on and off.
DigitalReadSerial: Read a switch, print the state out to the Arduino Serial Monitor.
AnalogReadSerial: Read a potentiometer, print it's state out to the Arduino Serial Monitor.
Fade: Demonstrates the use of analog output to fade an LED.
ReadAnalogVoltage : Reads an analog input and prints the voltage to the serial monitor

代碼
在以下代碼中,首先你初始化 pin 13 作為輸出
pinMode(13, OUTPUT);
在主迴圈中, 然後開啟LED燈:
digitalWrite(13, HIGH);
這條語句把5 伏的電壓送至 pin 13.導致2個引腳之間產生電壓差從而點亮 LED, 下面用這行代碼關閉LED:
digitalWrite(13, LOW);
 pin 13 迴歸0伏, LED就關了. 在開與關之間,你希望有足夠的時間讓人看到變化,所以 delay()命令讓Arduino 什麼在1000微秒(=1秒)中什麼都不要做。當你使用delay() 命令,這段時間裡面其他事都不會發生。理解了基本執行個體之後,可以從 BlinkWithoutDelay 執行個體中學習如何一邊做別的事一邊製造delay(延遲)。
理解了這個例子以後,從 DigitalReadSerial 執行個體中學習如何把開關的值讀取到 Arduino.
/*
  閃爍
  開啟LED1秒,再關上1秒,如此往複
 
  這個常式可以公開
 */
 
// 大多數Arduino 的Pin 13 上已經有一個 LED
// 起個名字:
int led = 13;

// setup過程只運行一次:
void setup() {               
  // 把這個數字引腳初始化為輸出引腳
  pinMode(led, OUTPUT);    
}

// loop過程永遠迴圈
void loop() {
  digitalWrite(led, HIGH);   // 開啟LED (電壓為HIGH)
  delay(1000);               // 等1秒
  digitalWrite(led, LOW);    // 降低電壓關上LED
  delay(1000);               // 等一秒
}
[取得代碼]
See Also
setup()
loop()
pinMode()
digitalWrite()
delay()
BareMinimum: The bare minimum of code needed to start an Arduino sketch.
Blink: Turn an LED on and off.
DigitalReadSerial: Read a switch, print the state out to the Arduino Serial Monitor.
AnalogReadSerial: Read a potentiometer, print it's state out to the Arduino Serial Monitor.
Fade: Demonstrates the use of analog output to fade an LED.
ReadAnalogVoltage : Reads an analog input and prints the voltage to the serial monitor

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.