I'm currently trying to make a music/mpd player on the raspberry pi using toggle switches to control play/pause; next and previous.I'm quite new to python and have made the play/pause work, but cant work out what I'm doing wrong with prev/next. I'm trying to write a program that would allow me to run "mpc prev" every time that the toggle changes from on to off (or off to on), but do nothing when it is staying still in one position, only when it changes.
import RPi.GPIO as GPIO import time from subprocess import call GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) x = 2 y = True z = True input_state = GPIO.input(23) while x == 2: if input_state == False: x = 1 elif input_state == True: x = 0 while True: if y == True and x == 1: call(["mpc", "prev"]) y = False z = True x = 2 elif z == True and x == 0: call(["mpc", "prev"]) z = False y = True x = 2 else: x = 2
Any ideas about how my code is wrong/how to improve it would be greatly appreciated!