Lesson 3: Names and variables
Halaman ini memerlukan browser terbaru (dengan dukungan SharedArrayBuffer). Silakan perbarui Chrome, Edge, Firefox, atau Safari ke versi terbaru.
English
Give pins a name
const int LED = 13;gives pin 13 a name.constmeans it never changes.- Now the code says what the pin is for. To move the LED to another pin, you change one line.
const int LED = 13;
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(800);
}
English
A variable can change
int wait = 1000;makes a variable: a box with a whole number in it.wait = wait - 100;takes 100 away from it.- Put variables that must remember their value outside
loop(), at the top.
const int LED = 13;
int wait = 1000;
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
digitalWrite(LED, HIGH);
delay(wait);
digitalWrite(LED, LOW);
delay(wait);
if (wait > 100) {
wait = wait - 100; // a little faster each time
}
}
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
The LED is now on pin 7. Use the name LED for the pin (const int LED = 7;) and blink it: 400 ms on, 400 ms off.
Klik Jalankan untuk melihat output di sini.
Kesulitan 3/5 · 200 koin + bonus kecil pada penyelesaian pertama hari ini
Start with int wait = 800;. Blink pin 13 (on for wait, off for wait). After each blink, make wait 200 smaller, but never smaller than 200.
Klik Jalankan untuk melihat output di sini.