5.1. Your first script¶
A Python script is a sequence of instructions saved in a text
file with a .py extension. The OpenMV Cam runs whatever script
is currently open in the IDE when you press the green Run
button, and prints any output in the IDE’s serial terminal along
the bottom of the window.
The simplest possible script is one line:
print("Hello, OpenMV!")
Pressing Run sends this script to the camera, which executes it and prints the message back in the IDE.
5.1.1. What print does¶
print() is a built-in function – a piece of code that lives
inside the firmware and is always available without any setup.
Hand it one or more values inside parentheses and it prints a text
representation of those values in the IDE.
You can hand print() anything, not just text:
print(42)
print(3.14)
print("temperature", 25)
Multiple arguments are separated by spaces in the output:
>>> temperature 25
5.1.3. Indentation matters¶
Python uses indentation – the whitespace at the start of a line –
to group related lines together. Inside a function, an
if statement, or a loop, every line that belongs to the block
is indented by the same amount (four spaces is the convention).
Mixing tabs and spaces or changing the indent width inside a block
is a syntax error.
Top-level statements like the print calls above sit at indent
zero. Indented blocks appear once you start writing branches and
loops.
Tip
The IDE auto-indents for you as soon as you finish a
line ending in :. If you ever get a mysterious
IndentationError, select the offending block and press
Tab / Shift+Tab to re-align it.
5.1.4. Running the script again¶
Every time you press Run, the camera stops whatever it was doing, loads the current script, and starts from the top. There is no separate compile step. Change a line, press Run again, and the new output appears in the terminal.
To make a script run automatically every time the camera powers
on, save it as main.py on the camera’s filesystem.
5.1.2. Comments¶
A
#starts a comment – the rest of the line is ignored by Python. Use comments to explain why the code does what it does; the code itself already shows what it does.There is no separate syntax for multi-line comments in Python. Comment several lines individually, or select the lines in the IDE and press
Ctrl+/to toggle them on and off.