So I ended up porting the plotter sketch to CLUE plant to get some amount of history. Code is below.
Main benefits, each pixel (column) represents a minute of time, so I can see over time how the moisture is detected, first plot. I also added a/b buttons to raise/lower the target percentage on device, which shows up as a second plot. The third plot just goes high/low depending if the motor ever turned on during that period.
I'm noticing these odd "humidity spikes". I'm curious if anyone has a theory why this might happen? I'm not adding water, the motor isn't turning on at all, and they last minutes at a time before they go down.
Code: Select all
import time
import board
import digitalio
import analogio
from adafruit_clue import clue
from plotter import Plotter
# Motor setup
motor = digitalio.DigitalInOut(board.P2)
motor.direction = digitalio.Direction.OUTPUT
# Soil sense setup
analog = analogio.AnalogIn(board.P1)
times = 100
last_read = 0
analog_sum = 0
analog_value = 65535
changed = False
last_motor = 0
percentage = 100
target = 50
button_up = True
plotter = Plotter(board.DISPLAY, scroll_px=1, mu_output=True)
plotter.display_on()
plotter.title = "CLUE plant"
plotter.y_range = (0, 100)
plotter.y_full_range = (0, 100)
plotter.y_min_range = 20
plotter.channels = 3
plotter.channel_colidx = (5, 3, 7)
plotter.change_stylemode(style="lines", mode="scroll")
motor_was_on = False
PLOT_RATE = 60
last_plot = 0
while True:
if time.monotonic() - last_read > 0.01:
last_read = time.monotonic()
analog_sum += analog.value
times -= 1
if times == 0:
analog_value = analog_sum / 100
times = 100
analog_sum = 0
changed = True
if changed:
changed = False
# Calculate a percentage (analog_value ranges from 0 to 65535)
percentage = analog_value / 65535 * 100
# Display the percentage
if time.monotonic() - last_plot > PLOT_RATE:
last_plot = time.monotonic()
plotter.data_add((percentage, target, target if motor_was_on else 0))
motor_was_on = False
if time.monotonic() - last_motor > 0.5:
if not motor.value and percentage < target:
last_motor = time.monotonic()
motor.value = True
motor_was_on = True
else:
last_motor = time.monotonic() + 5 # extra off time
motor.value = False
if button_up and clue.button_a and target > 10:
target -= 5
button_up = False
if button_up and clue.button_b and target < 90:
target += 5
button_up = False
if not button_up and not clue.button_a and not clue.button_b:
button_up = True