Untitled

可變電阻控制LEDvariableRegistanceLed
mail@pastecode.io avatar
unknown
plain_text
2 years ago
724 B
0
Indexable
Never
//https://blog.jmaker.com.tw/arduino-tutorials-4/
//variableRegistanceLed.ino
int sensorPin = A0;    //類比輸入的PIN腳 電阻中間腳位->A0
int ledPin = D4;      // 類比輸出的PIN腳,接到LED的正極 LED正極->D4
int sensorValue = 0;  // 類比輸入得到的數值

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  //LED的PIN腳設為輸出
}

void loop() {
  sensorValue = analogRead(sensorPin);   //讀取類比輸入的值會得到0~1023
  Serial.println(sensorValue);
  sensorValue = map(sensorValue,0,1023,0,255);  //將0~1023轉化成0~255
  analogWrite(ledPin,sensorValue);  //將結果用PWM的方式輸出給LED,改變亮度
  Serial.println(sensorValue);
}