Powered by TinkerSpace
Join free
TinkerPy Projects lessons
๐Ÿ”
Lesson 6 of 10 ยท Explorer ยท 12 min

Repeat and variables

Do things many times, and remember numbers in variables.

for _ in range(3): runs the indented lines 3 times. Here the LED blinks 3 times when the board starts, then o2 stays on.

TinkerPy
from tinkerpy_mega import *

def setup():
    for _ in range(3):
        write_output(o1, pwm(100))
        wait(300)
        write_output(o1, 0)
        wait(300)

def loop():
    write_output(o2, pwm(100))
A โ€œhelloโ€ blink at start-up.

Variables remember numbers

A variable is a box with a name that holds a number. Here speed grows by 10 each time.

TinkerPy
from tinkerpy_mega import *

def setup():
    pass  # your code goes here

def loop():
    speed = 0
    for _ in range(10):
        speed = speed + 10
        write_output(o1, pwm(speed))
        wait(200)
The LED gets brighter step by step.
๐Ÿš€ Your turn

Change it so speed grows by 20 and repeats only 5 times.

Chat with TinkerPy ๐Ÿ‘‹