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.

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

by burrito_poots on Thu Jun 03, 2021 2:32 pm

Foamyguy wrote: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.


I'll try this! I'm not entirely sure what's going on, but with zero changes, but code started working without the ghosting issues last night for a few mins after another reset, then started ghosting again — any idea why that would happen? My guess is it's outside of the code (memory issue, maybe a board issue, etc.) but I'm honestly not sure about anything here.

Code as of this last one:

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)

color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0xFFFFFF # White

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

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 Thu Jun 03, 2021 3:17 pm

FIXED MY GHOSTING ISSUE! WOOOT!!!! Thanks a ton. I'll post back when I enevitably break something else lol.

Foamyguy wrote: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.

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 Thu Jun 03, 2021 5:01 pm

Alright, last bit!

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)

color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0xFFFFFF # White

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

text_area4 = label.Label(terminalio.FONT, text="Mode: Filling", 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=20)
fillscreen.append(text_area5)
text_area6 = label.Label(terminalio.FONT, text="  ON", scale=1, color=0xFFFFFF, x=0, y=35)
fillscreen.append(text_area6)
text_area7 = label.Label(terminalio.FONT, text=" Time:", scale=1, color=0xFFFFFF, x=36, y=20)
fillscreen.append(text_area7)
text_area8 = label.Label(terminalio.FONT, text="0.0", scale=1, color=0xFFFFFF, x=110, y=20)
fillscreen.append(text_area8)
text_area9 = label.Label(terminalio.FONT, text=" Foam Pulse:", scale=1, color=0xFFFFFF, x=36, y=35)
fillscreen.append(text_area9)
text_area10 = label.Label(terminalio.FONT, text="0", scale=1, color=0xFFFFFF, x=122, y=35)
fillscreen.append(text_area10)

text_area11 = label.Label(terminalio.FONT, text="Mode: CIP", scale=1, color=0xFFFFFF, x=0, y=5)
CIPscreen.append(text_area11)
text_area12 = label.Label(terminalio.FONT, text="{ OFF", scale=2, color=0xFFFFFF, x=0, y=20)
CIPscreen.append(text_area12)
text_area13 = label.Label(terminalio.FONT, text="  ON", scale=2, color=0xFFFFFF, x=0, y=35)
CIPscreen.append(text_area13)
text_area14 = label.Label(terminalio.FONT, text="Mode: CIP", scale=1, color=0xFFFFFF, x=0, y=50)
CIPscreen.append(text_area14)

#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.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 current_program == "Fil" and buttond_was_pushed and program_running:
        # Button was pushed while fill program running so we want it off.
        # Switch it to off!
        text_area5.text = "{ OFF"
        text_area6.text = "  ON "
        program_running = False
        buttond_was_pushed = False

        if (range_mm >= 70): #"if program_running and (range_mm >= 70):" may need to update to this
            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 #or fill, and want CIP.
        # Go there
        display.show(CIPscreen)
        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!
        text_area12 = "  OFF"
        text_area13.text = "{ ON"
        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 and program running.
        # Make it stop!
        text_area12 = "{ OFF"
        text_area13.text = "  ON"
        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()


On my CIPscreen, lines 202 through 233 specifically, my little "{ OFF" portion is sticking when I click my button when it should be showing " OFF" -- any idea why? I thought I had made it just like the others that resolved my issues.

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 g3holliday on Thu Jun 03, 2021 5:36 pm

Just for the sake of it, you reimported
Code: Select all | TOGGLE FULL SIZE
adafruit_displayio_sh1107



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


:)

g3holliday
 
Posts: 2
Joined: Sun Jul 12, 2020 5:10 pm

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

by plitog on Sun Jun 06, 2021 6:19 pm

burrito_poots wrote:Alright, last bit!
On my CIPscreen, lines 202 through 233 specifically, my little "{ OFF" portion is sticking when I click my button when it should be showing " OFF" -- any idea why? I thought I had made it just like the others that resolved my issues.


On line 215, you have
Code: Select all | TOGGLE FULL SIZE
text_area12 = "  OFF"

Should that perhaps be
Code: Select all | TOGGLE FULL SIZE
text_area12.text = "  OFF"


Line 227 is also missing the ".text" on the assignment.

plitog
 
Posts: 5
Joined: Tue May 25, 2021 8:24 pm

Please be positive and constructive with your questions and comments.