2016年4月1日 星期五

Arduino的教學14-RGB led燈(共陰極)的使用

一、教學目標:
學習控制 rgb(紅、綠、藍)的三色led燈。

二、接線圖

RGB LED依序為、最長腳、
RGB LED的共陰極(最長腳)串接一個330的電阻後再接地(GND)。

RGB LED的R接Arduino腳位11。
RGB LED的G接Arduino腳位10。
RGB LED的B接Arduino腳位9。

三、程式碼
1.每隔一秒改變LED的顏色,紅、綠、藍、黃、青、洋紅、白



#define LEDR 9
#define LEDG 10
#define LEDB 11
 int timelimit=100;
// 關閉LED的函式
void turnoff(){
  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, LOW);
  digitalWrite(LEDB, LOW);
}
 
void setup() {                
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  turnoff();
}
 
// 每隔一秒改變LED的顏色
void loop() {
  turnoff();
  digitalWrite(LEDR, HIGH);
  delay(500);
 digitalWrite(LEDR, LOW);
 delay(500);
} 

// 根據接線圖定義控制R、G、B的腳位
#define LEDR 9
#define LEDG 10
#define LEDB 11

// 關閉LED的函式
void turnoff(){
  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, LOW);
  digitalWrite(LEDB, LOW);
}

void setup() {                
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  turnoff();
}

// 每隔一秒改變LED的顏色
void loop() {
  turnoff();
  digitalWrite(LEDR, HIGH);
  delay(1000);
  
  turnoff();
  digitalWrite(LEDG, HIGH);
  delay(1000);
  
  turnoff();
  digitalWrite(LEDB, HIGH);
  delay(1000);
  
  turnoff();
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, HIGH);
  delay(1000);
  
  turnoff();
  digitalWrite(LEDG, HIGH);
  digitalWrite(LEDB, HIGH);
  delay(1000);
  
  turnoff();
  digitalWrite(LEDB, HIGH);
  digitalWrite(LEDR, HIGH);
  delay(1000);


  turnoff();
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDB, HIGH);
  digitalWrite(LEDG, HIGH);
  delay(1000);

} 

2.利用pwm輸出,每一秒隨機出現顏色

 
#define LEDR 11
#define LEDG 10
#define LEDB 9

void setup() {                
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
}

int r = 0;
int g = 0;
int b = 0;
void loop() {
  r = random(0, 255);
  g = random(0, 255);
  b = random(0, 255);
  analogWrite(LEDR, r);
  analogWrite(LEDG, g);
  analogWrite(LEDB, b);
  delay(1000);
}
wemos用
// 根據接線圖定義控制R、G、B的腳位
#define LEDR D4
#define LEDG D3
#define LEDB D2
int t = 5;
// 關閉LED的函式
void turnoff() {
  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, LOW);
  digitalWrite(LEDB, LOW);
}

void setup() {
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  turnoff();
}

// 每隔一秒改變LED的顏色
void loop() {
  //  turnoff();
playOne();

}
//依序亮一個燈
void playOne() {
  digitalWrite(LEDR, HIGH);
  delay(t);
  digitalWrite(LEDR, LOW);

  delay(t);
  digitalWrite(LEDG, HIGH);
  delay(t);
  digitalWrite(LEDG, LOW);
  delay(t);


  digitalWrite(LEDB, HIGH);
  delay(t);
  digitalWrite(LEDB, LOW);
  delay(t);
}
//依序亮二個燈
void playTwo() {
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, HIGH);
  delay(t);

  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, LOW);
  delay(t);

  digitalWrite(LEDB, HIGH);
  digitalWrite(LEDG, HIGH);
  delay(t);

  digitalWrite(LEDB, LOW);
  digitalWrite(LEDG, LOW);
  delay(t);


  digitalWrite(LEDB, HIGH);
  digitalWrite(LEDR, HIGH);
  delay(t);
  digitalWrite(LEDB, LOW);
  digitalWrite(LEDR, LOW);
  delay(t);
}

四、作業 1.請加入一個可變電阻,可改變其delay的時間。 五、參考資源 http://yehnan.blogspot.tw/2013/01/arduinorgb-led.html http://forum.arduino.cc/index.php/topic,8320.0.html http://howtomechatronics.com/tutorials/arduino/how-to-use-a-rgb-led-with-arduino/