You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You will need to have Python and `mpremote` installed on your system, [follow these instructions](https://docs.micropython.org/en/latest/reference/mpremote.html).
47
+
You will need to have Python and `mpremote` installed on your system, [follow these instructions](https://docs.micropython.org/en/latest/reference/mpremote.html) to do so.
Copy the folder `arduino` and its content into your board's `lib` folder using your preferred method
56
-
55
+
### Manual Installation
56
+
Copy the folder `arduino` and its content into your board's `lib` folder using your preferred method.
57
57
58
58
59
59
## Usage
60
60
61
-
The structure of an Arduino MicroPython program will look as follows:
61
+
The structure of a MicroPython program based on the Arduino Runtime for MicroPython will look as follows:
62
62
63
-
```Python
63
+
```py
64
64
from arduino import*
65
65
66
66
defsetup():
@@ -75,46 +75,46 @@ start(setup, loop)
75
75
76
76
The program above will define two main methods: `setup()` and `loop()`.
77
77
`setup()` will be invoked once at the execution of the program, while `loop()` will be invoked over and over until the program is stopped.
78
-
The stop condition might be caused by a system error or by manual trigger from the user during development/test.
78
+
The stop condition might be caused by a system error or by manual trigger from the user during development/testing.
79
+
The `start()` command is what causes these functions to be run as described above.
79
80
80
-
The `start()` command is what causes the program to run, and is to be considered of high value in the MicroPython world.
81
-
While traditionally the code above would be written as follows
81
+
Traditionally the example code above would be written as follows in MicroPython:
82
82
83
-
```Python
83
+
```py
84
84
from time import sleep_ms
85
85
86
-
print('starting my program)
86
+
print('starting my program')
87
87
88
88
whileTrue:
89
89
print('loop')
90
90
sleep_ms(1000)
91
91
```
92
92
93
-
Using the Arduino Runtime for MicroPython introduces some nice features, like the possibility to wrap user code in functions which can be tested during learning/development using the REPL.
94
-
Running the Arduino formatted code, omitting the `start()` command, would create the functions and every variable orobjectin the MicroPython board and allow the user to simply change a variable, set the property of an objectand simply call `loop()` to see the results of their changes.
95
-
A more interactive approach to learning/testing/debugging.
93
+
Using the Arduino Runtime for MicroPython introduces some nice features, like the possibility to wrap user code in functions which can be tested during learning/development using the [REPL](https://docs.micropython.org/en/latest/reference/repl.html).
94
+
95
+
To debug your application interactively you can use an approach in which you define `setup()` and `loop()` but omit the `start()` command. That way you can change variables or set the property of an object through the REPL and simply call `loop()`to see the results of their changes without "restarting" the complete script.
96
96
97
-
We also introduce a new way of cleaning up andor resetting variables, objects, timers, leveraging a `cleanup()` method which will be called when the program is stopped or a system error happens which stops the execution of the program.
98
-
Please refer to the example "nano_esp32_advanced.py".
97
+
We also introduced a new way of cleaning up and or resetting variables, objects, timers, leveraging a `cleanup()` method which will be called when the program is stopped or a system error occurs which stops the execution of the program.
98
+
Please refer to the example ["nano_esp32_advanced.py"](./examples/02_nano_esp32_advanced.py).
99
99
100
-
This brings the implemented runtime commands to the three described below
100
+
The runtime commands used in the example are explained in more detail below.
101
101
102
102
### setup()
103
103
104
-
Is run _once_and should contain initialisation code.
104
+
Is run **once** and should contain initialisation code.
105
105
106
106
### loop()
107
107
108
108
Is run indefinitely until the program stops.
109
109
110
110
### cleanup()
111
111
112
-
Is run _once_ when the program stops. This happen either when the user manually stops the execution of the program orif an error in the user code is thrown.
113
-
It should contain code such as resetting the value of variables, stopping timers, causing threads to stop running.
112
+
Is run **once** when the program stops. This happen either when the user manually stops the execution of the program or if an error in the user code is thrown.
113
+
It should contain code such as resetting the value of variables, resetting peripherals, stopping timers, causing threads to stop running.
114
114
115
-
A `cleanup()` enhanced version of our initial program would look like this
115
+
A version of our initial program that leverages the `cleanup()` function would look like this:
116
116
117
-
```Python
117
+
```py
118
118
from arduino import*
119
119
120
120
defsetup():
@@ -133,18 +133,16 @@ start(setup, loop, cleanup)
133
133
> [!NOTE]
134
134
> `cleanup()` is not invoked when the program stops because the hardware reset button on the board was pressed.
135
135
136
-
137
136
## Commands
138
137
139
138
Here's a list of commands and wrappers that can be used in your Arduino MicroPython program.
140
139
141
140
### pin_mode(PIN_NUMBER/ID, MODE)
142
141
143
-
This method is only provided as a mean to transition from Arduino C++ to MicroPython, but is redundant and unnecessary.
144
-
Might be still used with no harm to slowly drop the habit.
142
+
This method is only provided as a mean to transition from Arduino C++ to MicroPython. In MicroPython a Pin object can be used directly instead.
145
143
The following methods apply the required Pin direction on the fly.
146
144
147
-
```Python
145
+
```py
148
146
pin_mode(3, INPUT)
149
147
pin_mode('D7', INPUT)
150
148
```
@@ -153,68 +151,67 @@ Will set the direction of the specified Pin and allow writing to or reading from
153
151
154
152
### digital_read(PIN_NUMBER/ID)
155
153
156
-
Reads a digital value from the pin withNumber orID specified.
154
+
Reads a digital value from the pin with the specified number or ID.
157
155
For example:
158
156
159
-
```Python
157
+
```py
160
158
digital_read(12)
161
159
digital_read('D7')
162
160
digital_read('A3')
163
161
```
164
162
165
-
return a value of `1`or`0` depending on the signal attached to the specified pins, for instance a button or a digital sensor.
163
+
returns a value of `1` or `0` depending on the signal attached to the specified pins, for instance a button or a digital sensor.
166
164
167
165
### digital_write(PIN_NUMBER/ID, VALUE)
168
166
169
167
Writes the digital value (`HIGH|LOW|True|False|1|0`) to the pin with Number or ID specified.
170
168
For example:
171
169
172
-
```Python
170
+
```py
173
171
digital_write(12, HIGH)
174
172
digital_write('D7', 1)
175
173
digital_write('A3', 0)
176
174
```
177
175
178
-
Will set the pin to the specified value.
176
+
Sets the pin to the specified value.
179
177
180
178
### analog_read(PIN_NUMBER/ID)
181
179
182
-
Reads the analog value for the Voltage applied to the pinpinwithNumber orID specified.
180
+
Reads the analog value for the Voltage applied to the pin with the specified number or ID.
183
181
For example:
184
182
185
-
```Python
183
+
```py
186
184
analog_read('A3')
187
185
analog_read('D18')
188
186
analog_read('2')
189
187
```
190
188
191
-
return a value between `0`and the maximum allowed by the processor's ADC based on the Voltage applied to the specified Pin.
189
+
returns a value between `0` and the maximum resolution of the processor's ADC based on the Voltage applied to the specified Pin.
192
190
Could be used to read the Voltage of a battery or any other analog source such as a potentiometer, light or moisture sensor to name a few.
193
191
194
192
### analog_write(PIN_NUMBER/ID, VALUE)
195
193
196
194
Writes an analog value (PWM) to the pin with Number or ID specified, if the Pin supports it.
197
-
VALUE should be between 0and the maximum allowed PWM value and it's highly platform specific.
198
-
The method makes a conversion betwen the number and`frequency`/`duty_cycle`.
195
+
VALUE should be between 0 and the maximum allowed PWM value 255.
199
196
200
-
```Python
197
+
```py
201
198
analog_write(12, 255)
202
199
analog_write('D7', 128)
203
200
analog_write('A3', 64)
204
201
```
205
202
206
-
Will generate a modulated signal on the specified Pin.
207
-
Can be used to control small motors with low current needsas well as servo motors.
203
+
Will generate a pulse width modulated signal on the specified Pin.
204
+
Can be used to control small motors with low current needs, servo motors or an LED's brightness.
208
205
209
206
> [!IMPORTANT]
210
-
> The numeric value forPIN_NUMBERis usually the processor's GPIO number, while values enclosed in quotes are "named pins" and are platform/implementation specific, not guaranteed to be valid. A `ValueError` exception with label "invalid pin" is thrown if the pin number or ID is not valid.
207
+
> The numeric value for PIN_NUMBER is usually the processor's GPIO number, while values enclosed in quotes are "named pins" and are platform/implementation specific. A `ValueError` exception with label "invalid pin" is thrown if the pin number or ID is not valid.
211
208
212
209
### delay(MILLISECONDS)
213
210
214
-
Will halt the execution of your program for the amount of _milliseconds_ specified in the parameter.
211
+
Will halt the execution of your program for the amount of **milliseconds** specified in the parameter.
215
212
It is to be considered a code-blocking command.
216
213
217
-
```Python
214
+
```py
218
215
delay(1000) # Delay the execution for 1 second
219
216
```
220
217
@@ -223,26 +220,27 @@ delay(1000) # Delay the execution for 1 second
223
220
Some utility methods are provided and are still in development:
224
221
225
222
*`map(x, in_min, in_max, out_min, out_max)`
226
-
will remap the value `x`from its inputrange to an output range
223
+
Remaps the value `x` from its input range to an output range
227
224
*`mapi(x, in_min, in_max, out_min, out_max)`
228
225
same as `map` but always returns an integer
229
226
*`random(low, high=None)`
230
-
will returna random number between `0`and`low`-1if no `high`isprovide, otherwise a value between `low`and`high`-1
227
+
Returns a random number between `0` and `low` - 1 if no `high` is provided, otherwise a value between `low` and `high` - 1
231
228
*`constrain(val, min_val, max_val)`
232
-
will return a capped value of the number `val` between `min_val`and`max_val`
229
+
Returns a capped value of the number `val` between `min_val` and `max_val`
233
230
*`lerp(start, stop, amount)`
234
-
will return a linear interpolation (percentage) of `amount` between `start`and`stop`
231
+
Returns a linear interpolation (percentage) of `amount` between `start` and `stop`
Will create a new Python file (`.py`) with the specified name at the provided path.
241
-
By default if a filewith the same name is found, it will append a timestamp, but overwrite can be forced to True.
242
-
Providing a source path it will use that file's content, effectively copying the code from one file to the newly created one.
243
-
Example:
237
+
Creates a new Python file (`.py`) with the specified name at the provided path.
238
+
By default if a file with the same name is found, it will append a timestamp to the filename, but overwrite can be forced by setting that parameter to True.
239
+
Providing a source path it will use that file's content, effectively copying the code from the source file to the newly created one.
240
+
241
+
Examples:
244
242
245
-
```Python
243
+
```py
246
244
create_sketch('my_arduino_sketch')
247
245
create_sketch('my_arduino_sketch', 'tmp')
248
246
create_sketch('main')
@@ -252,4 +250,4 @@ The method returns the Python file's full path.
0 commit comments