Hi pez,
Any ideas?
You are comparing strings that means
>>> '9' > '12'
True
>>> '7777' > '8'
False
>>>
You have to convert the string to an int first.
#!/usr/bin/env python2
# LED_volume.py
# IMPORTS
import sys
import time
import sys
import RPi.GPIO as GPIO
from mpd import MPDClient
#GPIO Setup
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
LED1 = 05
LED2 = 06
LED3 = 12
LED4 = 13
GPIO.setup(LED1,GPIO.OUT, initial=GPIO.LOW) #1st LED
GPIO.setup(LED2,GPIO.OUT, initial=GPIO.LOW) #2nd LED
GPIO.setup(LED3,GPIO.OUT, initial=GPIO.LOW) #3rd LED
GPIO.setup(LED4,GPIO.OUT, initial=GPIO.LOW) #4th LED
#definitions
client = MPDClient()
client.connect('localhost', 6600)
# Poll the volumelevel and set GPIO.output accordingly
while True:
status = client.status()
volume = int(status['volume'])
# print('volume = ' + str(volume))
if volume == 0 : # 1st LED
GPIO.output(LED1,GPIO.LOW)
GPIO.output(LED2,GPIO.LOW)
GPIO.output(LED3,GPIO.LOW)
GPIO.output(LED4,GPIO.LOW)
if volume > 0 and volume <= 50: # 1st LED
GPIO.output(LED1,GPIO.HIGH)
GPIO.output(LED2,GPIO.LOW)
GPIO.output(LED3,GPIO.LOW)
GPIO.output(LED4,GPIO.LOW)
if volume > 50 and volume <= 75: # 2nd LED
GPIO.output(LED1,GPIO.LOW)
GPIO.output(LED2,GPIO.HIGH)
GPIO.output(LED3,GPIO.LOW)
GPIO.output(LED4,GPIO.LOW)
if volume > 75 and volume <= 87: # 3rd LED
GPIO.output(LED1,GPIO.LOW)
GPIO.output(LED2,GPIO.LOW)
GPIO.output(LED3,GPIO.HIGH)
GPIO.output(LED4,GPIO.LOW)
if volume > 87 and volume < 100: # 4th LED
GPIO.output(LED1,GPIO.LOW)
GPIO.output(LED2,GPIO.LOW)
GPIO.output(LED3,GPIO.LOW)
GPIO.output(LED4,GPIO.HIGH)
if volume == 100: # ALL LEDs
GPIO.output(LED1,GPIO.HIGH)
GPIO.output(LED2,GPIO.HIGH)
GPIO.output(LED3,GPIO.HIGH)
GPIO.output(LED4,GPIO.HIGH)
time.sleep(0.2) # Delay loop for 0.2 second
# disconnect
client.disconnect()
# end of programm
This script gives you the following behaveor:
- volume = 0 all LEDs off
- volume between 0 and 50 only LED1 is on
- volume between 50 and 75 only LED2 is on
- volume between 75 and 87 only LED3 is on
- volume between 87 and 100 only LED4 is on
- volume = 100 all LEDs on
Frank