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

Variables, Maths & Logic

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!

๐Ÿ“–Key words

Variable

A named box that stores a number, like count = 0. You can read it and change it while the program runs.

Assign (=)

Puts a value into a variable: speed = 50 means โ€œspeed becomes 50โ€. The right side is worked out first.

Operators

+ add, - subtract, * multiply, / divide, % remainder (what is left over).

Comparison

A question with a yes / no answer: == equal, != not equal, < less, > more, <=, >=.

Logic words

and: both must be yes ยท or: at least one is yes ยท not: flips yes and no.

๐ŸŽฌSee it work

The variable box

Press Step to run speed = speed + 10. Watch the box grow and the LED brighten.

Even or odd?

Press Step to run the program one line at a time. Watch count change and the lamp follow.

๐Ÿง How it works

Making and changing variables

  • count = 0 โ†’ makes a box called count holding 0.
  • count = count + 1 โ†’ take what is in the box, add 1, put it back. The right side is worked out first.
  • Names follow the pin-name rules: small letters and _ (fan_speed). Donโ€™t reuse a pinโ€™s name for a variable.
  • To remember a number from one loop round to the next (a score, a count), give its starting value in setup:. A count = 0 at the top of loop: starts again from 0 every round.
  • A program can have up to 10 number variables.

Maths on the board

  • 5 + 3 = 8 ยท 10 - 3 = 7 ยท 4 * 2 = 8 ยท use brackets: (2 + 3) * 4 = 20.
  • Whole numbers only: 9 / 2 = 4 (no half) and 9 % 2 = 1 (the 1 left over). Numbers go from -32768 to 32767.
  • % is a superstar: count % 2 == 0 โ†’ even ยท count % 5 == 0 โ†’ every 5th time.
  • Use a variable as a value: lamp = pwm(speed) sets the brightness from 0 to 100 (more on pwm in chapter 8).
  • random(1, 6) gives a surprise whole number from 1 to 6 โ€” a dice!

Comparisons: yes or no?

  • == equal, != not equal, < less than, > greater than, <= less or equal, >= greater or equal.
  • = gives a value (count = 5); == asks a question (if count == 5:). Compile fixes a single = inside an if for you.
  • Every comparison gives only yes or no โ€” perfect for if, elif and while.

Logic: and, or, not

  • and: both must be yes โ€” key1 == ON and key2 == ON (like a two-key bank locker).
  • or: at least one must be yes โ€” key1 == ON or key2 == ON.
  • not: flips the answer โ€” not key2 == ON is yes when key2 is not pressed.
  • Write every comparison in full: if x > 1 and x < 5: (not 1 < x < 5).

๐Ÿ’ปCode

# 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.

Open TinkerPy Code

๐ŸŽ‰Fun examples

๐ŸCricket scoreboard

runs = runs + 4 for a boundary, runs = runs + 6 for a six. The scoreboard is just a variable!

๐ŸฌSharing laddoos

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.

๐Ÿ”Two keys, one locker

Bank lockers often need two keys turned together โ€” the bankโ€™s key and yours. That is an โ€œandโ€ decision.

๐ŸŒWhere itโ€™s used

๐Ÿ› ๏ธProject ideas

Button press counter

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 Simulator
Even-or-odd blinker

Make 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 Simulator

โญPoints to remember

  1. A variable is a named box that stores a number.
  2. = puts a value into a variable; == asks if two are equal.
  3. count = count + 1 adds 1 to count.
  4. The right side is worked out first.
  5. Give a starting value in setup: to remember it between loop rounds.
  6. + add, - subtract, * multiply, / divide, % remainder.
  7. Whole numbers only: 9 / 2 = 4 and 9 % 2 = 1.
  8. count % 2 == 0 means count is even.
  9. random(1, 6) gives a surprise number from 1 to 6.
  10. Comparisons: == != < > <= >= give yes or no.
  11. and: both yes ยท or: at least one yes ยท not: flips the answer.
  12. Write comparisons in full: x > 1 and x < 5.
  13. A program can have up to 10 number variables.
๐Ÿคฏ
Did you know?

The word โ€œvariableโ€ means โ€œable to changeโ€ โ€” and a variable in your program can change thousands of times every second while it runs!

โ“Mini quiz

๐Ÿ”‘
Log in to try the mini quiz

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

Log in