Arduino(1) 基本操作

課堂習作(1):LED燈

電路圖
 
程式碼
Arduino範例
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;  //第13個腳位請改成12,因為第13個腳位為測試用腳位可能會有訊號不穩定的狀態
 
// the setup routine runs once when you press reset:
void setup() {     //setcup內的程式是執行一次之後即完成設定不需要以loop的方式重覆執行   
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}
 
// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}
 
 

課堂習作(2):開關

電路圖
程式碼

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  12;      // the number of the LED pin
               //第13個腳位請改成12,因為第13個腳位為測試用腳位可能會有訊號不穩定的狀態
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
 
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  //不要忘了設定pinMode