ุงู†ุชู‚ู„ ู„ู„ู…ุญุชูˆู‰
Robo EgyptRobo Egypt
โ˜ฐ Course contents
Robo Maker: home
0. Lab tour1
1. The start: the Arduino6
2. Wiring and the traffic light4
3. Inputs: the button4
4. Sound and movement5
5. Sensors and numbers5
6. Distance, the screen and colours5
7. Motors and the car4
8. The project: the city's smart gate3
9. From blocks to C++ code5
10. Sensors and screens in code5
11. Smart systems5
12. Bluetooth: control from the phone1

๐Ÿ› ๏ธ Section project: a button that turns on and off

Section 3: Inputs: the buttonLesson 3.3 ยท โฑ 12 min
๐ŸŽฏ Project goal
Every press of the button changes the LED: one press turns it on, and the next press turns it off, like a light switch.

We need a variable that remembers the LED's state. On every press we flip the state, then wait until the button is released so one press isn't counted many times.

  1. Make a variable called on.
  2. If the button is pressed: set on = its opposite (not on).
  3. Wait until the button is released (wait until not pressed).
  4. Turn the LED on if on is true, otherwise turn it off.

๐Ÿ“Œ Another example: the button turns on but not off

With no โ€œelseโ€: one press turns the LED on, and it stays on.

๐Ÿงฉ The button turns on but not off
Blocks: loop โ€” repeats forever
Before pressing
Before pressing
After pressing and letting go
After pressing and letting go
โœ๏ธ Practise
Build the project.
โœ๏ธ Practise here in the lessonor open the lab in a new page โ†—

๐ŸŽฏ Challenge yourself

Easy

A press blinks the LED 5 times, then it stops.

๐Ÿ‘€ Show the solution
๐Ÿงฉ Solution
Blocks: loop โ€” repeats forever
In the lab
In the lab

Result: five blinks.

Medium

A press lights the LED for two seconds (a timer).

๐Ÿ‘€ Show the solution
๐Ÿงฉ Solution
Blocks: loop โ€” repeats forever
In the lab
In the lab

Result: it lights up, then goes off by itself.

Hard

Not pressed, the LED blinks slowly (waiting), and pressed, it stays steadily on.

๐Ÿ‘€ Show the solution
๐Ÿงฉ Solution
Blocks: loop โ€” repeats forever
In the lab
In the lab

Result: a waiting blink, then a steady light.

Finished the lesson? Mark it so your progress counts.