Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running total counter
#1
I am working on a project that I need to keep a running total of how many times the output was signaled. I cannot figure out how to perform the operators to keep count. I need help with figuring out the code to write on if GPOI 13 is True then ass 1 to total and if GPIO 15 is True then subtract from total. And print the total

import RPi.GPIO as GPIO
import time
import itertools


GPIO.setmode(GPIO.BOARD)

GPIO.setup(11, GPIO.IN) #PIR POOL MOTION
GPIO.setup(13, GPIO.IN) #PIR GATE 1 SENSOR
GPIO.setup(15, GPIO.IN) #PIR GATE 2 SENSOR
GPIO.setup(36, GPIO.OUT) #LED POOL MOTION
GPIO.setup(38, GPIO.OUT) #LED GATE 1 SENSOR
GPIO.setup(40, GPIO.OUT) #LED GATE 2 SENSOR
counter = 0

try:
    time.sleep(2) # to stabilize sensor
   
   
   
    while True:

        if GPIO.input(11):
            GPIO.output(36, True)
            print ("DETECTED3")
            time.sleep(2) #to avoid multiple detection
            GPIO.output(36, False)

        if GPIO.input(13):
            counter = 0
            counter +=1
            print ("1+")    
            GPIO.output(38, True)
            print ("DETECTED2")
            time.sleep(2) #to avoid multiple detection
            GPIO.output(38, False)
                       

        if GPIO.input(15):
            GPIO.output(40, True)
            print ("DETECTED1")
            time.sleep(2) #to avoid multiple detection
            GPIO.output(40, False)

        else:
            print ("Not detected")
        time.sleep(2) #loop delay, should be less than detection delay

except:
    GPIO.cleanup()
buran write Mar-05-2021, 02:10 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Thanks Buran this is my first time asking a question on here. I will review the BBcode help.
Reply
#3
With the following changes, counts will contain the count for each gpio pin.
There is however a problem in that this is going to display on every iteration, So I leave that for you to figure out

import RPi.GPIO as GPIO
import time
import itertools
 
 
GPIO.setmode(GPIO.BOARD)
 
GPIO.setup(11, GPIO.IN) #PIR POOL MOTION
GPIO.setup(13, GPIO.IN) #PIR GATE 1 SENSOR
GPIO.setup(15, GPIO.IN) #PIR GATE 2 SENSOR
GPIO.setup(36, GPIO.OUT) #LED POOL MOTION
GPIO.setup(38,        show_counts()
 GPIO.OUT) #LED GATE 1 SENSOR
GPIO.setup(40, GPIO.OUT) #LED GATE 2 SENSOR

maxpins = 20
counts = [0] * maxpins

def show_counts():
    for i in range(maxpins):
        if counts[i]:
            print(f"GPIO pin {i} count is {counts[i]}")

try:
    time.sleep(2) # to stabilize sensor
    
    while True:
 
        if GPIO.input(11):
            GPIO.output(36, True)
            counts[11] += 1
            print ("DETECTED3")
            time.sleep(2) #to avoid multiple detection
            GPIO.output(36, False)
 
        if GPIO.input(13):
            counts[13] += 1
            print ("1+")    
            GPIO.output(38, True)
            print ("DETECTED2")
            time.sleep(2) #to avoid multiple detection
            GPIO.output(38, False)

        if GPIO.input(15):
            GPIO.output(40, True)
            counts[15] += 1
            print ("DETECTED1")
            time.sleep(2) #to avoid multiple detection
            GPIO.output(40, False)
 
        else:
            print ("Not detected")
        show_counts()
        time.sleep(2) #loop delay, should be less than detection delay
        
except:
    GPIO.cleanup()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Keep a running total(budget) mcmxl22 3 4,547 Dec-07-2016, 11:04 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020