--- tags: HW(HomeWork) Guide --- # 0520 / 0526 課程 Code + HW(HomeWork) Guide - 注意: 有==黃標==的都是你要寫作業一定要看懂的地方。 ## 電路圖 - 電路圖: ![](https://i.imgur.com/waM8irf.png =75%x) - 實際電路圖: ![](https://i.imgur.com/EZVpfxV.png =75%x) ### 電路解析 - 上述電路圖是一個==接了一個三色 LED (稱為 RGB LED) 的電路==,並利用光敏電阻去對==個別對 R、G、B==的強度做控制。 - 上面的三個光敏電阻,利用光線改變光敏電阻值,但實際上==測得的適接地電阻==的電壓。 - 每一個光敏電阻串聯電阻的電路,測到的都是 R2 的電壓,等效於: ![](https://i.imgur.com/kg9EfIu.png =55%x) - RGB LED = 三個 LED ==放進一個 LED 殼子裡面== - ![](https://i.imgur.com/2uWz5AH.png) - 你可以看到其實 RGB LED 電路圖等價於: - ![](https://i.imgur.com/PxiXzKN.png =35%x) - 上面寫的 `common` 在你們的範例裡面就是接到 GND(0V) 上。 - 而 RGB LED 可以發出不同的顏色光是藉由==調整 R、G、B 三色 LED 的亮度調出來的== - 實物範例: ![](https://i.imgur.com/QtgKCcg.png =55%x) - 光線色相圖: ![](https://i.imgur.com/xVEj19d.png =45%x) ## Code: ```arduino= // // Arduino Starter Kit projects // // Created by Claudio Carnino // Modified by Josh Chang // Copyright © 2021 Futurenest. All rights reserved. // // 數位輸出腳位 enum digitalOutput { LedRed = 11, LedGreen = 10, LedBlue = 9, }; // 類比輸入腳位 enum analogInput { AiRed = A0, AiGreen = A1, AiBlue = A2, }; // 正規化後(0-255)輸出至 PWM 的對應 RGB 數值 int outputRedValue = 0; int outputGreenValue = 0; int outputBlueValue = 0; // 類比輸入的 RGB 大小相對讀值(0-1024) int inputRedValue = 0; int inputGreenValue = 0; int inputBlueValue = 0; void setup() { // 設定串列埠傳輸速率,單位:鮑率(Baud rate) Serial.begin(9600); // 設定數位輸出對應腳位 pinMode(LedRed, OUTPUT); pinMode(LedGreen, OUTPUT); pinMode(LedBlue, OUTPUT); } void loop() { // 透過類比輸入從光敏電阻電路讀取電壓值 inputRedValue = analogRead(AiRed); delay(5); inputGreenValue = analogRead(AiGreen); delay(5); inputBlueValue = analogRead(AiBlue); // 將輸入(0-1024)正規化成 PWM 值(0-255) outputRedValue = inputRedValue / 4; outputGreenValue = inputGreenValue / 4; outputBlueValue = inputBlueValue / 4; // 將 PWM 值輸出至對應 RGB LED 腳位 analogWrite(LedRed, outputRedValue); analogWrite(LedGreen, outputGreenValue); analogWrite(LedBlue, outputBlueValue); // 顯示數值在串列監視器 Serial.print("Input: "); Serial.print("R: "); Serial.print(inputRedValue); Serial.print(", G: "); Serial.print(inputGreenValue); Serial.print(", B: "); Serial.print(inputBlueValue); Serial.print(" # "); Serial.print("Output: "); Serial.print("R: "); Serial.print(outputRedValue); Serial.print(", G: "); Serial.print(outputGreenValue); Serial.print(", B: "); Serial.print(outputBlueValue); Serial.println("\n"); } ``` ### 局部 Code 詳解 - ==等號的意思是 "把右邊的東西給左邊" 喔==,並不是指==左右相等== - `Apple = 1` 是指把 `1` 這個==資料==丟給 `Apple` - 所以不會出現 `1 = Apple` 這種寫法,因為 `1` 是數字,數字==跟變數不一樣,是==不能被分配資料==的 - 觀察以下接腳==來判斷哪一個光敏電阻的 Analog Pin 是對應哪一個 LED== ```C= AiRed = A0, AiGreen = A1, AiBlue = A2, ``` - 以下三個變數讀取了實際的光敏電阻值, `analogRead` 讀取了 `AiRed` 引腳的數值後==丟給了 `inputRedValue` 當作給 LED 的輸入== ```C= inputRedValue = analogRead(AiRed); delay(5); inputGreenValue = analogRead(AiGreen); delay(5); inputBlueValue = analogRead(AiBlue); ``` - 以下三個變數實際地控制了紅、綠、藍的發光程度 ```C= int outputRedValue; int outputGreenValue; int outputBlueValue; ``` - 而上述的==outputValue 則被給了 inputvalue/4 的數值== ```C= outputRedValue = inputRedValue / 4; outputGreenValue = inputGreenValue / 4; outputBlueValue = inputBlueValue / 4; ```