Lesson 4: for loops
이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
Do it several times
- A
forloop repeats lines a fixed number of times. for (int i = 0; i < 3; i++)countsi= 0, 1, 2. That is 3 times.
void setup() {
pinMode(13, OUTPUT);
for (int i = 0; i < 3; i++) { // 3 quick blinks
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
}
void loop() {
}
- The blinks are in
setup(), so they happen once.loop()is empty, so then the board does nothing.
English
A row of LEDs
- The loop variable can be a pin number.
void setup() {
for (int pin = 2; pin <= 5; pin++) {
pinMode(pin, OUTPUT);
}
}
void loop() {
for (int pin = 2; pin <= 5; pin++) {
digitalWrite(pin, HIGH);
delay(150);
digitalWrite(pin, LOW);
}
}
- Press Run: a light runs along pins 2, 3, 4, 5.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Blink pin 13 exactly 5 times (300 ms on, 300 ms off), then stop with the LED off. Use a for loop.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Switch on pins 2, 3, 4, 5 and 6 one after the other, 200 ms apart, so the bar grows. Then switch them all off and start again. Use for loops.
출력을 보려면 '실행'을 클릭하세요.