מבוא לרובוטיקה וארדואינו: מדליקים נורה · כל קובץ בעמוד משלו, עם הקוד המלא וכפתור הורדה.Intro to robotics & Arduino: light an LED · one file per page, with the full code and a download button.
⬇ להוריד הכל (zip)Download all (zip)
הנורה שבלוח (פין 13) מהבהבת. בלי חיווט — רק מעלים ומריצים. setup() פעם אחת, loop() לנצח.The onboard LED (pin 13) blinks. No wiring — just upload and run. setup() once, loop() forever.
/* * Lesson 1 - Level 1: Blink * The "Hello World" of Arduino. * Uses the LED already built onto the board (pin 13). * No wiring needed - just upload and watch. */// Pin 13 has an LED soldered onto most Arduino boards.const int LED_PIN = 13;void setup() { // Runs ONCE when the board powers up. // Tell the board this pin is an OUTPUT (we drive it). pinMode(LED_PIN, OUTPUT);}void loop() { // Runs FOREVER, over and over, as long as there is power. digitalWrite(LED_PIN, HIGH); // turn the LED on (5V on the pin) delay(500); // wait 500 ms (half a second) digitalWrite(LED_PIN, LOW); // turn the LED off (0V on the pin) delay(500); // wait half a second}
אותו קוד בדיוק — רק מחליפים את מספר הפין ל-9, ומחברים נורה משלכם דרך נגד 220Ω ל-GND.The exact same code — just change the pin to 9, and wire your own LED through a 220Ω resistor to GND.
/* * Lesson 1 - Level 2: Blink an EXTERNAL LED * Same program as Level 1, but now the LED is one YOU wired * on the breadboard, on pin 9. * * Wiring: * pin 9 --> [220 ohm resistor] --> LED long leg (+, anode) * LED short leg (-, cathode) --> GND */const int LED_PIN = 9; // the pin our LED is wired tovoid setup() { pinMode(LED_PIN, OUTPUT);}void loop() { digitalWrite(LED_PIN, HIGH); // on delay(300); digitalWrite(LED_PIN, LOW); // off delay(300);}
עכשיו הארדואינו גם קורא קלט. הלולאה המלאה בשש שורות: חש (כפתור) → חושב (if) → פועל (נורה).Now the Arduino also reads input. The full loop in six lines: sense (button) → think (if) → act (LED).
/* * Lesson 1 - Bonus: a button that controls the LED * Now the Arduino also READS the world (an input), not just drives it. * This is the sense -> think -> act loop in six lines. * * Wiring: * LED : pin 9 --> [220 ohm] --> LED(+), LED(-) --> GND * Button: one leg --> pin 2, the other leg --> GND * (INPUT_PULLUP means: not pressed = HIGH, pressed = LOW) */const int LED_PIN = 9;const int BTN_PIN = 2;void setup() { pinMode(LED_PIN, OUTPUT); pinMode(BTN_PIN, INPUT_PULLUP); // uses the chip's internal resistor}void loop() { // SENSE: read the button bool pressed = (digitalRead(BTN_PIN) == LOW); // THINK + ACT: if pressed, light the LED; otherwise turn it off if (pressed) { digitalWrite(LED_PIN, HIGH); } else { digitalWrite(LED_PIN, LOW); }}
שלוש נורות ברצף — אדום, ירוק, צהוב. "תוכנית" היא פשוט רשימת צעדים לפי הסדר.Three LEDs in sequence — red, green, yellow. A "program" is just a list of steps, in order.
/* * Lesson 1 - Bonus: a tiny traffic light (two or three LEDs) * Still just blink - but now a SEQUENCE across several pins. * Shows that "a program" is simply a list of steps, in order. * * Wiring (each LED): pin --> [220 ohm] --> LED(+), LED(-) --> GND */const int RED = 9;const int YELLOW = 10;const int GREEN = 11;void setup() { pinMode(RED, OUTPUT); pinMode(YELLOW, OUTPUT); pinMode(GREEN, OUTPUT);}void loop() { // RED digitalWrite(RED, HIGH); delay(3000); digitalWrite(RED, LOW); // GREEN digitalWrite(GREEN, HIGH); delay(3000); digitalWrite(GREEN, LOW); // YELLOW (short) digitalWrite(YELLOW, HIGH); delay(1000); digitalWrite(YELLOW, LOW);}
פין דיגיטלי הוא דלוק/כבוי — אבל analogWrite מהבהב מהר (PWM) ונותן בהירות ביניים. חובה פין עם ~.A digital pin is on/off — but analogWrite flickers fast (PWM) for in-between brightness. Needs a ~ pin.
/* * Lesson 1 - Bonus: FADE instead of blink * A digital pin is only ON or OFF - but analogWrite() flickers it * so fast (PWM) that the LED looks half-bright. We ride that from * 0 (off) up to 255 (full) and back, to breathe the LED in and out. * * NOTE: use a pin marked with "~" (3, 5, 6, 9, 10, 11). We use 9. * * Wiring: same as Level 2. */const int LED_PIN = 9; // must be a ~ (PWM) pinvoid setup() { pinMode(LED_PIN, OUTPUT);}void loop() { // fade up: 0 -> 255 for (int b = 0; b <= 255; b++) { analogWrite(LED_PIN, b); delay(5); } // fade down: 255 -> 0 for (int b = 255; b >= 0; b--) { analogWrite(LED_PIN, b); delay(5); }}