Skip to content

Commit 6fb6dbb

Browse files
committed
loop and setup
1 parent 39fc5c5 commit 6fb6dbb

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

content/micropython/01.basics/08.reference/reference.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ For example:
107107
- [float()](#float)
108108
- [int()](#int)
109109
- [Local / Global Variables](#local--global-variables)
110+
- [Sketch](#sketch)
111+
- [loop()](#loop)
112+
- [setup()](#setup)
110113

111114
## Digital I/O
112115

@@ -1421,3 +1424,50 @@ because it is not declared globally.
14211424
'''
14221425
#print(local_var)
14231426
```
1427+
1428+
## Sketch
1429+
1430+
MicroPython uses scripts as opposed to traditional sketches that require the `void loop()` and `void setup()` functions.
1431+
1432+
A script can be as simple as a single line that prints "Hello World!"
1433+
1434+
```python
1435+
print("Hello World!")
1436+
```
1437+
1438+
### loop()
1439+
1440+
`while True:`
1441+
1442+
A loop is not required in a MicroPython script, but is required in order to run a script continuously on the board. To have a loop in a program, we need to use a [while loop]().
1443+
1444+
**Example:**
1445+
1446+
The example below runs a loop that increases the `value` variable by `1` each time the loop is run. It then prints the value to the REPL, and waits for a second.
1447+
1448+
```python
1449+
import time
1450+
value = 0
1451+
1452+
while True:
1453+
value += 1
1454+
print(value)
1455+
time.sleep(1)
1456+
```
1457+
1458+
### setup()
1459+
1460+
In MicroPython, there's no equalivent of the `setup()` function. Configurations that are traditionally done in this function can simply be declared at the top of the program.
1461+
1462+
**Example:**
1463+
1464+
This script simply declares a pin's pin mode (see [pinMode()](#pinmode)).
1465+
1466+
```python
1467+
from machine import Pin
1468+
1469+
output_pin = Pin(5, Pin.OUT)
1470+
1471+
while True:
1472+
#loops forever
1473+
```

0 commit comments

Comments
 (0)