powered by TinkerSpace.in
Join free
TinkerPy Tutorial ๐Ÿ“˜ Part 1 ยท Starter ยท Chapter 3 of 24

Names, Comments, import, setup and loop

Every TinkerPy program is built like a house: the toolboxes at the door (import), name tags on every pin, a start-up room that you pass through once (setup) and a room where the action never stops (loop). Learn the plan once and every program becomes easy to read!

๐Ÿ“–Key words

import

Switches on a library โ€” an extra toolbox of functions, like import buzzer or import servo. Import lines go at the very top.

Pin name

A friendly name for a pin: o1 = set_name("lamp"). After that you write lamp instead of o1 everywhere.

Comment

Text after a #. The board ignores it โ€” it is a note for people. TinkerPy adds one for you when you press Enter.

setup:

The block that runs only once, when the board starts: pin settings and one-time jobs like a start-up beep.

loop:

The block that runs again and again, forever. When it reaches the end, it starts again from the top of loop.

Indentation

The 4 spaces at the start of a line. Indented lines belong to the block above them (setup:, loop:, if โ€ฆ).

๐ŸŽฌSee it work

Once, then forever

Press Play. Watch setup run once (the start-up beep), then loop run again and again.

From o1 to lamp

Tap โ€œUse namesโ€ and watch pin numbers turn into plain-English names.

๐Ÿง How it works

The five parts of every program

  • 1. Libraries: # Add the libraries you need and the import lines.
  • 2. Pin names: # Give your pins easy-to-remember names and the set_name lines.
  • 3. Title: # ===== Hello beep ===== โ€” the name of your project.
  • 4. setup: runs once โ€” first the lines Circuit Connections wrote, then your one-time lines. 5. loop: runs forever โ€” your main code.
  • You donโ€™t type parts 1โ€“3: Circuit Connections writes them as you pick parts and names. Parts 4 and 5 are where you code.

Names and comments that write themselves

  • In Circuit Connections, tap a pin, pick the part and type a name. TinkerPy writes o1 = set_name("lamp") at the top and lamp = set_output(led) in setup.
  • Good names: small letters, _ instead of spaces, not starting with a number โ€” lamp, bell_button, red_led. Type โ€œRed LEDโ€ and Compile turns it into red_led.
  • Old code with o1 / i1? Tap the PIN button, then Use names everywhere โ€” it swaps them for your names everywhere.
  • Type a line and press Enter: a comment appears โ€” lamp = ON # turn lamp ON. Want your own words? Write them on a line of their own, starting with # โ€” TinkerPy keeps those.
  • The # button: press to hide / show comments, hold until the ring fills to add a comment to every line that has none.

import: switching on toolboxes

  • Plain things โ€” ON, OFF, wait(), if, loops, maths โ€” need no import.
  • Extra parts need their library: buzzer, servo, ultrasonic, dht11, motor, bluetooth, oled_displayโ€ฆ
  • Pick a buzzer or a servo in Circuit Connections and its import line appears by itself; remove the part and the line goes away.
  • A new project starts with import oled_display, oled_begin() and the o7 / o8 lines, because the OLED screen is switched on. Not using it? Switch the OLED off in Circuit Connections โ†’ Special components.

setup: and loop:

  • setup: = once. Like waking up in the morning. Put start-up beeps and starting values here.
  • loop: = forever. Like breathing all day. Put checking buttons, reading sensors and blinking here.
  • Both end with a colon (:), and the lines inside are indented by 4 spaces.
  • The board runs the lines from top to bottom, one at a time. At the end of loop it jumps back to the first line of loop โ€” not to setup.
  • Wrong spaces? Compile says Inconsistent indentation โ€” line up the lines and compile again.

๐Ÿ’ปCode

# Add the libraries you need
import oled_display

# ===== My TinkerPy Project =====

# This code block executes only once
setup:
    oled_begin()  # starts the OLED
    o7 = set_function(SDA)  # cannot be changed
    o8 = set_function(SCL)  # cannot be changed

# This code block executes forever
loop:

