0

Broke my old code after update, any idea what I missed?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Broke my old code after update, any idea what I missed?

by burrito_poots on Thu May 13, 2021 4:44 pm

Hey gang, its been 2019 since I last coded this board. I updated because I'm trying to get my larger OLED feather (the 128 x 64 one) to work. I tried the example provided here: https://learn.adafruit.com/adafruit-128 ... cuitpython to no luck. Tried going back to my original code, also no luck. It's been so long I'm basically starting form scratch again with a lot of this.

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 | TOGGLE FULL SIZE
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()


burrito_poots
 
Posts: 125
Joined: Thu May 30, 2019 5:02 pm

Re: Broke my old code after update, any idea what I missed?

by mikeysklar on Thu May 13, 2021 6:39 pm

@burrito_poots,

Let's get your bootloader, CircuitPython release and libraries current if they are not already. Then go back to the example code you tried for the 128x64 OLED and get that running. It sounds like you are running CircuitPython 6.2 what else did you update?

https://circuitpython.org/board/feather_m4_express/

mikeysklar
 
Posts: 5767
Joined: Mon Aug 01, 2016 8:10 pm

Re: Broke my old code after update, any idea what I missed?

by burrito_poots on Fri May 14, 2021 5:49 pm

mikeysklar wrote:@burrito_poots,

Let's get your bootloader, CircuitPython release and libraries current if they are not already. Then go back to the example code you tried for the 128x64 OLED and get that running. It sounds like you are running CircuitPython 6.2 what else did you update?

https://circuitpython.org/board/feather_m4_express/


Updated the boot loader and the board to the latest stable release for it as well so those bases are covered!

Now trying to go about updating all of my libraries. I'll reconfirm here since I asked on discord but, I assumed originally I could just only put the libraries on my board that are specifically called out in my code -- that doesn't appear to be the case though because some libraries may use/reference others? I'm not even sure where to start here on which to drag over/ which to not, as there are a ton to comb through -- is there any rule of thumb here or?

burrito_poots
 
Posts: 125
Joined: Thu May 30, 2019 5:02 pm

Re: Broke my old code after update, any idea what I missed?

by burrito_poots on Fri May 14, 2021 9:04 pm

mikeysklar wrote:@burrito_poots,

Let's get your bootloader, CircuitPython release and libraries current if they are not already. Then go back to the example code you tried for the 128x64 OLED and get that running. It sounds like you are running CircuitPython 6.2 what else did you update?

https://circuitpython.org/board/feather_m4_express/


Alright, so I updated some of my libraries but I'm not sure if all are updated yet that need it or not, this is what I'm getting:\
Screen Shot 2021-05-14 at 7.58.11 PM.png
Screen Shot 2021-05-14 at 7.58.11 PM.png (159.07 KiB) Viewed 361 times


Here's my current code as is:

Code: Select all | TOGGLE FULL SIZE
import board
import displayio
import terminalio
# import busio
import digitalio
# import adafruit_ssd1306
import adafruit_displayio_sh1107

displayio.release_displays()
# oled_reset = board.D9

# Use for I2C
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)

# SH1107 is vertically oriented 64x128
WIDTH = 128
HEIGHT = 64
BORDER = 2

# display = adafruit_displayio_sh1107.SH1107(display_bus, width=WIDTH, height=HEIGHT)

import rotaryio
import time
from adafruit_motorkit import MotorKit
import adafruit_vl6180x
# from adafruit_debouncer import Debouncer

sensor = adafruit_vl6180x.VL6180X(i2c)
# display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
display = adafruit_displayio_sh1107.SH1107(display_bus, width=WIDTH, height=HEIGHT)



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()

burrito_poots
 
Posts: 125
Joined: Thu May 30, 2019 5:02 pm

Re: Broke my old code after update, any idea what I missed?

by burrito_poots on Sun May 16, 2021 6:13 pm

Alright so one of my libraries was out of date, now getting a new error and still slightly confused but would love some eyeballs on this who may be able to help :)

Heres the error I'm getting:
Screen Shot 2021-05-16 at 5.03.00 PM.png
Screen Shot 2021-05-16 at 5.03.00 PM.png (59.71 KiB) Viewed 340 times


