Lesson 8: Brightness with PWM
Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
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.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Make the LED on pin 9 about a quarter as bright as full: analogWrite with 64.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Make a dimmer: read the knob on A0 and set the brightness of the LED on pin 9 with map và analogWrite.
Nhấn Chạy để xem kết quả ở đây.