I am attempting to calculate the total number of available tables when an object is detected by the IR sensor

24 Views Asked by At

I am attempting to calculate the total number of available tables when an object is detected by the IR sensor. How can I get the actual result without the loop.

import BlynkLib
from BlynkTimer import BlynkTimer
from signal import signal, SIGTERM, SIGHUP, pause
from rpi_lcd import LCD
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(26, GPIO.IN)
GPIO.setup(24, GPIO.IN)

BLYNK_AUTH_TOKEN = '0WPrQT4snYnF_nwa2WAdXiUqWrYsAA-F'

# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH_TOKEN)

# Create BlynkTimer Instance
timer = BlynkTimer()

# Create LCD
lcd = LCD()

# Initial with the total number of tables
available_tables = 2

# Function to sync the data from virtual pins
@blynk.on("connected")
def blynk_connected():
    print("Hi, You have Connected to New Blynk2.0")
    print(".......................................................")
    lcd.text("Connected Now", 1)
    time.sleep(2)

# Define a callback function for the sensor
def sensor_callback(channel):
    global available_tables
    if GPIO.input(26) == 0 or GPIO.input(24) == 0:
        available_tables -= 1
    else:
        available_tables += 1
            
    #ensure available tables remains within the range of 0 to 2
    available_tables = max(0,min(2, available_tables))

# Add event detection to the GPIO pins with GPIO.FALLING
GPIO.add_event_detect(26, GPIO.FALLING, callback=sensor_callback, bouncetime=200)
GPIO.add_event_detect(24, GPIO.FALLING, callback=sensor_callback, bouncetime=200)

# Function to exit safely
def safe_exit(signum, frame):
    exit(1)

signal(SIGTERM, safe_exit)
signal(SIGHUP, safe_exit)

try:
    while True:
        if GPIO.input(26) == 0:
            lcd.text("Table 1 is Full,", 1)
            blynk.virtual_write(0, "Not Available")
            blynk.set_property(2, "color", "#D3435C")
            time.sleep(0.01)
        else:
            lcd.text("Table 1 Free", 1)
            blynk.virtual_write(0, "Available")
            blynk.set_property(2, "color", "#23C48E")
            time.sleep(0.01)

        if GPIO.input(24) == 0:
            lcd.text("Table 2 is Full,", 2)
            blynk.virtual_write(1, "Not Available")
            blynk.set_property(3, "color", "#D3435C")
            time.sleep(0.01)
        else:
            lcd.text("Table 2 Free", 2)
            blynk.virtual_write(1, "Available")
            blynk.set_property(3, "color", "#23C48E")
            time.sleep(0.01)

        blynk.virtual_write(4, f"{available_tables}")

        blynk.run()
        timer.run()
        pass

except KeyboardInterrupt:
    GPIO.cleanup()

I am trying to use the sensor callback method to track the total number of available tables. If with a loop, the count is continuously decremented when an object is detected by the IR sensor. However, I need to hold the result

EXAMPLE:

Available tables = 2

When sensor 1 detects an object, the total number of available tables is reduced, leaving 1 table available.

# Define a callback function for the sensor
def sensor_callback(channel):
    global available_tables
    if GPIO.input(26) == 0 or GPIO.input(24) == 0:
        available_tables -= 1
    else:
        available_tables += 1
            
    #ensure available tables remains within the range of 0 to 2
    available_tables = max(0,min(2, available_tables))

# Add event detection to the GPIO pins with GPIO.FALLING
GPIO.add_event_detect(26, GPIO.FALLING, callback=sensor_callback, bouncetime=200)
GPIO.add_event_detect(24, GPIO.FALLING, callback=sensor_callback, bouncetime=200)

I found the problem when I use the method of this, it will be fine when starting calculate but after a few second the outcome result will become mull. How can I solve it?

0

There are 0 best solutions below