It at least seems simple, but I'm so illiterate here at this I'm still unsure how to fix it. Any help would be appreciated!

burrito_poots
 
Posts: 125
Joined: Thu May 30, 2019 5:02 pm

Re: Broke my old code after update, any idea what I missed?

by mikeysklar on Mon May 17, 2021 8:20 pm

Good job getting going with the necessary libraries.

You will now need to update your code.

The adafruit_displayio_sh1107 library no longer accepts "fill". I would comment out that line in the showMainScreen method.

Code: Select all | TOGGLE FULL SIZE
display.fill(0)

mikeysklar
 
Posts: 5767
Joined: Mon Aug 01, 2016 8:10 pm

Re: Broke my old code after update, any idea what I missed?

by burrito_poots on Tue Jun 01, 2021 4:43 pm

Alrighty, so here's my current issue after semi-wrapping my brain around how the new displayio stuff works: as I'm updating my code to replace all my old "text label UI", I've tried just simply updating my main screen that shows and waits until you hit a button before moving on. In the code below, its lines 59 through 70 (starting with "def showMainScreen():") that is basically the first screen you see:

Code: Select all | TOGGLE FULL SIZE
import board
import displayio
import terminalio
import busio
import digitalio
import adafruit_displayio_sh1107

from adafruit_display_text import label
import adafruit_displayio_sh1107

displayio.release_displays()
# old_reset = board.D9

# Use for I2C
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)

# SH1107 is vertically oriented 64x128
WIDTH = 128
HEIGHT = 64
BORDER = 2

display = adafruit_displayio_sh1107.SH1107(display_bus, width=WIDTH, height=HEIGHT)

# Make the display context
splash = displayio.Group(max_size=10)
display.show(splash)

import rotaryio
import time
from adafruit_motorkit import MotorKit
import adafruit_vl6180x
# from adafruit_debouncer import Debouncer

sensor = adafruit_vl6180x.VL6180X(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():
    text1 = "monarch"
    text_area = label.Label(terminalio.FONT, text=text1, scale=3, color=0xFFFFFF, x=3, y=12)
    splash.append(text_area)
    text2 = "FLUID SYSTEMS"
    text_area2 = label.Label(terminalio.FONT, text=text2, scale=1, color=0xFFFFFF, x=28, y=36)
    splash.append(text_area2)
    text3 = "nano MK.I"
    text_area3 = label.Label(terminalio.FONT, text=text3, scale=1, color=0xFFFFFF, x=70, y=55)
    splash.append(text_area3)

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()


The problem is, when I load in the above code, this little pseudo loading screen appears then disappears, not waiting on a button push to move on -- any idea why? I'm also getting this error code for the whole program but I'm not sure as to what this is referring to:
"main.py output:
Range: 4mm
Range2: 6mm
Traceback (most recent call last):
File "main.py", line 242, in <module>
TypeError: function takes 2 positional arguments but 1 were given

Code done running.

Press any key to enter the REPL. Use CTRL-D to reload
."

burrito_poots
 
Posts: 125
Joined: Thu May 30, 2019 5:02 pm

Re: Broke my old code after update, any idea what I missed?

by mikeysklar on Wed Jun 02, 2021 11:53 am

I think you might want to turn off "auto_refesh" and switch to manual which should avoid any updates to the display until your button press is detected. Some examples here:

Code: Select all | TOGGLE FULL SIZE
display = ST7789(display_bus, width=240, height=240, rowstart=80, auto_refresh=False)


Code: Select all | TOGGLE FULL SIZE
display.auto_refresh = False


Code: Select all | TOGGLE FULL SIZE
display.refresh()


https://learn.adafruit.com/circuitpytho ... al-refresh

The python error you are seeing might be related to the graphic images popping up on the display. I think a python snake logo comes up along with the REPL text you provided.

I'm not sure why you are getting the two arguments expected error here on the last line of the script which is calling display.show(). It must be something about how text_area is being populated. Possibly a missing comment or argument on one of the *text* setup lines.

mikeysklar
 
Posts: 5767
Joined: Mon Aug 01, 2016 8:10 pm

Re: Broke my old code after update, any idea what I missed?

by burrito_poots on Wed Jun 02, 2021 3:21 pm

Alright, here's how I implemented this into my code, as of currently I still run into the same issue (specifically lines 60 and lines 82):

Code: Select all | TOGGLE FULL SIZE
import board
import displayio
import terminalio
import busio
import digitalio
import adafruit_displayio_sh1107

from adafruit_display_text import label
import adafruit_displayio_sh1107

displayio.release_displays()
# old_reset = board.D9

# Use for I2C
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)

