Lesson 8: Brightness with PWM
이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
Half on
analogWrite(9, 128);makes pin 9 switch on and off very fast: on about half of the time.- This is called PWM. An LED looks half as bright.
- The number goes from 0 (always off) to 255 (always on).
- Only pins with a ~ on the board can do this: 3, 5, 6, 9, 10 and 11.
void setup() {
pinMode(9, OUTPUT);
}
void loop() {
for (int b = 0; b <= 255; b = b + 5) { // slowly brighter
analogWrite(9, b);
delay(20);
}
for (int b = 255; b >= 0; b = b - 5) { // slowly darker
analogWrite(9, b);
delay(20);
}
}
English
From a knob to brightness
analogReadgives 0–1023, butanalogWritewants 0–255.map(value, 0, 1023, 0, 255)changes one range into the other.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Make the LED on pin 9 about a quarter as bright as full: analogWrite with 64.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Make a dimmer: read the knob on A0 and set the brightness of the LED on pin 9 with map 및 analogWrite.
출력을 보려면 '실행'을 클릭하세요.