So I work in the beverage industry and I decided to try and make a can counter using a Raspberry PI4. It needs to use two industrial sensors on the GPIO that detect the cans.

Full Disclosure I have just been googling most code and reading when I get errors in terminal to try and fix the issue. I have done some rudimentary C# and C++ programming doing PLC stuff but it's nothing like what i'm trying to do right now. Just some simple statements for conversions and formulas.

I have had it counting via the sensor on a very rudimentary code

import RPi.GPIO as GPIO
import time

GPIN = 16
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN, GPIO.IN)

counting = 0

while True:
    while GPIO.input(GPIN) == 0:
        time.sleep(0.1)

    counting = counting + 1
    print(counting)


    while GPIO.input(GPIN) == 1:
        time.sleep(0.1)

This counts in the terminal. It is of note I need to count the on and off state with a slight delay to keep accidental double counts from happening. I have even added in a GUI with guizero that makes it count in a window. although currently I cannot replicate that from what I remember working and i foolishly didn't save that as i was trying to get to the next stage, but the rough of it was instead of the print(counting) section in the above code I had the app.display() info.

Problem is I need it to count 2 sensors at the same time, one before the can rejector and one after. So I did some reading and figured I needed to run two (or maybe 3) loops at the same time. as I have 2 sensors that need a constant loop, plus it seems like a need another loop that runs and refreshes the GUI. I got turned into threading and have been trying to implement it as that seems like what I want but haven't been able to make heads or tails of it. I can get the GUI to display, but the sensors don't read. If I switch back to my simple code it counts away. Im having trouble meshing the two together.

import threading
from guizero import App, Text, PushButton
import RPi.GPIO as GPIO
import time

GPIN1 = 16
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN1, GPIO.IN)

GPIN2 = 15
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN2, GPIO.IN)


counting1 = 0
counting2 = 0
counting3 = counting1 - counting2

def sensor1():
    global counting1
    while GPIO.input(GPIN1) == 0:
        time.sleep(0.1)                  
    counting1 = counting1 + 1    
    while GPIO.input(GPIN1) == 1:
        time.sleep(0.1)

def sensor2():
    global counting2
    while GPIO.input(GPIN2) == 0:
        time.sleep(0.1)                  
    counting2 = counting2 + 1    
    while GPIO.input(GPIN2) == 1:
        time.sleep(0.1)

x = threading.Thread(target=sensor1)
y = threading.Thread(target=sensor2)
x.start()
y.start()

while True:
        app = App(title="Can Count")
        message = Text(app, text="Total")
        message = Text(app, text=(counting1))
        message = Text(app, text="Rejected")
        message = Text(app, text=(counting3))
        app.display()

I'm just a bit stumped I'm sure my way isn't the best way to do this, any advice, tips or pointers in the right direction would be appreciated. I'm trying to crash course youtube python tutorials on the side but I am still coming up short.

It seems like I can get the display to show updates if i close the window via the x it restarts the window and shows the update but I have tried a few different things with guizero using a def text(): above that code and text.repeat(10, text) thinking this would redraw the screen but that doesn't work or breaks the gui or the code.

Also I know I call PushButton and don't use it, but the end goal will have a simple reset the counter button.. Just haven't got there yet.

0

There are 0 answers