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
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}
 
void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
 
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}
+開關switch case
 

課堂習作(3):倒水燈

思考模式
兩組開關控制兩個LED燈,兩組開關要分別能控制兩個LED燈,要考慮LED燈會做到漸亮漸暗所以會用到PWM腳位,為了達到倒水燈的效果,所以一個開關要控制兩個LED燈
 
電路圖
程式碼
//第一組開關以及LED燈 
const int buttonPin_1 = 3;    
const int ledPin_1 =  10;  
               //第13個腳位請改成12,因為第13個腳位為測試用腳位可能會有訊號不穩定的狀態
//第二組開關以及LED燈
const int buttonPin_2 = 4;
const int ledPin_2 = 11;
 
int buttonState_1 = 0;     
int buttonState_2 = 0;
 
void setup() {
  //不要忘了設定pinMode
  pinMode(ledPin_1, OUTPUT);  
  pinMode(ledPin_2, OUTPUT);     
  pinMode(buttonPin_1, INPUT);  
  pinMode(buttonPin_2, INPUT);     
}
 
void loop(){
  buttonState_1 = digitalRead(buttonPin_1);
  buttonState_2 = digitalRead(buttonPin_2);
  //一個開關控制兩個LED燈  
  if (buttonState_1 == 1) {      
    digitalWrite(ledPin_1, HIGH); 
    digitalWrite(ledPin_2, LOW); 
  } 
  if (buttonState_2 == 1){
    digitalWrite(ledPin_1, LOW); 
    digitalWrite(ledPin_2, HIGH);
  }
}
 
 
 
應用switch case的寫法
//Arduino
//switch case 應用範例
//設定變數
int buttonPin_1 = 3;  
int ledPin_1 = 11;
 
int buttonPin_2 = 4;
int ledPin_2 = 10;
 
int pinRead_1;
int pinRead_2;
 
int range;
 
//設定pin的輸入與輸出
void setup() {
  pinMode(ledPin_1, OUTPUT); 
  pinMode(buttonPin_1, INPUT); 
  pinMode(ledPin_2, OUTPUT); 
  pinMode(buttonPin_2, INPUT);
}
 
void loop() {
pinRead_1 = digitalRead(buttonPin_1);
pinRead_2 = digitalRead(buttonPin_2);
if(pinRead_1==0&&pinRead_2==1){
  range=1; //執行switch case中的 case 0
}
if(pinRead_1==1&&pinRead_2==0){
  range=0; //執行switch case中的 case 1
}
switch (range) {  
 
//switch case的做法是讓程式去選擇底下要執行的區塊,區塊的內容為case到break之間
case 0: 
  digitalWrite(ledPin_1,LOW);
  digitalWrite(ledPin_2,HIGH);
  break;
  
case 1: 
  digitalWrite(ledPin_1,HIGH);
  digitalWrite(ledPin_2,LOW);
  break;
  }
}
 
漸亮漸暗
Arduino範例
/*
 Fading
 This example shows how to fade an LED using the analogWrite() function.
 The circuit:
 * LED attached from digital pin 9 to ground.
 Created 1 Nov 2008
 By David A. Mellis
 modified 30 Aug 2011
 By Tom Igoe
 This example code is in the public domain.
 */
 
int ledPin = 9;    // LED connected to digital pin 9
 
void setup()  { 
  // nothing happens in setup 
 
void loop()  { 
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    //fadeValue = fadeValue + 5;
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);  
    //將第9個腳位當成類比輸出PWM~       
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
 
  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
}
 
倒水燈完整程式碼
int buttonPin_1 = 3; 
int ledPin_1 = 10;
int buttonPin_2 = 4;
int ledPin_2 = 11;
int pinRead_1;
int pinRead_2;
int range;
int brightness;
int brightness1;
int brightness2;
int delayDuration=30;
 
void setup() {
  pinMode(ledPin_1, OUTPUT); 
  pinMode(buttonPin_1, INPUT); 
  pinMode(ledPin_2, OUTPUT); 
  pinMode(buttonPin_2, INPUT);
}
 
void loop() {
pinRead_1 = digitalRead(buttonPin_1);
pinRead_2 = digitalRead(buttonPin_2);
if(pinRead_1==0 && pinRead_2==1){
  range=0;
}
if(pinRead_1==1 && pinRead_2==0){
  range=1;
}
if(pinRead_1==1 && pinRead_2==1){
  range=2;
}
if(pinRead_1==0 && pinRead_2==0){
  range=3;
}
switch (range) {  
 
//switch case的做法是讓程式去選擇底下要執行的區塊,區塊的內容為case到break之間
case 0: 
  analogWrite(ledPin_1,brightness1);
  brightness1 = brightness1 - 5 ;
  if (brightness1 <= 0) {
    brightness1 = 0 ; 
  } 
  delay(delayDuration);
  analogWrite(ledPin_2,brightness2);
  brightness2 = brightness2 + 5 ;
  if (brightness2 >= 255) {
    brightness2 = 255 ; 
  } 
  delay(delayDuration);
  break;
  
case 1: 
  analogWrite(ledPin_1,brightness1);
  brightness1 = brightness1 + 5 ;
  if (brightness1 >= 255) {
    brightness1 = 255 ; 
  } 
  delay(delayDuration);
  analogWrite(ledPin_2,brightness2);
  brightness2 = brightness2 - 5 ;
  if (brightness2 <= 0) {
    brightness2 = 0 ; 
  } 
  delay(delayDuration);
  break;
  
  case 2: 
  if (brightness2 != 0 ){
    analogWrite(ledPin_1,brightness1=0);
  }
  else {
    analogWrite(ledPin_1,brightness1=255);
  }
  break;
  
  case 3: 
  analogWrite(ledPin_1,brightness);
  analogWrite(ledPin_2,brightness);
  brightness = brightness - 5 ;
  if (brightness <= 0) {
    brightness = 0 ; 
  } 
  delay(delayDuration);
  break;
  }
}
 
 
倒水燈(亞東技術學院:水流版本)
程式碼
short ledU=3,ledD=5,ledM=11,GSenserU=7,GSenserD=8;
short MAX_BRI=170;
boolean b,gu,gd;
int bri;
 
