Every inventor began with one small project. Pick one and bring your idea to life today!
Great inventors and great teachers all started with one small step. Take yours today!
The same kits we use in 40+ school labs โ grab yours and build something youโre proud of.
Log in to continue
A variable is a box with a name that remembers a number. Scores, counts, speeds and levels all live in variables โ and your board can do maths with them and make clever decisions with and, or and not in a flash!
A named box that stores a number, like count = 0. You can read it and change it while the program runs.
Puts a value into a variable: speed = 50 means โspeed becomes 50โ. The right side is worked out first.
+ add, - subtract, * multiply, / divide, % remainder (what is left over).
A question with a yes / no answer: == equal, != not equal, < less, > more, <=, >=.
and: both must be yes ยท or: at least one is yes ยท not: flips yes and no.
Press Step to run speed = speed + 10. Watch the box grow and the LED brighten.
Press Step to run the program one line at a time. Watch count change and the lamp follow.
# Give your pins easy-to-remember names o1 = set_name("lamp") # ===== Getting brighter ===== # This code block executes only once setup: lamp = set_output(led) # This code block executes forever loop: speed = 0 # set speed to 0 repeat(10): # repeat the steps below 10 times in a row speed = speed + 10 # add 10 to speed lamp = pwm(speed) # brightness = the number in speed wait(200) # pause here for 200 milliseconds before continuing
speed grows by 10 in each of the 10 rounds, so the lamp gets brighter step by step: 10, 20 โฆ 100. Then loop starts again and speed = 0 resets it. The comments on the two speed lines are our own โ clearer than the automatic ones.
# Give your pins easy-to-remember names i1 = set_name("button") o1 = set_name("lamp") # ===== Button press counter ===== # This code block executes only once setup: button = set_input(push_button, active_low) lamp = set_output(led) count = 0 # start counting from 0 # This code block executes forever loop: if button == ON: # when button is ON count = count + 1 # one more press if count % 5 == 0: # every 5th press lamp = ON # turn lamp ON wait(500) # pause here for 500 milliseconds before continuing lamp = OFF # turn lamp OFF wait(300) # one press counts once
Button press counter: count starts at 0 in setup, so it is remembered between loop rounds. Each press adds 1, and on every 5th press (count % 5 == 0) the lamp flashes. The wait(300) stops one long press from counting many times.
# Give your pins easy-to-remember names o1 = set_name("lamp") # ===== Even-or-odd blinker ===== # This code block executes only once setup: lamp = set_output(led) count = 0 # set count to 0 # This code block executes forever loop: count = count + 1 # set count to count + 1 if count % 2 == 0: # is count even? lamp = ON # turn lamp ON else: # otherwise lamp = OFF # turn lamp OFF wait(500) # pause here for 500 milliseconds before continuing
Even-or-odd blinker: count goes 1, 2, 3, 4 โฆ every half second. count % 2 == 0 is yes for even numbers, so the lamp is on for 2, 4, 6 โฆ and off for 1, 3, 5 โฆ
# Give your pins easy-to-remember names i1 = set_name("key1") i2 = set_name("key2") o1 = set_name("green") o2 = set_name("red") # ===== Two-key locker ===== # This code block executes only once setup: key1 = set_input(push_button, active_low) key2 = set_input(push_button, active_low) green = set_output(led) red = set_output(led) # This code block executes forever loop: if key1 == ON and key2 == ON: # both keys turned? green = ON # turn green ON red = OFF # turn red OFF elif key1 == ON or key2 == ON: # only one key? green = OFF # turn green OFF red = ON # turn red ON else: # otherwise green = OFF # turn green OFF red = OFF # turn red OFF
Two-key locker with and / or: both keys pressed โ green (open); only one key โ red (warning); no key โ both off. The and question is checked first, so pressing both never reaches the or line.
runs = runs + 4 for a boundary, runs = runs + 6 for a six. The scoreboard is just a variable!
Share 9 laddoos between 2 friends: each gets 4 (9 / 2) and 1 is left over (9 % 2). That is exactly how the board divides.
Bank lockers often need two keys turned together โ the bankโs key and yours. That is an โandโ decision.
Count button presses in a variable that starts in setup. Flash the LED on every 5th press, and make a buzzer beep on every 10th press (count % 10 == 0). Remember a short wait so one press counts once.
๐งฐ You need: push button, LED, buzzer, TinkerPy board or SimulatorMake a variable that counts up every half second. Light a red LED on even counts and a green LED on odd counts. Then change it to light only on every 3rd count.
๐งฐ You need: 2 LEDs, TinkerPy board or SimulatorThe word โvariableโ means โable to changeโ โ and a variable in your program can change thousands of times every second while it runs!
5 quick questions to check yourself โ free with your TinkerPy account.
Log in