|
| 1 | +import time |
| 2 | +import board |
| 3 | +import digitalio |
| 4 | +import analogio |
| 5 | +from adafruit_clue import clue |
| 6 | + |
| 7 | +# Turn off the NeoPixel |
| 8 | +clue.pixel.fill(0) |
| 9 | + |
| 10 | +# Motor setup |
| 11 | +motor = digitalio.DigitalInOut(board.P2) |
| 12 | +motor.direction = digitalio.Direction.OUTPUT |
| 13 | + |
| 14 | +# Soil sense setup |
| 15 | +analog = analogio.AnalogIn(board.P1) |
| 16 | + |
| 17 | +def read_and_average(analog_in, times, wait): |
| 18 | + analog_sum = 0 |
| 19 | + for _ in range(times): |
| 20 | + analog_sum += analog_in.value |
| 21 | + time.sleep(wait) |
| 22 | + return analog_sum / times |
| 23 | + |
| 24 | +clue_display = clue.simple_text_display(title=" CLUE Plant", title_scale=1, text_scale=3) |
| 25 | +clue_display.show() |
| 26 | + |
| 27 | +while True: |
| 28 | + # Take 100 readings and average them |
| 29 | + analog_value = read_and_average(analog, 100, 0.01) |
| 30 | + # Calculate a percentage (analog_value ranges from 0 to 65535) |
| 31 | + percentage = analog_value / 65535 * 100 |
| 32 | + # Display the percentage |
| 33 | + clue_display[0].text = "Soil: {} %".format(int(percentage)) |
| 34 | + # Print the values to the serial console |
| 35 | + print((analog_value, percentage)) |
| 36 | + |
| 37 | + if percentage < 50: |
| 38 | + motor.value = True |
| 39 | + clue_display[1].text = "Motor ON" |
| 40 | + clue_display[1].color = (0, 255, 0) |
| 41 | + time.sleep(0.5) |
| 42 | + |
| 43 | + # always turn off quickly |
| 44 | + motor.value = False |
| 45 | + clue_display[1].text = "Motor OFF" |
| 46 | + clue_display[1].color = (255, 0, 0) |
0 commit comments