////////////////////////////////////////////////////////////////////////////////
// /*
// 基本元件: RGB LED
// 功能 : 依序亮紅綠藍各1秒
// */
// #define redPin 15
// #define greenPin 2
// #define bluePin 4
// void setup() {
// pinMode(redPin, OUTPUT);
// pinMode(greenPin, OUTPUT);
// pinMode(bluePin, OUTPUT);
// }
// void loop() {
// setColor(255, 0, 0); delay(1000);
// setColor(0, 255, 0); delay(1000);
// setColor(0, 0, 255); delay(1000);
// }
// void setColor(uint8_t redValue, uint8_t greenValue, uint8_t blueValue){
// analogWrite(redPin, redValue);
// analogWrite(greenPin, greenValue);
// analogWrite(bluePin, blueValue);
// }
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// /*
// 基本元件: RGB LED
// 功能 : 依色碼表亮出指定色彩
// */
// #define redPin 15
// #define greenPin 2
// #define bluePin 4
// void setup() {
// pinMode(redPin, OUTPUT);
// pinMode(greenPin, OUTPUT);
// pinMode(bluePin, OUTPUT);
// }
// // 指定色彩(orange) #FFA500
// void loop() {
// setColor(0xFF, 0xA5, 0x00);
// delay(10);
// }
// void setColor(uint8_t redValue, uint8_t greenValue, uint8_t blueValue){
// analogWrite(redPin, redValue);
// analogWrite(greenPin, greenValue);
// analogWrite(bluePin, blueValue);
// }
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// /*
// 基本元件: RGB LED
// 功能 : 彩虹霓虹燈
// */
// #define redPin 15
// #define greenPin 2
// #define bluePin 4
// void setup(){
// pinMode(redPin, OUTPUT); // R
// pinMode(greenPin, OUTPUT); // G
// pinMode(bluePin, OUTPUT); // B
// }
// // 彩虹 #FF0000 #FF6600 #FFFF00 #00FF00 #0000FF #00FFFF #FF00FF
// void loop(){
// setColor(0xFF, 0x00, 0x00);delay(1000);
// setColor(0xFF, 0x66, 0x00);delay(1000);
// setColor(0xFF, 0xFF, 0x00);delay(1000);
// setColor(0x00, 0xFF, 0x00);delay(1000);
// setColor(0x00, 0x00, 0xFF);delay(1000);
// setColor(0x00, 0xFF, 0xFF);delay(1000);
// setColor(0xFF, 0x00, 0xFF);delay(1000);
// }
// void setColor(uint8_t redValue, uint8_t greenValue, uint8_t blueValue){
// analogWrite(redPin, redValue);
// analogWrite(greenPin, greenValue);
// analogWrite(bluePin, blueValue);
// }
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// /*
// 基本元件: RGB LED
// 功能 : 紅色呼吸燈
// */
// #define redPin 15
// #define greenPin 2
// #define bluePin 4
// #define step 1
// #define speed 10
// void setup(){
// pinMode(redPin, OUTPUT); // R
// pinMode(greenPin, OUTPUT); // G
// pinMode(bluePin, OUTPUT); // B
// }
// void loop(){
// for(int i=0; i<256; i=i+step){
// setColor(i, 0, 0);
// delay(speed);
// }
// for(int i=255; i>=0; i-=step){
// setColor(i, 0, 0);
// delay(speed);
// }
// }
// void setColor(uint8_t redValue, uint8_t greenValue, uint8_t blueValue){
// analogWrite(redPin, redValue);
// analogWrite(greenPin, greenValue);
// analogWrite(bluePin, blueValue);
// }
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// /*
// 基本元件: RGB LED
// 功能 : 各色呼吸燈
// */
// #define redPin 15
// #define greenPin 2
// #define bluePin 4
// #define step 1
// #define speed 5
// void setup() {
// pinMode(redPin, OUTPUT);
// pinMode(greenPin, OUTPUT);
// pinMode(bluePin, OUTPUT);
// }
// void loop() {
// // 紅逐步增加 綠255 藍0
// for(int i=0; i<256; i+=step){
// setColor(i, 255, 0);
// delay(speed);
// }
// // 紅255 綠逐步減少 藍0
// for(int i=255; i>=0; i-=step){
// setColor(255, i, 0);
// delay(speed);
// }
// // 紅255 綠0 藍逐步增加
// for(int i=0; i<256; i+=step){
// setColor(255, 0, i);
// delay(speed);
// }
// // 紅255 綠逐步增加 藍255
// for(int i=0; i<256; i+=step){
// setColor(255, i, 255);
// delay(speed);
// }
// // 紅逐步減少 綠255 藍255
// for(int i=255; i>=0; i-=step){
// setColor(i, 255, 255);
// delay(speed);
// }
// // 紅0 綠255 藍逐步減少
// for(int i=255; i>=0; i-=step){
// setColor(0, 255, i);
// delay(speed);
// }
// // 紅逐步增加 綠255 藍0
// for(int i=0; i<256; i+=step){
// setColor(i, 255, 0);
// delay(speed);
// }
// // 紅255 綠逐步減少 藍0
// for(int i=255; i>=0; i-=step){
// setColor(255, i, 0);
// delay(speed);
// }
// // 紅255 綠0 藍逐步增加
// for(int i=0; i<256; i+=step){
// setColor(255, 0, i);
// delay(speed);
// }
// // 紅255 綠逐步增加 藍255
// for(int i=0; i<256; i+=step){
// setColor(255, i, 255);
// delay(speed);
// }
// // 紅逐步減少 綠255 藍255
// for(int i=255; i>=0; i-=step){
// setColor(i, 255, 255);
// delay(speed);
// }
// // 紅0 綠255 藍逐步減少
// for(int i=255; i>=0; i-=step){
// setColor(0, 255, i);
// delay(speed);
// }
// }
// void setColor(uint8_t redValue, uint8_t greenValue, uint8_t blueValue){
// analogWrite(redPin, redValue);
// analogWrite(greenPin, greenValue);
// analogWrite(bluePin, blueValue);
// }
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
基本元件: RGB LED
功能 : 共陰極、共陽極條件編譯
*/
#define redPin 15
#define greenPin 2
#define bluePin 4
// 共陰極、共陽極設定
#define hardwareConfig 0 //COMMON_ANODE=1 COMMON_CATHODE=0
uint8_t T(uint8_t value){
// 條件編譯
#if hardwareConfig
// 共陽極
return ~value;
#else
// 共陰極
return value;
#endif
}
void setup(){
pinMode(redPin, OUTPUT); // R
pinMode(greenPin, OUTPUT); // G
pinMode(bluePin, OUTPUT); // B
}
// 彩虹 #FF0000 #FF6600 #FFFF00 #00FF00 #0000FF #00FFFF #FF00FF
void loop(){
setColor(0xFF, 0x00, 0x00);delay(1000);
setColor(0xFF, 0x66, 0x00);delay(1000);
setColor(0xFF, 0xFF, 0x00);delay(1000);
setColor(0x00, 0xFF, 0x00);delay(1000);
setColor(0x00, 0x00, 0xFF);delay(1000);
setColor(0x00, 0xFF, 0xFF);delay(1000);
setColor(0xFF, 0x00, 0xFF);delay(1000);
}
void setColor(uint8_t redVal, uint8_t greenVal, uint8_t blueVal){
analogWrite(redPin, T(redVal));
analogWrite(greenPin, T(greenVal));
analogWrite(bluePin, T(blueVal));
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// /*
// 基本元件: RGB LED
// 功能 : 共陰極、共陽極條件編譯
// */
// #define redPin 15
// #define greenPin 2
// #define bluePin 4
// // 共陰極、共陽極設定
// #define hardwareConfig 0 //COMMON_ANODE=1 COMMON_CATHODE=0
// // 條件編譯
// #if hardwareConfig==0
// // 共陰極
// void setColor(unsigned char redVal, unsigned char greenVal, unsigned char blueVal){
// analogWrite(redPin, redVal);
// analogWrite(greenPin, greenVal);
// analogWrite(bluePin, blueVal);
// }
// #else
// // 共陽極
// void setColor(unsigned char redVal, unsigned char greenVal, unsigned char blueVal){
// analogWrite(redPin, (unsigned char)~redVal);
// analogWrite(greenPin, (unsigned char)~greenVal);
// analogWrite(bluePin, (unsigned char)~blueVal);
// }
// #endif
// void setup(){
// pinMode(redPin, OUTPUT); // R
// pinMode(greenPin, OUTPUT); // G
// pinMode(bluePin, OUTPUT); // B
// }
// void loop(){
// setColor(255, 0, 0); delay(1000);
// setColor(0, 255, 0); delay(1000);
// setColor(0, 0, 255); delay(1000);
// }
////////////////////////////////////////////////////////////////////////////////