Reading, memorizing and calculating
Variables can be used to store values and information. In principle, they are containers for changeable values. Saved variables can be called up again at a later time to read or change their value or to calculate with them. Variables always consist of a unique name. This name is arbitrary and is assigned by the user.
Utilization of variables
In the following example, we combine a variable with an if condition
. We initialize the variable counter
with the value 0 and the revision number of the Joy-Car in the on start
block. In the forever
block, the if condition
is used to check whether the variable has the value 1. If this is the case, we activate the headlights of the Joy-Car. If this is not the case, we switch off the headlights. The value of the counter
variable is then displayed on the LEDs and the value of the counter
variable is increased by 1.
JoyCar.initJoyCar(RevisionMainboard.OnepThree)
let counter = 0
basic.forever(function () {
if (counter == 1) {
JoyCar.light(ToggleSwitch.On)
} else {
JoyCar.light(ToggleSwitch.Off)
}
basic.showString("" + (counter))
counter += 1
})
The sample code in MicroPython is there because it is also available in MakeCode. This is super handy because if you're already comfortable with MakeCode, it's much easier to get started with MicroPython. By having sample code in both environments, you can continue learning without much interruption. This will help you understand how programming works and you can see how to do the same things in different programming languages.
# Import necessary libraries
from microbit import *
import neopixel
# Define your Joy-Car mainboard revision
joycar_rev = 1.3
# Define object for the lights
np = neopixel.NeoPixel(pin0, 8)
# Define values for the lights
# Values which LEDs are to be activated
headlights = (0, 3)
backlights = (5, 6)
indicator_left = (1, 4)
indicator_right = (2, 7)
indicator_warning = (1, 2, 4, 7)
# Values, which color should be displayed on the LEDs
led_white = (60, 60, 60)
led_red = (60, 0, 0)
led_off = (0, 0, 0)
led_red_br = (255, 0, 0)
led_orange = (100, 35, 0)
# method to activate/deactivate lights
def lights(on = True):
if on:
for x, y in zip(headlights, backlights):
# define white for the headlights
np[x] = led_white
# define dark red for the backlights
np[y] = led_red
else:
for x, y in zip(headlights, backlights):
# define black for the headlights and backlights
np[x] = led_off
np[y] = led_off
np.show()
# Define variable for counting
counter = 0
while True:
# Check whether the counter is 1
if counter == 1:
# Switch on low beam
lights()
else:
# Switch off low beam
lights(on = False)
# Display counter on LED matrix
display.scroll(str(counter))
# Increase counter by 1
counter += 1
Changing variables
In the next step, we add an additional component to our previous example. We use another if query
to check whether the value of our variable is below 4. If this is the case, we increase our counter
variable by 1 again. If this is not the case and the value of our variable is 4 or greater, we set our variable back to 0.
In this way, we run through our instructions again and again, even without using a loop.
JoyCar.initJoyCar(RevisionMainboard.OnepThree)
let counter = 0
basic.forever(function () {
if (counter == 1) {
JoyCar.light(ToggleSwitch.On)
} else {
JoyCar.light(ToggleSwitch.Off)
}
basic.showString("" + (counter))
if (counter < 4) {
counter += 1
} else {
counter = 0
}
})
The sample code in MicroPython is there because it is also available in MakeCode. This is super handy because if you're already comfortable with MakeCode, it's much easier to get started with MicroPython. By having sample code in both environments, you can continue learning without much interruption. This will help you understand how programming works and you can see how to do the same things in different programming languages.
# Import necessary libraries
from microbit import *
import neopixel
# Define your Joy-Car mainboard revision
joycar_rev = 1.3
# Define object for the lights
np = neopixel.NeoPixel(pin0, 8)
# Define values for the lights
# Values which LEDs are to be activated
headlights = (0, 3)
backlights = (5, 6)
indicator_left = (1, 4)
indicator_right = (2, 7)
indicator_warning = (1, 2, 4, 7)
# Values, which color should be displayed on the LEDs
led_white = (60, 60, 60)
led_red = (60, 0, 0)
led_off = (0, 0, 0)
led_red_br = (255, 0, 0)
led_orange = (100, 35, 0)
# method to activate/deactivate lights
def lights(on = True):
if on:
for x, y in zip(headlights, backlights):
# define white for the headlights
np[x] = led_white
# define dark red for the backlights
np[y] = led_red
else:
for x, y in zip(headlights, backlights):
# define black for the headlights and backlights
np[x] = led_off
np[y] = led_off
np.show()
# Define variable for counting
counter = 0
while True:
# Check whether the counter is 1
if counter == 1:
# Switch on low beam
lights()
else:
# Switch off low beam
lights(on = False)
# Display counter on LED matrix
display.scroll(str(counter))
# The counter should be in the interval between 0 and 4
if counter < 4:
# Increase counter by 1
counter += 1
else:
# Reset counter to 0
counter = 0
Variable -1
Of course, a variable does not always have to be increased by 1. Variables can also be decreased and used for more complex mathematical calculations. Since the variables only represent a container, they can be used for practically any value, any buffer and any calculation. In the next example, we will show once again how our previous example works with a subtraction instead of an addition. This time we have used the set variable to
block instead of the change variable by
block and provided it with a mathematical operation (counter - 1)
.
JoyCar.initJoyCar(RevisionMainboard.OnepThree)
let counter = 0
basic.forever(function () {
if (counter == 1) {
JoyCar.light(ToggleSwitch.On)
} else {
JoyCar.light(ToggleSwitch.Off)
}
basic.showString("" + (counter))
if (counter > 0) {
counter = counter - 1
} else {
counter = 4
}
})
The sample code in MicroPython is there because it is also available in MakeCode. This is super handy because if you're already comfortable with MakeCode, it's much easier to get started with MicroPython. By having sample code in both environments, you can continue learning without much interruption. This will help you understand how programming works and you can see how to do the same things in different programming languages.
# Import necessary libraries
from microbit import *
import neopixel
# Define your Joy-Car mainboard revision
joycar_rev = 1.3
# Define object for the lights
np = neopixel.NeoPixel(pin0, 8)
# Define values for the lights
# Values which LEDs are to be activated
headlights = (0, 3)
backlights = (5, 6)
indicator_left = (1, 4)
indicator_right = (2, 7)
indicator_warning = (1, 2, 4, 7)
# Values, which color should be displayed on the LEDs
led_white = (60, 60, 60)
led_red = (60, 0, 0)
led_off = (0, 0, 0)
led_red_br = (255, 0, 0)
led_orange = (100, 35, 0)
# method to activate/deactivate lights
def lights(on = True):
if on:
for x, y in zip(headlights, backlights):
# define white for the headlights
np[x] = led_white
# define dark red for the backlights
np[y] = led_red
else:
for x, y in zip(headlights, backlights):
# define black for the headlights and backlights
np[x] = led_off
np[y] = led_off
np.show()
# Define variable for counting
counter = 0
while True:
# Check whether the counter is 1
if counter == 1:
# Switch on low beam
lights()
else:
# Switch off low beam
lights(on = False)
# Display counter on LED matrix
display.scroll(str(counter))
# The counter should be in the interval 4 to 0 (backwards)
if counter > 0:
# Decrease counter by 1
counter -= 1
else:
# Reset counter to 4
counter = 4