Lesson 3: Names and variables
Cette page nécessite un navigateur récent (avec support SharedArrayBuffer). Veuillez mettre à jour Chrome, Edge, Firefox ou Safari vers la dernière version.
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
}
}
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
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.
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 3/5 · 200 pièces + un petit bonus lors de la première réussite aujourd'hui
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.
Cliquez sur Exécuter pour voir le résultat ici.