# SH1107 is vertically oriented 64x128
WIDTH = 128
HEIGHT = 64
BORDER = 2

display = adafruit_displayio_sh1107.SH1107(display_bus, width=WIDTH, height=HEIGHT)

# Make the display context
splash = displayio.Group(max_size=10)
display.show(splash)

import rotaryio
import time
from adafruit_motorkit import MotorKit
import adafruit_vl6180x
# from adafruit_debouncer import Debouncer

sensor = adafruit_vl6180x.VL6180X(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

# turn off auto refresh
display.auto_refresh = False

#
# your code here that changes the display
#

# when ready to show results, call refresh()
#display.refresh()

def showMainScreen():
    text1 = "monarch"
    text_area = label.Label(terminalio.FONT, text=text1, scale=3, color=0xFFFFFF, x=3, y=12)
    splash.append(text_area)
    text2 = "FLUID SYSTEMS"
    text_area2 = label.Label(terminalio.FONT, text=text2, scale=1, color=0xFFFFFF, x=28, y=36)
    splash.append(text_area2)
    text3 = "nano MK.I"
    text_area3 = label.Label(terminalio.FONT, text=text3, scale=1, color=0xFFFFFF, x=70, y=55)
    splash.append(text_area3)

showMainScreen()

display.refresh()

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()


Is the display.show() the old way of doing display.refrehs()? I wrote this code prior to displayio being fully implemented back on like CircuitPython 4.something so I imagine a lot of things are outdated and that's most of my issues here. But to also comment, yes, when my code runs, the display flashes that then goes to displaying "press any key to enter the REPL..." along with the Python logo on my actual OLED display.

Error code I'm currently getting:

Traceback (most recent call last):
File "main.py", line 254, in <module>
TypeError: function takes 2 positional arguments but 1 were given

Code done running.

Press any key to enter the REPL. Use CTRL-D to reload.

burrito_poots
 
Posts: 125
Joined: Thu May 30, 2019 5:02 pm

Re: Broke my old code after update, any idea what I missed?

by burrito_poots on Wed Jun 02, 2021 7:42 pm

Alright, SO, slowly but surely getting there but I have some questions.

The new displayio stuff is a huge learning curve for me. The old code is hard to translate over. As of right now, this is my code:

Code: Select all | TOGGLE FULL SIZE
import board
import displayio
import terminalio
import busio
import digitalio
import adafruit_displayio_sh1107

from adafruit_display_text import label
import adafruit_displayio_sh1107

displayio.release_displays()
# old_reset = board.D9

# Use for I2C
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)

# SH1107 is vertically oriented 64x128
WIDTH = 128
HEIGHT = 64
BORDER = 2

display = adafruit_displayio_sh1107.SH1107(display_bus, width=WIDTH, height=HEIGHT)

# Make the display context
splash = displayio.Group()


display.show(splash)


fillscreen = displayio.Group()
CIPscreen = displayio.Group()
fillscreenActive = displayio.Group()

import rotaryio
import time
from adafruit_motorkit import MotorKit
import adafruit_vl6180x
# from adafruit_debouncer import Debouncer

