Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Waveshare CM5 boards
The Best Arduino Robot for Beginners
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
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 >