Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Mechatronics LAB
Published © GPL3+

Structuring Code into Functional Blocks with Functions

This section explores the fundamental concept of functions in programming with the ESP32 using Arduino and MicroPython. Functions are essent

BeginnerProtip1 hour47
Structuring Code into Functional Blocks with Functions

Things used in this project

Hardware components

ESP32 Development Board
×1
LED (for practical demonstrations)
×1
Resistors (as needed)
×1
Push-button Switch (optional)
×1
Breadboard
×1

Story

Read more

Schematics

2_10_structuring_code_into_functional_blocks_with_functions_1Eg9chd4un.png

Code

Arduino code

Arduino
const int LED_BUILTIN = 13;  // Built-in LED pin on ESP32 (adjust if needed)
const int inputPin = 17;     // Define the input pin connected to a button or sensor

// Blink an LED once
void blink1() {
    digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
    delay(500);                      // wait 500 milliseconds
    digitalWrite(LED_BUILTIN, LOW);  // turn the LED off
    delay(500);                      // wait 500 milliseconds
}

// Blink an LED the number of times given in the count parameter
void blink2(int count) {
    while(count > 0) {
        digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
        delay(500);                      // wait 500 milliseconds
        digitalWrite(LED_BUILTIN, LOW);  // turn the LED off
        delay(500);                      // wait 500 milliseconds
        count--;                         // decrement count
    }
}

// Blink an LED using the given delay period
// Return the number of times the LED flashed
int blink3(int period) {
    int blinkCount = 0;
    Serial.print("Input pin state: ");
    Serial.println(digitalRead(inputPin));  // Debugging line to check pin state
    while(digitalRead(inputPin) == LOW) {   // Change to LOW for pull-up
        digitalWrite(LED_BUILTIN, HIGH);
        delay(period);
        digitalWrite(LED_BUILTIN, LOW);
        delay(period);
        blinkCount++;  // increment the count
    }
    return blinkCount;  // return the count value
}

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(inputPin, INPUT_PULLUP);  // Use internal pull-up resistor
    Serial.begin(9600);
    // Additional setup code as needed
}

void loop() {
    // Example usage of blink functions
    blink1();
    delay(1000);  // wait 1 second between blinks
    int count = blink3(250); // blink the LED 250 ms on and 250 ms off
    Serial.print("Blink count: ");
    Serial.println(count);
    delay(2000);  // wait 2 seconds before repeating
}

Micropython Code

Python
from machine import Pin  # Import the Pin class from the machine module to control the pins
import time              # Import the time module for delays

LED_BUILTIN = Pin(2, Pin.OUT)  # Define the built-in LED pin (GPIO 2 on ESP32) and set it as an output
inputPin = Pin(17, Pin.IN, Pin.PULL_UP)  # Define the input pin connected to a button or sensor and enable the internal pull-up resistor

# Function to blink the LED once
def blink1():
    LED_BUILTIN.value(1)  # Turn the LED on
    time.sleep(0.5)       # Wait for 500 milliseconds
    LED_BUILTIN.value(0)  # Turn the LED off
    time.sleep(0.5)       # Wait for another 500 milliseconds

# Function to blink the LED a specified number of times
def blink2(count):
    while count > 0:             # Loop until the count reaches 0
        LED_BUILTIN.value(1)     # Turn the LED on
        time.sleep(0.5)          # Wait for 500 milliseconds
        LED_BUILTIN.value(0)     # Turn the LED off
        time.sleep(0.5)          # Wait for another 500 milliseconds
        count -= 1               # Decrement the count

# Function to blink the LED with a specified delay period and return the number of blinks
def blink3(period):
    blink_count = 0  # Initialize the blink counter
    print("Input pin state: ", inputPin.value())  # Print the current state of the input pin for debugging
    while inputPin.value() == 0:  # Loop while the input pin is LOW (button pressed)
        LED_BUILTIN.value(1)               # Turn the LED on
        time.sleep(period / 1000.0)        # Wait for the specified period (converted to seconds)
        LED_BUILTIN.value(0)               # Turn the LED off
        time.sleep(period / 1000.0)        # Wait for the specified period (converted to seconds)
        blink_count += 1                   # Increment the blink counter
    return blink_count  # Return the total number of blinks

# Main loop to use the blink functions
while True:
    blink1()                  # Call the blink1 function to blink the LED once
    time.sleep(1)             # Wait for 1 second between blinks
    count = blink3(250)       # Call the blink3 function to blink the LED with 250 ms on and 250 ms off
    print("Blink count: ", count)  # Print the number of blinks
    time.sleep(2)             # Wait for 2 seconds before repeating the loop

Credits

Mechatronics LAB
75 projects • 47 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .
Contact

Comments

Please log in or sign up to comment.