sensor = adafruit_vl6180x.VL6180X(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

# turn off auto refresh
#display.auto_refresh = False


text_area = label.Label(terminalio.FONT, text="monarch", scale=3, color=0xFFFFFF, x=3, y=12)
splash.append(text_area)
text_area2 = label.Label(terminalio.FONT, text="FLUID SYSTEMS", scale=1, color=0xFFFFFF, x=28, y=36)
splash.append(text_area2)
text_area3 = label.Label(terminalio.FONT, text="nano MK.I", scale=1, color=0xFFFFFF, x=70, y=55)
splash.append(text_area3)

#display.refresh()

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
        text_area4 = label.Label(terminalio.FONT, text="Mode:", scale=1, color=0xFFFFFF, x=0, y=5)
        fillscreen.append(text_area4)
        text_area5 = label.Label(terminalio.FONT, text="{ OFF", scale=1, color=0xFFFFFF, x=0, y=15)
        fillscreen.append(text_area5)
        text_area6 = label.Label(terminalio.FONT, text="  ON", scale=1, color=0xFFFFFF, x=0, y=25)
        fillscreen.append(text_area6)
        text_area7 = label.Label(terminalio.FONT, text=" Time:", scale=1, color=0xFFFFFF, x=36, y=5)
        fillscreen.append(text_area7)
        text_area8 = label.Label(terminalio.FONT, text="0.0", scale=1, color=0xFFFFFF, x=102, y=5)
        fillscreen.append(text_area8)
        text_area9 = label.Label(terminalio.FONT, text=" Foam Pulse:", scale=1, color=0xFFFFFF, x=36, y=25)
        fillscreen.append(text_area9)
        text_area10 = label.Label(terminalio.FONT, text="0", scale=1, color=0xFFFFFF, x=116, y=25)
        fillscreen.append(text_area10)
        text_area11 = label.Label(terminalio.FONT, text="º", scale=1, color=0xFFFFFF, x=32, y=5)
        fillscreen.append(text_area11)
        text_area12 = label.Label(terminalio.FONT, text="º", scale=1, color=0xFFFFFF, x=32, y=13)
        fillscreen.append(text_area12)
        text_area13 = label.Label(terminalio.FONT, text="º", scale=1, color=0xFFFFFF, x=32, y=21)
        fillscreen.append(text_area13)
        text_area14 = label.Label(terminalio.FONT, text="º", scale=1, color=0xFFFFFF, x=32, y=29)
        fillscreen.append(text_area14)
        display.show(fillscreen)
        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
        text_area4 = label.Label(terminalio.FONT, text="Mode:", scale=1, color=0xFFFFFF, x=0, y=5)
        fillscreenActive.append(text_area4)
        text_area5 = label.Label(terminalio.FONT, text="  OFF", scale=1, color=0xFFFFFF, x=0, y=15)
        fillscreenActive.append(text_area5)
        text_area6 = label.Label(terminalio.FONT, text="{ ON ", scale=1, color=0xFFFFFF, x=0, y=25)
        fillscreenActive.append(text_area6)
        text_area7 = label.Label(terminalio.FONT, text=" Time:", scale=1, color=0xFFFFFF, x=36, y=5)
        fillscreenActive.append(text_area7)
        text_area8 = label.Label(terminalio.FONT, text=str(timingA_value), scale=1, color=0xFFFFFF, x=102, y=5)
        fillscreenActive.append(text_area8)
        text_area9 = label.Label(terminalio.FONT, text=" Foam Pulse:", scale=1, color=0xFFFFFF, x=36, y=25)
        fillscreenActive.append(text_area9)
        text_area10 = label.Label(terminalio.FONT, text="0", scale=1, color=0xFFFFFF, x=116, y=25)
        fillscreenActive.append(text_area10)
        text_area11 = label.Label(terminalio.FONT, text="º", scale=1, color=0xFFFFFF, x=32, y=5)
        fillscreenActive.append(text_area11)
        text_area12 = label.Label(terminalio.FONT, text="º", scale=1, color=0xFFFFFF, x=32, y=13)
        fillscreenActive.append(text_area12)
        text_area13 = label.Label(terminalio.FONT, text="º", scale=1, color=0xFFFFFF, x=32, y=21)
        fillscreenActive.append(text_area13)
        text_area14 = label.Label(terminalio.FONT, text="º", scale=1, color=0xFFFFFF, x=32, y=29)
        fillscreenActive.append(text_area14)
        display.show(fillscreenActive)


        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
        text_area9 = label.Label(terminalio.FONT, text=" Foam Pulse:", scale=1, color=0xFFFFFF, x=36, y=25)
        fillscreenActive.append(text_area9)
        text_area10 = label.Label(terminalio.FONT, text=str(timingB_value), scale=1, color=0xFFFFFF, x=116, y=25)
        fillscreenActive.append(text_area10)
        display.show(fillscreenActive)


        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()


It's half functioning and half updated, basically just fixing each crack as it appears.

Right now, I have two groups, "fillscreen" & "fillscreenActive" which basically overlap a lot -- one is a static, the other is when I click a button and essentially turn my program on and start twisting my rotary encoders to update two text strings.

Just in relation to these areas, is there a better way to do this so I'm only updating those strings of text themselves and not basically duplicating everything?

burrito_poots
 
Posts: 125
Joined: Thu May 30, 2019 5:02 pm

Re: Broke my old code after update, any idea what I missed?

by Foamyguy on Wed Jun 02, 2021 7:52 pm

You can create the label once and append it to the group only once.

Code: Select all | TOGGLE FULL SIZE
text_area5 = label.Label(terminalio.FONT, text="{ OFF", scale=1, color=0xFFFFFF, x=0, y=15)
fillscreen.append(text_area5)


Then later on you can modify the text in it like this:

Code: Select all | TOGGLE FULL SIZE
text_area5.text = "New string"


I think best way to do it is to create all of your labels and add them to a group before you start your "main loop" section. Then inside your main loop just update the text property on them as needed.

Foamyguy
 
Posts: 50
Joined: Mon May 26, 2014 4:24 pm

Re: Broke my old code after update, any idea what I missed?

by burrito_poots on Wed Jun 02, 2021 8:34 pm

Oooh okay cool -- do I still need to add the "fillscreen.append(text_area5)" below each text modification?

Foamyguy wrote:You can create the label once and append it to the group only once.

Code: Select all | TOGGLE FULL SIZE
text_area5 = label.Label(terminalio.FONT, text="{ OFF", scale=1, color=0xFFFFFF, x=0, y=15)
fillscreen.append(text_area5)


Then later on you can modify the text in it like this:

Code: Select all | TOGGLE FULL SIZE
text_area5.text = "New string"


I think best way to do it is to create all of your labels and add them to a group before you start your "main loop" section. Then inside your main loop just update the text property on them as needed.

burrito_poots
 
Posts: 125
Joined: Thu May 30, 2019 5:02 pm

Re: Broke my old code after update, any idea what I missed?

by Foamyguy on Wed Jun 02, 2021 8:38 pm

nope, you only need to append it to the Group once, generally right after you create it. Then after that you can update the text property only and shouldn't need to do anything else.

Foamyguy
 
Posts: 50
Joined: Mon May 26, 2014 4:24 pm

Re: Broke my old code after update, any idea what I missed?

by burrito_poots on Wed Jun 02, 2021 8:50 pm

Foamyguy wrote:nope, you only need to append it to the Group once, generally right after you create it. Then after that you can update the text property only and shouldn't need to do anything else.


Okay, so its working mostly and big plus is I'm not having my memory issues I was after deleting that fat block of code but, some of my numbers aren't replacing, like they're updating but the old numbers are also sticking around? Any idea why? Here's the as most recent:

Code: Select all | TOGGLE FULL SIZE
import board
import displayio
import terminalio
import busio
import digitalio
import adafruit_displayio_sh1107

from adafruit_display_text import label
import adafruit_displayio_sh1107

displayio.release_displays()
# old_reset = board.D9

# Use for I2C
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)

