# Import necessary modules from MicroPython
from machine import Pin
import time
# Define the GPIO pins for the 8 LEDs
# These are arranged in the order they will be cycled through.
# MicroPython uses the actual GPIO number directly.
LED_PINS = [2, 4, 5, 13, 14, 16, 17, 18]
NUM_LEDS = len(LED_PINS) # Calculate number of LEDs
# Define the GPIO pins for the buttons
GREEN_BUTTON_PIN = 34 # Button to turn on/advance LED
RED_BUTTON_PIN = 35 # Button to turn off/revert LED
# GPIO 36 and 39 are also provided by the user, but not assigned specific functionality in this code.
# BUTTON_3_PIN = 36
# BUTTON_4_PIN = 39
# --- Counter Variable ---
# This counter will keep track of how many LEDs are currently lit.
# Its value will range from 0 (all off) to NUM_LEDS (8, all on).
# It also serves as the 0-based index for the *next* LED to be turned ON
# by the green button.
current_led_counter = 0 # Starts at 0, meaning no LEDs are currently lit
# --- Button Debouncing Variables ---
# Debouncing helps prevent multiple rapid readings from a single button press.
DEBOUNCE_DELAY_MS = 50 # milliseconds
# Convert debounce delay to seconds for time.ticks_ms()
DEBOUNCE_DELAY_TICKS = DEBOUNCE_DELAY_MS
# Variables for the green button
# Initialize button state (assuming button is not pressed at start, i.e., HIGH due to PULL_UP)
green_button_state = 1 # 1 for HIGH (released), 0 for LOW (pressed)
last_green_button_state = 1
last_green_debounce_time = 0
# Variables for the red button
# Initialize button state (assuming button is not pressed at start, i.e., HIGH due to PULL_UP)
red_button_state = 1 # 1 for HIGH (released), 0 for LOW (pressed)
last_red_button_state = 1
last_red_debounce_time = 0
# --- Pin Objects ---
# Create Pin objects for LEDs
led_objects = []
for pin_num in LED_PINS:
led = Pin(pin_num, Pin.OUT) # Set pin as an output
led.value(0) # Turn off LED initially (0 for LOW)
led_objects.append(led)
# Create Pin objects for buttons
# Pin.IN sets it as an input, Pin.PULL_UP enables the internal pull-up resistor
green_button = Pin(GREEN_BUTTON_PIN, Pin.IN, Pin.PULL_UP)
red_button = Pin(RED_BUTTON_PIN, Pin.IN, Pin.PULL_UP)
# If using other buttons:
# button_3 = Pin(BUTTON_3_PIN, Pin.IN, Pin.PULL_UP)
# button_4 = Pin(BUTTON_4_PIN, Pin.IN, Pin.PULL_UP)
# --- Setup equivalent ---
# In MicroPython, setup tasks are usually placed directly at the top level
# of the script or within a main function that is called at the end.
print("ESP32 LED Control with Counter Starting (MicroPython)...")
print(f"Initial Number of LEDs ON: {current_led_counter}")
# --- Main loop equivalent ---
# MicroPython scripts typically run in a continuous loop.
while True:
# --- Green Button Logic ---
reading_green = green_button.value() # Read the raw value of the button pin
# Check for state change to start debounce timer
if reading_green != last_green_button_state:
last_green_debounce_time = time.ticks_ms() # Get current time in milliseconds
# If the button state has been stable for longer than the debounce delay
if (time.ticks_ms() - last_green_debounce_time) > DEBOUNCE_DELAY_TICKS:
# Only act if the button state has actually changed and is now pressed (0 for LOW)
if reading_green != green_button_state:
green_button_state = reading_green
if green_button_state == 0: # Green Button is pressed (LOW)
print("Green button pressed!")
# Increment the counter and turn on an LED if not all are already ON
if current_led_counter < NUM_LEDS:
led_objects[current_led_counter].value(1) # Turn on the LED (1 for HIGH)
print(f"LED Number {current_led_counter + 1} (GPIO {LED_PINS[current_led_counter]}) is now ON.")
current_led_counter += 1 # Increment the counter
else:
print("All LEDs are already ON. Cannot turn on more.")
print(f"Total LEDs ON: {current_led_counter}")
last_green_button_state = reading_green # Save the reading for the next iteration
# --- Red Button Logic ---
reading_red = red_button.value() # Read the raw value of the button pin
# Check for state change to start debounce timer
if reading_red != last_red_button_state:
last_red_debounce_time = time.ticks_ms() # Get current time in milliseconds
# If the button state has been stable for longer than the debounce delay
if (time.ticks_ms() - last_red_debounce_time) > DEBOUNCE_DELAY_TICKS:
# Only act if the button state has actually changed and is now pressed (0 for LOW)
if reading_red != red_button_state:
red_button_state = reading_red
if red_button_state == 0: # Red Button is pressed (LOW)
print("Red button pressed!")
# Decrement the counter and turn off an LED if not all are already OFF
if current_led_counter > 0:
current_led_counter -= 1 # Decrement the counter
led_objects[current_led_counter].value(0) # Turn off the LED (0 for LOW)
print(f"LED Number {current_led_counter + 1} (GPIO {LED_PINS[current_led_counter]}) is now OFF.")
else:
print("No LEDs are currently ON to turn off.")
print(f"Total LEDs ON: {current_led_counter}")
last_red_button_state = reading_red # Save the reading for the next iteration
# A small delay to keep the loop from running too fast, though debounce helps with this
time.sleep_ms(10) # MicroPython equivalent of delay() in milliseconds