/ Buckaroo_Plant_Care_Bot / code.py
code.py
1 # SPDX-FileCopyrightText: 2020 John Park for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # Bonsai Buckaroo + CLUE Plant Care Bot 6 7 import time 8 import board 9 from digitalio import DigitalInOut, Direction 10 from analogio import AnalogIn 11 from adafruit_clue import clue 12 from adafruit_display_text import label 13 import displayio 14 import terminalio 15 import pwmio 16 17 moist_level = 50 # adjust this value as needed for your plant 18 19 board.DISPLAY.brightness = 0.8 20 clue.pixel.fill(0) # turn off NeoPixel 21 22 clue_display = displayio.Group() 23 24 # draw the dry plant 25 dry_plant_file = open("dry.bmp", "rb") 26 dry_plant_bmp = displayio.OnDiskBitmap(dry_plant_file) 27 # CircuitPython 6 & 7 compatible 28 dry_plant_sprite = displayio.TileGrid( 29 dry_plant_bmp, 30 pixel_shader=getattr(dry_plant_bmp, "pixel_shader", displayio.ColorConverter()), 31 ) 32 # CircuitPython 7 compatible 33 # dry_plant_sprite = displayio.TileGrid( 34 # dry_plant_bmp, pixel_shader=dry_plant_bmp.pixel_shader 35 # ) 36 clue_display.append(dry_plant_sprite) 37 38 # draw the happy plant on top (so it can be moved out of the way when needed) 39 happy_plant_file = open("happy.bmp", "rb") 40 happy_plant_bmp = displayio.OnDiskBitmap(happy_plant_file) 41 # CircuitPython 6 & 7 compatible 42 happy_plant_sprite = displayio.TileGrid( 43 happy_plant_bmp, 44 pixel_shader=getattr(happy_plant_bmp, "pixel_shader", displayio.ColorConverter()), 45 ) 46 # CircuitPython 7 compatible 47 # happy_plant_sprite = displayio.TileGrid( 48 # happy_plant_bmp, pixel_shader=happy_plant_bmp.pixel_shader 49 # ) 50 clue_display.append(happy_plant_sprite) 51 52 # Create text 53 # first create the group 54 text_group = displayio.Group(scale=3) 55 # Make a label 56 title_label = label.Label(terminalio.FONT, text="CLUE Plant", color=0x00FF22) 57 # Position the label 58 title_label.x = 10 59 title_label.y = 4 60 # Append label to group 61 text_group.append(title_label) 62 63 soil_label = label.Label(terminalio.FONT, text="Soil: ", color=0xFFAA88) 64 soil_label.x = 4 65 soil_label.y = 64 66 text_group.append(soil_label) 67 68 motor_label = label.Label(terminalio.FONT, text="Motor off", color=0xFF0000) 69 motor_label.x = 4 70 motor_label.y = 74 71 text_group.append(motor_label) 72 73 clue_display.append(text_group) 74 board.DISPLAY.show(clue_display) 75 76 motor = DigitalInOut(board.P2) 77 motor.direction = Direction.OUTPUT 78 79 buzzer = pwmio.PWMOut(board.SPEAKER, variable_frequency=True) 80 buzzer.frequency = 1000 81 82 sense_pin = board.P1 83 analog = AnalogIn(board.P1) 84 85 86 def read_and_average(ain, times, wait): 87 asum = 0 88 for _ in range(times): 89 asum += ain.value 90 time.sleep(wait) 91 return asum / times 92 93 94 time.sleep(5) 95 96 while True: 97 # take 100 readings and average them 98 aval = read_and_average(analog, 100, 0.01) 99 # calculate a percentage (aval ranges from 0 to 65535) 100 aperc = aval / 65535 * 100 101 # display the percentage 102 soil_label.text = "Soil: {} %".format(int(aperc)) 103 print((aval, aperc)) 104 105 if aperc < moist_level: 106 happy_plant_sprite.x = 300 # move the happy sprite away 107 time.sleep(1) 108 motor.value = True 109 motor_label.text = "Motor ON" 110 motor_label.color = 0x00FF00 111 buzzer.duty_cycle = 2 ** 15 112 time.sleep(0.5) 113 114 # always turn off quickly 115 motor.value = False 116 motor_label.text = "Motor off" 117 motor_label.color = 0xFF0000 118 buzzer.duty_cycle = 0 119 120 if aperc >= moist_level: 121 happy_plant_sprite.x = 0 # bring back the happy sprite