Repeating Statements with a Counter Arduino and MicroPython

MechatronicsLAB
2 min readAug 27, 2024

In this section, we explore the for loop, a fundamental construct in programming for repeating statements a specified number of times. The for loop provides precise control over initialization, condition testing, and iteration, making it ideal for tasks where you need to execute a block of code iteratively with a counter.

What We Will Learn in This Section:

  • How to use the for loop to iterate through a sequence of instructions based on a counter.
  • Different forms of for loops, including variations in initialization, condition testing, and iteration.
  • Practical applications of for loops in ESP32 projects, such as LED blinking sequences and sensor data processing.

Why Is This Lesson Important to You?

Mastering the for loop is essential for efficiently handling repetitive tasks in your ESP32 projects. Whether you’re implementing timed sequences, iterating over arrays, or performing iterative calculations, the for loop offers precise control and clarity in code structure.

Components List:

Arduino and MicroPython for ESP32" is your go-to guide for mastering ESP32 projects with clear examples and practical code. For free reading, visit Mechatronics Lab and grab your copy here

Code Arduino:

void setup() {
Serial.begin(9600);
}
void loop(){
Serial.println("for(int i=0; i < 4; i++)");
for(int i=0; i < 4; i++)
{
Serial.println(i);
}
delay(1000);
}

Here’s the explanation of the Arduino code:

1. setup() Function

void setup() {
Serial.begin(9600);
}
  • Serial.begin(9600);: Initializes the serial communication at a baud rate of 9600. This allows the Arduino to send data to the serial monitor on your computer.

2. loop() Function

void loop(){
Serial.println("for(int i=0; i < 4; i++)");
for(int i=0; i < 4; i++)
{
Serial.println(i);
}
delay(1000);
}
  • Serial.println(“for(int i=0; i < 4; i++)”);: Sends the string “for(int i=0; i < 4; i++)” to the serial monitor. This text is displayed to indicate the start of the for loop.
  • for(int i=0; i < 4; i++): A for loop that runs four times, with i taking values from 0 to 3.
  • Serial.println(i);: Prints the current value of i to the serial monitor in each iteration of the loop.
  • delay(1000);: Pauses the execution for 1 second (1000 milliseconds) before repeating the loop() function.

MicroPython

import time
for i in range(4):
print(i)
time.sleep(1)

Here’s the explanation of the MicroPython code:

1. for Loop

for i in range(4):
print(i)
time.sleep(1)

for i in range(4):: A for loop that iterates four times, with i taking values from 0 to 3.

print(i): Prints the current value of i to the console in each iteration of the loop.

time.sleep(1): Pauses the execution for 1 second before moving to the next iteration of the loop.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

MechatronicsLAB
MechatronicsLAB

Written by MechatronicsLAB

0 Followers

Our main focus will be on Mechatronics Technology where we will work in #Industrial_Automation, #PLC, #IoT, #Raspberry_Pi, #Arduino, #Microcontroller

No responses yet

Write a response