from machine import Pin, ADC import time # Set up the ADC pin for moisture reading moisture_sensor = ADC(Pin(34)) moisture_sensor.atten(ADC.ATTN_11DB) # Set attenuation for full range (0-3.3V) # Set up LEDs on pins 25, 26, and 27 green_led = Pin(25, Pin.OUT) yellow_led = Pin(26, Pin.OUT) red_led = Pin(27, Pin.OUT) # Set up the button on GPIO13, with an internal pull-up resistor button = Pin(13, Pin.IN, Pin.PULL_UP) running = True # Flag to control the loop # Function to check if the button is pressed to stop the loop def check_button(): global running if button.value() == 0: # If button is pressed (value will be 0) running = False # Set running to False to stop the loop # Function to update LED states based on the moisture level def update_leds(moisture_value): if moisture_value > 1000: green_led.off() yellow_led.off() red_led.on() # Turn on Red LED for values > 1000 elif 700 <= moisture_value <= 999: green_led.off() yellow_led.on() # Turn on Yellow LED for values between 700 and 999 red_led.off() elif 400 <= moisture_value <= 699: green_led.on() # Turn on Green LED for values between 400 and 699 yellow_led.off() red_led.off() else: # Turn on all LEDs for values between 0 and 399 green_led.on() yellow_led.on() red_led.on() while running: moisture_value = moisture_sensor.read() # Read the analog value print("Moisture Level:", moisture_value) # Update the LEDs based on the current moisture value update_leds(moisture_value) # Check the button state inside the loop to stop if pressed check_button() time.sleep(0.1) # Short delay for readings print("Measurement stopped by button.")