114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
75% Percent Complete
By Kevin McAleer, 4 Minutes
Often we want to run a piece of code a specific number of times.
We can use the for statement to run a block of code a number of times:
for
for i in range(100): print('i is:',i)
In this example, the loop will start by assign the variable i the value 0, and then run the block of code print('i is:',i) which will print out:
i
0
print('i is:',i)
i is 0
Parameters / Arguments Parameters, also known as arguments are variables that act as inputs into a function. We’ll look at functions shortly. An example of an argument is the value 100 that we just used in in the range function. In some programming languages there is a difference between an argument (a literal value like 100), and a parameter that is a variable (such as a that refers to another value). MicroPython doesn’t make such a distinction. Arguments can be values or variables Arguments are separated by commas Arguments are positional - the order the arguments are provided should match the order the function expects Arguments can be named, in which case the order doesn’t matter Arguments can be optional
Parameters, also known as arguments are variables that act as inputs into a function. We’ll look at functions shortly. An example of an argument is the value 100 that we just used in in the range function. In some programming languages there is a difference between an argument (a literal value like 100), and a parameter that is a variable (such as a that refers to another value). MicroPython doesn’t make such a distinction.
Parameters
arguments
100
range
a
The range function can take one, two or three parameters: range(start «optional», end, steps «optional»)
start
end
steps
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 until the end number is reached.
range()
1
for i in range(100): print(i)
If two parameters are provided, the range() function starts from start value and increments in steps of 1 until the end value is reached (its actually the end value -1).
-1
for i in range(50,200): print(i)
If three parameters are provided, the range() function starts from start value and increments in steps of the steps value until the end value is reached (again, the end value -1).
for i in range(50,200,10): print(i)
if we want to run a block of code many times, based on whether or not a condition is True or False, we can use the while loop.
True
False
while
You’ll often see this to run blocks of code indefinitely on MicroControllers as we may want to run the code forever or until the device is reset.
while True: print('hello')
The while loop is particularly useful in MicroPython as we may want to check the status of a sensor or externally connected device.
import machine button = machine.Pin(0, machine.Pin.IN) while button.value() == 0: print('button is not pressed')
In this example we use the machine library to access the general purpose input/output pins (also known as GPIO pins). We assign pin 0 to the button variable and also define the pin as an input put using the machine.Pin.IN parameter.
machine
GPIO
pin 0
button
machine.Pin.IN
The progam will keep printing the button is not pressed until button is pressed and returns a 1 value.
button is not pressed
Break
Continue
Pass
< Previous Next >