Anyone willing to take a look at my original code and see what I need to update or fix? I'm hoping it's something easy to a skilled eye (old formatting or calling of wrong libraries somehow). It worked prior to updating to circuit pythons latest build for my M4 Express.
Code: Select all
import board
# import busio
import digitalio
import adafruit_ssd1306
import rotaryio
import time
from adafruit_motorkit import MotorKit
import adafruit_vl6180x
# from adafruit_debouncer import Debouncer
i2c = board.I2C()
sensor = adafruit_vl6180x.VL6180X(i2c)
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
encoderB = rotaryio.IncrementalEncoder(board.D12, board.D11)
button_e = digitalio.DigitalInOut(board.D10)
button_e.direction = digitalio.Direction.INPUT
button_e.pull = digitalio.Pull.UP
encoderA = rotaryio.IncrementalEncoder(board.A4, board.A3)
button_d = digitalio.DigitalInOut(board.A2)
button_d.direction = digitalio.Direction.INPUT
button_d.pull = digitalio.Pull.UP
range_mm = sensor.range
kit = MotorKit()
buttond_was_pushed = False
buttone_was_pushed = False
current_program = "Monarch"
program_running = False
lastA_position = None
lastB_position = None
def showMainScreen():
display.fill(0)
display.text('M O N A R C H', 0, 0, 1)
display.text(' - Ind. -', 0, 10, 1)
display.text('MK. I', 95, 20, 1)
showMainScreen()
while True:
if button_d.value == 0:
# Button_d has been pressed
buttond_was_pushed = True
if button_e.value == 0:
# Button_e has been pressed
buttone_was_pushed = True
#if current_program == "Monarch" and buttond_was_pushed:
if current_program != "Fil" and buttond_was_pushed:
# We are on main screen or CIP, but want Fil.
# Go there
display.fill(0)
display.text('Mode:', 0, 0, 1)
display.text('{ OFF', 0, 10, 1)
display.text(' ON', 0, 20, 1)
display.text(' Time:', 36, 0, 1)
display.text('0.0', 102, 0, 1)
display.text(' Foam Pulse:', 36, 20, 1)
display.text('0', 116, 20, 1)
display.text('º', 32, 0, 1)
display.text('º', 32, 8, 1)
display.text('º', 32, 16, 1)
display.text('º', 32, 24, 1)
current_program = "Fil"
buttond_was_pushed = False
if current_program == "Fil" and buttond_was_pushed and not program_running:
# Button was pushed, but program not yet running.
# Make it run!
program_running = True
buttond_was_pushed = False
try:
range_mm = sensor.range
print('Range: {0}mm'.format(range_mm))
except RuntimeError:
print("retrying!")
# time.sleep(.00001)
if current_program == "Fil" and not buttond_was_pushed and program_running:
# Fill program running, but button is not pushed... now what?
positionA = encoderA.position
if lastA_position is None or positionA != lastA_position:
print(positionA)
lastA_position = positionA
timingA_value = 0.1*encoderA.position
print(timingA_value) # can remove this once verified it works
display.fill(0)
display.text('Mode:', 0, 0, 1)
display.text(' OFF', 0, 10, 1)
display.text('{ ON ', 0, 20, 1)
display.text(' Time: ', 36, 0, 1)
display.text(str(timingA_value), 102, 0, 1)
display.text('º', 32, 0, 1)
display.text('º', 32, 8, 1)
display.text('º', 32, 16, 1)
display.text('º', 32, 24, 1)
positionB = encoderB.position
if lastB_position is None or positionB != lastB_position:
print(positionB)
lastB_position = positionB
timingB_value = encoderB.position
print(timingB_value) # can remove this once verified it works
display.text(' Foam Pulse: ', 36, 20, 1)
display.text(str(timingB_value), 116, 20, 1)
if (range_mm >= 70):
kit.motor2.throttle = 1.0 # Load first can
time.sleep(.8) # Piston extension time
kit.motor2.throttle = 0 # Piston retract
time.sleep(1) # need for can to fall into chute
kit.motor2.throttle = 1.0 # Load Second can
time.sleep(.8) # Piston extension time
kit.motor2.throttle = 0 # Piston retract
time.sleep(1) # prevents lift hitting moving cans
kit.motor4.throttle = 1.0 # Drop lift piston
kit.motor1.throttle = 1.0 # Start Purge
time.sleep(1.5) # Hold for 1.5 s
kit.motor1.throttle = 0 # Stop Purge
kit.motor3.throttle = 1.0 # Start Fill
time.sleep(timingA_value) # Hold for set time
kit.motor3.throttle = 0 # End fill
for _ in range(timingB_value):
time.sleep(0.05) # Hold for set time
kit.motor3.throttle = 1.0 # Start Foam Pulse
time.sleep(0.05) # Hold for set time
kit.motor3.throttle = 0 # End Foam Pulse
kit.motor4.throttle = 0 # Raise lift
time.sleep(.75)
try:
range_mm = sensor.range
print('Range2: {0}mm'.format(range_mm))
except RuntimeError:
print("retrying!")
# time.sleep(.00001)
if current_program != "CIP" and buttone_was_pushed:
# We are on Monarch, and want CIP.
# Go there
display.fill(0)
display.text('Mode:', 0, 0, 1)
# display.text('{ OFF', 0, 10, 1)
display.text(' CIP', 0, 20, 1)
display.text(' Valves Closed', 36, 20, 1)
display.text('º', 32, 0, 1)
display.text('º', 32, 8, 1)
display.text('º', 32, 16, 1)
display.text('º', 32, 24, 1)
kit.motor4.throttle = 0.0
kit.motor3.throttle = 0.0
current_program = "CIP"
program_running = False
buttone_was_pushed = False
if current_program == "CIP" and buttone_was_pushed and not program_running:
# Button was pushed, but program not yet running.
# Make it run!
display.fill(0)
display.text('Mode:', 0, 0, 1)
# display.text('{ OFF', 0, 10, 1)
display.text(' CIP', 0, 20, 1)
display.text(' Valves Open', 36, 20, 1)
display.text('º', 32, 0, 1)
display.text('º', 32, 8, 1)
display.text('º', 32, 16, 1)
display.text('º', 32, 24, 1)
kit.motor4.throttle = 1.0
time.sleep(.5)
kit.motor3.throttle = 1.0
current_program = "CIP"
program_running = True
buttone_was_pushed = False
if current_program == "CIP" and buttone_was_pushed and program_running:
# Button was pushed, but program not yet running.
# Make it run!
display.fill(0)
display.text('Mode:', 0, 0, 1)
# display.text('{ OFF', 0, 10, 1)
display.text(' CIP', 0, 20, 1)
display.text(' Valves Closed', 36, 20, 1)
display.text('º', 32, 0, 1)
display.text('º', 32, 8, 1)
display.text('º', 32, 16, 1)
display.text('º', 32, 24, 1)
kit.motor4.throttle = 0.0
kit.motor3.throttle = 0.0
current_program = "CIP"
program_running = False
buttone_was_pushed = False
#if current_program == "CIP" and buttone_was_pushed and not program_running:
# Button was pushed, but program not yet running.
# Make it run!
#program_running = True
#buttone_was_pushed = False
if current_program == "Fil" and buttond_was_pushed and program_running:
# Turn the program back off.
current_program = "Monarch"
# buttond_was_pushed = False
program_running = False
# showMainScreen()
display.show()