void setup() {
  pinMode(ledU, OUTPUT);
  pinMode(ledD, OUTPUT);
  pinMode(GSenserU,INPUT);
  pinMode(GSenserD,INPUT);
  digitalWrite(ledD, HIGH);
}
 
void loop() {
  gu = digitalRead(GSenserU);
  gd = digitalRead(GSenserD);
  if(gu && !gd){
    bri+=2;
    ledPwm();
  }
  else if(!gu && gd){
    bri-=2;
    ledPwm();
  }
  delay(6);
}
void ledPwm(){
  if(bri>MAX_BRI) bri=MAX_BRI;
  if(bri<0) bri=0;
  if(bri!=MAX_BRI && bri!=0 && bri%3==0)
    analogWrite(ledM, 30);
  else
    digitalWrite(ledM, LOW);
 
  analogWrite(ledU, bri);
  analogWrite(ledD, MAX_BRI-bri);
}
 
 光敏電阻 程式碼
 
 


int photocellPin = 2; // 光敏電阻 (photocell) 接在 anallog pin 2
int photocellVal = 0; // photocell variable
int minLight = 200;   // 最小光線門檻值
int ledPin = 9;
int ledState = 0; 

void setup() {
  pinMode(ledPin, OUTPUT); 
  Serial.begin(9600);
}

void loop() {
  // 讀取光敏電阻並輸出到 Serial Port 
  photocellVal = analogRead(photocellPin);
  Serial.println(photocellVal);   
  
  // 光線不足時打開 LED
  if (photocellVal < minLight && ledState == 0) {
    digitalWrite(ledPin, HIGH); // turn on LED
    ledState = 1;
  }
  
  // 光線充足時關掉 LED
  if (photocellVal > minLight && ledState == 1) {
    digitalWrite(ledPin, LOW); // turn off LED
    ledState = 0;
  }  
  
  delay(100);       
}

 課堂習作(4):可調式電阻 呼吸燈

程式碼

int lighting = 0;
int Led_01 = 3;
int val = 0;

void setup()  {
  pinMode(Led_01, OUTPUT);
}

void loop()  {
  lighting = analogRead(0);
  val = map(lighting, 0, 1023, 0, 255);
  analogWrite(3, val); 
}

課堂習作(5):伺服馬達操作

SG-90 伺服馬達
棕:GND
紅:4.8v-6v
橙:PWM 輸入
價位:NTD95
 
電路圖
 
Arduino範例程式
 
// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott
 
#include <Servo.h> 
 
Servo myservo;
// create servo object to control a servo 
 
int potpin = 0;  
// analog pin used to connect the potentiometer
int val;    
// variable to read the value from the analog pin 
 
void setup() 
  myservo.attach(9);  
  // attaches the servo on pin 9 to the servo object 
 
void loop() 
  val = analogRead(potpin);            
  // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);    
  //map 前面兩組數字到後面兩組範圍內 
  // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  
  // sets the servo position according to the scaled value 
  delay(15);                           
  // waits for the servo to get there 
 
 

課堂習作(6):距離感測器


const int trig = 2;
const int echo = 4;
const int inter_time = 500;
int time = 0;

void setup() {
  Serial.begin(9600);
  pinMode (trig, OUTPUT);
  pinMode (echo, INPUT);
}

void loop() {
  float duration, distance;
  digitalWrite(trig, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig, LOW);
  duration = pulseIn (echo, HIGH);
  distance = (duration/2)/29;
  
  Serial.print("Data:");
  Serial.print (time/1000);
  Serial.print(", d = ");
  Serial.print(distance);
  Serial.println(" cm");
  time = time + inter_time;
  delay(inter_time);
}


課堂習作(7):距離感測器+伺服馬達


#include <Servo.h> 
 
Servo myservo;


const int trig = 2;
const int echo = 4;
const int inter_time = 500;
int time = 0;
int val;  

void setup() {
  Serial.begin(9600);
  pinMode (trig, OUTPUT);
  pinMode (echo, INPUT);
  myservo.attach(9);
}

void loop() {
  float duration, distance;
  digitalWrite(trig, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig, LOW);
  duration = pulseIn (echo, HIGH);
  distance = (duration/2)/29;
  
  val = map(distance, 0, 10, 0, 179);
  myservo.write(val);
  
  Serial.print("Data:");
  Serial.print (time/1000);
  Serial.print(", d = ");
  Serial.print(distance);
  Serial.println(" cm");
  time = time + inter_time;
  delay(inter_time);
}

Reference

Arduino Libraries
 
杜邦頭
 
 
 
 
COSCUP2014發表內容
 
2014年人機互動概論課程
 
 
 
解決訊號無法傳輸問題
avrdude stk500_getsync() not in sync resp=0x30