A brand-new project looks like this. The OLED screen is switched on, so its import line and its three setup lines are already there; setup: and loop: are waiting for your code. Circuit Connections pops up straight away so you can add your parts.

# Add the libraries you need
import buzzer

# Give your pins easy-to-remember names
o1 = set_name("light")
o2 = set_name("beeper")

# ===== Hello beep =====

# This code block executes only once
setup:
    light = set_output(led)
    beeper = set_output(buzzer)
    # beep once at start
    beeper = ON  # turn beeper ON
    wait(300)  # pause here for 300 milliseconds before continuing
    beeper = OFF  # turn beeper OFF

# This code block executes forever
loop:
    light = ON  # turn light ON

Start-up hello beep: the buzzer named beeper (o2) beeps once in setup, then loop keeps the LED named light (o1) on. Picking the buzzer added import buzzer at the top. The # beep once at start line is our own comment; the others appeared when we pressed Enter.

# Add the libraries you need
import buzzer

# Give your pins easy-to-remember names
o1 = set_name("beam")
o2 = set_name("horn")

# ===== Lighthouse =====

# This code block executes only once
setup:
    beam = set_output(led)
    horn = set_output(buzzer)
    # one long horn when the lighthouse wakes up
    horn = ON  # turn horn ON
    wait(1000)  # pause here for 1000 milliseconds before continuing
    horn = OFF  # turn horn OFF

# This code block executes forever
loop:
    # a short flash, then a long dark gap
    beam = ON  # flash!
    wait(200)  # pause here for 200 milliseconds before continuing
    beam = OFF  # turn beam OFF
    wait(1800)  # dark for almost 2 seconds

A fully labelled program with all five parts: import, pin names, title, setup and loop. The lines โ€œ# one long horn โ€ฆโ€ and โ€œ# a short flash โ€ฆโ€, plus โ€œflash!โ€ and โ€œdark for almost 2 secondsโ€, were written by hand. Tip: TinkerPy keeps comments on their own line; for your own words at the end of a line, change the automatic comment after you press Enter.

Open TinkerPy Code

๐ŸŽ‰Fun examples

โฐYour day as code

setup: wake up, brush your teeth. loop: breathe, blink, listenโ€ฆ you never stop those!

๐Ÿท๏ธName tags help everyone

On the first day of school, name tags make it easy to call people. Pin names do the same for your code.

๐Ÿ“–Code is read more than written

Programmers spend more time reading code than typing it โ€” good names and comments save hours.

๐ŸŒWhere itโ€™s used

๐Ÿ› ๏ธProject ideas

Start-up hello beep

Pick an LED on o1 (name it light) and a buzzer on o2 (name it beeper). In setup, make the buzzer beep once; in loop, keep the LED on. Run it in the Simulator, then move the beep into loop and see what changes.

๐Ÿงฐ You need: TinkerPy Code (Simulator)
Label every line of a program

Take the Lighthouse program and write your own comment on every line you typed, plus one section comment at the top of setup and loop. Ask a friend to read only the comments and guess what the gadget does.

๐Ÿงฐ You need: TinkerPy Code, a friend

โญPoints to remember

  1. Every program has: imports, pin names, a title, setup: and loop:.
  2. Circuit Connections writes the import, name and setup lines for you.
  3. import switches on a library (an extra toolbox).
  4. ON, OFF, wait(), if, loops and maths need no import.
  5. o1 = set_name("lamp") gives o1 the name lamp.
  6. Names use small letters and _ (no spaces, no number first).
  7. A # starts a comment; the board ignores it.
  8. Press Enter and TinkerPy writes a comment for you.
  9. Your own comments on a line of their own are kept.
  10. setup: runs once when the board starts.
  11. loop: runs again and again, forever.
  12. setup: and loop: end with a colon.
  13. Lines inside a block are indented by 4 spaces.
  14. Code runs from top to bottom; loop goes back to its own top.
๐Ÿคฏ
Did you know?

When a loop has no wait(), the board can run it many thousands of times every second โ€” far faster than you can blink!

โ“Mini quiz

๐Ÿ”‘
Log in to try the mini quiz

5 quick questions to check yourself โ€” free with your TinkerPy account.

Log in