|
| 1 | +--- |
| 2 | + |
| 3 | +featured: micropython-101 |
| 4 | +title: '2. Micropython - Digital I/O' |
| 5 | +description: 'Learn the basics for digital IO ports on MicroPython.' |
| 6 | +author: 'Pedro Lima' |
| 7 | +hero_image: "./hero-banner.png" |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +Loops are fundamental constructs in programming that allow you to execute a block of code multiple times. In MicroPython, loops help you perform repetitive tasks efficiently and are an awesome tool to keep in your coder's toolbox. In this article, we will explore the different loop structures available. |
| 12 | + |
| 13 | +## Loop Structures in MicroPython |
| 14 | + |
| 15 | +MicroPython supports two primary loop structures: |
| 16 | + |
| 17 | +- **`for` loops**: Iterate over a sequence (like a list, tuple, or string). |
| 18 | +- **`while` loops**: Continue executing as long as a condition is true. |
| 19 | + |
| 20 | +Let's delve into each of these with examples. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +## Using a `for` Loop |
| 25 | + |
| 26 | +The `for` loop is used for iterating over a sequence. It automatically retrieves each item in the sequence one after another. |
| 27 | + |
| 28 | +### Syntax |
| 29 | + |
| 30 | +```python |
| 31 | +for variable in sequence: |
| 32 | + # Code block to execute |
| 33 | +``` |
| 34 | + |
| 35 | +- **`for`**: Keyword that starts the loop. |
| 36 | +- **`variable`**: Takes the value of each item in the sequence during iteration this is where you will get the value for each iteration of a collection. |
| 37 | +- **`in`**: Keyword used to specify the sequence to iterate over. |
| 38 | +- **`sequence`**: The collection (like a list, tuple, or string) over which the loop iterates. |
| 39 | +- **Code block**: The indented block of code that runs on each iteration. |
| 40 | + |
| 41 | +### Example: Iterating Over "Arduino" with a `for` Loop |
| 42 | + |
| 43 | +```python |
| 44 | +import time |
| 45 | + |
| 46 | +cycle = 1 |
| 47 | +for letter in "Arduino": |
| 48 | + print(f"{cycle} - {letter} - printed with for loop") |
| 49 | + cycle += 1 |
| 50 | + time.sleep(3) |
| 51 | +``` |
| 52 | + |
| 53 | +**Explanation:** |
| 54 | + |
| 55 | +- **Import `time` Module**: We import the `time` module to use the `sleep()` function for delays. |
| 56 | +- **Initialize `cycle` Variable**: We start a `cycle` counter at 1. |
| 57 | +- **`for letter in "Arduino"`**: The loop iterates over each character in the string `"Arduino"`, assigning each character to the variable `letter`. |
| 58 | +- **Print Statement**: Outputs the cycle number, the current letter, and mentions that it's printed with a `for` loop. |
| 59 | +- **Increment `cycle`**: Increases the cycle counter by 1 after each iteration. |
| 60 | +- **`time.sleep(3)`**: Pauses the program for 3 seconds before the next iteration. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +## Using a `while` Loop |
| 65 | + |
| 66 | +A `while` loop continues to execute as long as a specified condition is true. |
| 67 | + |
| 68 | +### Syntax of a `while` Loop |
| 69 | + |
| 70 | +```python |
| 71 | +while condition: |
| 72 | + # Code block to execute |
| 73 | +``` |
| 74 | + |
| 75 | +- **`while`**: Keyword that starts the loop. |
| 76 | +- **`condition`**: A boolean expression evaluated before each iteration; if `True`, the loop continues. |
| 77 | +- **Code block**: The indented block of code that runs on each iteration. |
| 78 | + |
| 79 | +### Example: Iterating Over "Arduino" with a `while` Loop |
| 80 | + |
| 81 | +```python |
| 82 | +import time |
| 83 | + |
| 84 | +word = "Arduino" |
| 85 | +index = 0 |
| 86 | +cycle = 1 |
| 87 | + |
| 88 | +while index < len(word): |
| 89 | + letter = word[index] |
| 90 | + print(f"{cycle} - {letter} - printed with while loop") |
| 91 | + index += 1 |
| 92 | + cycle += 1 |
| 93 | + time.sleep(3) |
| 94 | +``` |
| 95 | + |
| 96 | +**Explanation:** |
| 97 | + |
| 98 | +- **Initialize Variables**: |
| 99 | + - `word`: The string we're iterating over. |
| 100 | + - `index`: Starts at 0, used to access each character in `word`. |
| 101 | + - `cycle`: Counts the number of iterations. |
| 102 | +- **`while index < len(word)`**: The loop continues as long as `index` is less than the length of `word`. |
| 103 | +- **Retrieve Letter**: `letter = word[index]` gets the character at the current index. |
| 104 | +- **Print Statement**: Outputs the cycle number, the current letter, and mentions that it's printed with a `while` loop. |
| 105 | +- **Increment Counters**: Increases `index` and `cycle` by 1. |
| 106 | +- **`time.sleep(3)`**: Pauses the program for 3 seconds before the next iteration. |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +## Conclusion |
| 113 | + |
| 114 | +Loops are essential for automating repetitive tasks in MicroPython. Understanding how to use different loop structures allows you to write more efficient and effective code. In these examples, we've demonstrated how to iterate over the string "Arduino" using various loop methods, printing each letter with a delay to observe the output in real time. |
| 115 | + |
| 116 | +**Try Modifying the Examples** |
| 117 | + |
| 118 | +- **Different Strings**: Replace `"Arduino"` with another word to see how the loops handle different sequences. |
| 119 | +- **Additional Information**: Include more details in the print statement, such as the ASCII value of each letter. |
0 commit comments