Counting button pressed on remote controle

On a raspberry Pi 4 i have connected a small remote control. I have the buttons in a case statement. One button is the “Select” button. I want my program to count the times this button is pressed and react according to it. So, 1 time pressed, start this, 2 times pressed start that, etc.

I allready have a problem with the increment of my var, from 1 time pressed to 2 time, with +=1.

Someon willing to help?

Thx in advance, Pit

I add my code so far it is adjusted code found on the internet.

#!/usr/bin/python

import subprocess
import sys
import os

from lirc import RawConnection
def ProcessIRRemote():

    #get IR command
    #keypress format = (hexcode, repeat_num, command_key, remote_id)
    try:
        keypress = conn.readline(.0001)
    except:
        keypress=""

    if (keypress != "" and keypress != None):

        data = keypress.split()
        sequence = data[1]
        command = data[2]

        #ignore command repeats
        if (sequence != "00"):
           return

        #print(command)
        

        match command:
            case "KEY_POWER":
                print("De aan en uit knop.")
                os.system('systemctl poweroff')
            case "KEY_MUTE":
                print("Stilte.")
            case "KEY_PREVIOUS":
                print("Vorige nummer.")
            case "KEY_PLAY":
                print("Huidige nummer afspelen.")
            case "KEY_NEXT":
                print("Volgende nummer.")
            case "KEY_UP":
                print("Wielpijl op.")
            case "KEY_LEFT":
                print("Wielpijl links.")
            case "KEY_OK":
                print("Wielpijl OK.")
            case "KEY_RIGHT":
                print("Wielpijl rechts.")
            case "KEY_DOWN":
                print("Wielpijl neer.")
            case "KEY_EXIT":
                print("De Exit knop.")
            case "KEY_CHANNEL":
                print("Keuzeknop.")
            case "KEY_SELECT":
                gedrukt += 1
                print(gedrukt)
            case "KEY_VOLUMEDOWN":
                print("Geluid zachter.")
            case "KEY_VOLUMEUP":
                print("Geluid harder.")

#define Global
conn = RawConnection()
global gedrukt
gedrukt = 0
print("Starting Up...")
while True:
      ProcessIRRemote()

Hi,

here is a related thread from last week from a user that is also using a Rasberry Pi in their application where a button is used to select between different menu options.

I assume you see an error message when this case is matched.

Without a global gedrukt the varaible is not accessible inside your function.

You can fix it like this:

            case "KEY_SELECT":
                global gedrukt
                gedrukt += 1
                print(gedrukt)

Here is an example that shows the error I expected you to have seen.

$ cat tmpdir/a.py
global a

def foo():
    a += 1

foo()
$ py3 tmpdir/a.py
Traceback (most recent call last):
  File "/Users/barry/tmpdir/a.py", line 6, in <module>
    foo()
    ~~~^^
  File "/Users/barry/tmpdir/a.py", line 4, in foo
    a += 1
    ^
UnboundLocalError: cannot access local variable 'a' where it is not associated with a value

As @barry-scott said, you’ve put global gedrukt in the wrong place. It should be inside the function, not outside it in the main code.