# SH1107 is vertically oriented 64x128
WIDTH = 128
HEIGHT = 64
BORDER = 2

display = adafruit_displayio_sh1107.SH1107(display_bus, width=WIDTH, height=HEIGHT)

# Make the display context
splash = displayio.Group()


display.show(splash)


fillscreen = displayio.Group()
CIPscreen = displayio.Group()
fillscreenActive = displayio.Group()

import rotaryio
import time
from adafruit_motorkit import MotorKit
import adafruit_vl6180x
# from adafruit_debouncer import Debouncer

sensor = adafruit_vl6180x.VL6180X(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

# turn off auto refresh
#display.auto_refresh = False


text_area = label.Label(terminalio.FONT, text="monarch", scale=3, color=0xFFFFFF, x=3, y=12)
splash.append(text_area)
text_area2 = label.Label(terminalio.FONT, text="FLUID SYSTEMS", scale=1, color=0xFFFFFF, x=28, y=36)
splash.append(text_area2)
text_area3 = label.Label(terminalio.FONT, text="nano MK.I", scale=1, color=0xFFFFFF, x=70, y=55)
splash.append(text_area3)

#display.refresh()

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
        text_area4 = label.Label(terminalio.FONT, text="Mode:", scale=1, color=0xFFFFFF, x=0, y=5)
        fillscreen.append(text_area4)
        text_area5 = label.Label(terminalio.FONT, text="{ OFF", scale=1, color=0xFFFFFF, x=0, y=15)
        fillscreen.append(text_area5)
        text_area6 = label.Label(terminalio.FONT, text="  ON", scale=1, color=0xFFFFFF, x=0, y=25)
        fillscreen.append(text_area6)
        text_area7 = label.Label(terminalio.FONT, text=" Time:", scale=1, color=0xFFFFFF, x=36, y=5)
        fillscreen.append(text_area7)
        text_area8 = label.Label(terminalio.FONT, text="0.0", scale=1, color=0xFFFFFF, x=102, y=5)
        fillscreen.append(text_area8)
        text_area9 = label.Label(terminalio.FONT, text=" Foam Pulse:", scale=1, color=0xFFFFFF, x=36, y=25)
        fillscreen.append(text_area9)
        text_area10 = label.Label(terminalio.FONT, text="0", scale=1, color=0xFFFFFF, x=116, y=25)
        fillscreen.append(text_area10)
        text_area11 = label.Label(terminalio.FONT, text="º", scale=1, color=0xFFFFFF, x=32, y=5)
        fillscreen.append(text_area11)
        text_area12 = label.Label(terminalio.FONT, text="º", scale=1, color=0xFFFFFF, x=32, y=13)
        fillscreen.append(text_area12)
        text_area13 = label.Label(terminalio.FONT, text="º", scale=1, color=0xFFFFFF, x=32, y=21)
        fillscreen.append(text_area13)
        text_area14 = label.Label(terminalio.FONT, text="º", scale=1, color=0xFFFFFF, x=32, y=29)
        fillscreen.append(text_area14)
        display.show(fillscreen)
        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
       
        text_area5.text = "  OFF"
        text_area6.text = "{ ON "
        text_area8.text = str(timingA_value)
       
        #display.show(fillscreen)


        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
        text_area10.text = str(timingB_value)
        #display.show(fillscreen)


        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()


Specifically:

text_area5
text_area6
text_area8
text_area10

The "{ " in front of the ON and OFF text (5 and 6 here) basically acts as a visual identifier, when I push a button the program switches to on or off and so the display updates that. But it's ghosting/sticking on the
"ON" portion after the first time turning it on, and the number fields that are updated via the encoders is also ghosting behind them and not replacing them (text 8 and 10)

?

burrito_poots
 
Posts: 125
Joined: Thu May 30, 2019 5:02 pm

Re: Broke my old code after update, any idea what I missed?

by Foamyguy on Thu Jun 03, 2021 11:11 am

In this section of code starting around line 90:

Code: Select all | TOGGLE FULL SIZE
if current_program != "Fil" and buttond_was_pushed:
        # We are on main screen or CIP, but want Fil.
        # Go there
        text_area4 = label.Label(terminalio.FONT, text="Mode:", scale=1, color=0xFFFFFF, x=0, y=5)
        fillscreen.append(text_area4)
        #....


All of these text_areas 4 through 12 should be created and appended to the fillscreen Group sometime before the main loop (the while True: loop).

Then inside the main loop here there should be calls like text_area4.text = "Mode:" whenever you need to set or change the text that is showing.

Foamyguy
 
Posts: 50
Joined: Mon May 26, 2014 4:24 pm

Please be positive and constructive with your questions and comments.