1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #define RED_PIN 15 #define GREEN_PIN 2 #define BLUE_PIN 13 #define PWM_FREQ 5000 #define PWM_RES 8 #define RED_CH 0 #define GREEN_CH 1 #define BLUE_CH 2 void setup() { Serial.begin(115200); // 初始化 PWM 通道 ledcSetup(RED_CH, PWM_FREQ, PWM_RES); ledcSetup(GREEN_CH, PWM_FREQ, PWM_RES); ledcSetup(BLUE_CH, PWM_FREQ, PWM_RES); // 綁定 GPIO 到 PWM 通道 ledcAttachPin(RED_PIN, RED_CH); ledcAttachPin(GREEN_PIN, GREEN_CH); ledcAttachPin(BLUE_PIN, BLUE_CH); } void loop() { // 顏色變化循環(共陰模組:高亮度 → 0) setColor(255, 0, 0); // 紅 delay(1000); setColor(0, 255, 0); // 綠 delay(1000); setColor(0, 0, 255); // 藍 delay(1000); setColor(255, 255, 0); // 黃 delay(1000); setColor(0, 255, 255); // 青 delay(1000); setColor(255, 0, 255); // 紫 delay(1000); setColor(255, 255, 255); // 白(全亮) delay(1000); setColor(0, 0, 0); // 關燈(黑) delay(1000); } // 設定顏色亮度(0~255),共陰 LED:數值越大亮度越低 void setColor(uint8_t r, uint8_t g, uint8_t b) { // 對共陰 RGB LED,需要將亮度反轉(0 為全亮,255 為關閉) ledcWrite(RED_CH, 255 - r); ledcWrite(GREEN_CH, 255 - g); ledcWrite(BLUE_CH, 255 - b); } |
Direct link: https://paste.plurk.com/show/6zCeTrL1zHjfz0cN0z3T