/////////////////////////////////////////////////////////////////////////////////////////////
/*
電子元件:可變電阻及伺服馬達
功能:使用滑動電位器控制伺服馬達
*/
#include <ESP32Servo.h>
Servo myServo;
#define servoPin 25
#define potPin 35
void setup() {
// 設置伺服馬達的控制腳位
myServo.attach(servoPin);
// 設置可變電阻腳位模式為輸入
pinMode(potPin, INPUT);
}
void loop(){
// 從可變電阻讀取類比訊號(0~4095)
int val = analogRead(potPin);
// 使用map函數將(0, 4095)映射至(0, 180)
val = map(val, 0, 4095, 0, 180);
// 控制伺服馬達
myServo.write(val);
delay(100);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// /*
// 電子元件:可變電阻及RGB LED
// 功能:調光氣氛燈(使用旋鈕可變電阻控制RGB LED)
// */
// // RGB LED 接腳
// const int LedRedPin = 23;
// const int LedGreenPin = 22;
// const int LedBluePin = 21;
// // 可變電阻(電位器) 接腳
// const int PotRedPin = 15;
// const int PotGreenPin = 2;
// const int PotBluePin = 4;
// void setup() {
// Serial.begin(115200);
// pinMode(PotRedPin, INPUT);
// pinMode(PotGreenPin, INPUT);
// pinMode(PotBluePin, INPUT);
// pinMode(LedRedPin, OUTPUT);
// pinMode(LedGreenPin, OUTPUT);
// pinMode(LedBluePin, OUTPUT);
// }
// void loop() {
// int valueR = analogRead(PotRedPin);
// int valueG = analogRead(PotGreenPin);
// int valueB = analogRead(PotBluePin);
// valueR = map(valueR, 0, 4095, 0, 255);
// valueG = map(valueG, 0, 4095, 0, 255);
// valueB = map(valueB, 0, 4095, 0, 255);
// analogWrite(LedRedPin, valueR);
// analogWrite(LedGreenPin, valueG);
// analogWrite(LedBluePin, valueB);
// delay(100);
// }
/////////////////////////////////////////////////////////////////////////////////////////////