Lesson 3: Names and variables
تحتاج هذه الصفحة إلى متصفح حديث (يدعم SharedArrayBuffer). يرجى تحديث Chrome أو Edge أو Firefox أو Safari إلى أحدث إصدار.
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
}
}
English
In the exam
- 0478/22/F/M/23 Q4: describe how variables and constants are used in programming. The mark scheme's points, in your sketch: both store data (
LEDholds 13,waitholds 1000); the data is reached by a name (an identifier), not by its memory address; the value of a variable can change while the program runs (waitgets smaller); the value of a constant stays the same (LEDis always 13).
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
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.
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
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.
اضغط تشغيل لرؤية المخرجات هنا.