Hi, I have followed this tutorial https://www.allaboutcircuits.com/projects/building-raspberry-pi-controllers-part-5-reading-analog-data-with-an-rpi/ and managed to read the pot perfectly. I then mapped the output to a range between 0 - 100. this gives a steady reading between 0 and 100 whenever I change the dial. The problem is that when I call
socketIO.emit('volume', vol);
I get a lot of falls readings from the potentiometer, as though there’s noise on the GPIO pins.
Just wondering if anyone else has used this approach to incorporate a pot into Volumio, or have any ideas on what to try.?
heres my full code so far
[code]# include RPi libraries in to Python code
import RPi.GPIO as GPIO
import time
from socketIO_client import SocketIO, LoggingNamespace
socketIO = SocketIO(‘localhost’, 3000)
import subprocess
instantiate GPIO as an object
GPIO.setmode(GPIO.BCM)
define GPIO pins with variables a_pin and b_pin
a_pin = 18
b_pin = 23
readings = []
oldVol = 0
max_samples = 8
sleepTime = 0.1
def highestNum(nums):
return max(nums)
create discharge function for reading capacitor data
def discharge():
GPIO.setup(a_pin, GPIO.IN)
GPIO.setup(b_pin, GPIO.OUT)
GPIO.output(b_pin, False)
time.sleep(0.005)
create time function for capturing analog count value
def charge_time():
GPIO.setup(b_pin, GPIO.IN)
GPIO.setup(a_pin, GPIO.OUT)
count = 0
GPIO.output(a_pin, True)
while not GPIO.input(b_pin):
count = count +1
return count
create analog read function for reading charging and discharging data
def analog_read():
discharge()
return charge_time()
def remap( x, oMin, oMax, nMin, nMax ):
#range check
if oMin == oMax:
return None
if nMin == nMax:
return None
#check reversed input range
reverseInput = False
oldMin = min( oMin, oMax )
oldMax = max( oMin, oMax )
if not oldMin == oMin:
reverseInput = True
#check reversed output range
reverseOutput = False
newMin = min( nMin, nMax )
newMax = max( nMin, nMax )
if not newMin == nMin :
reverseOutput = True
portion = (x-oldMin)*(newMax-newMin)/(oldMax-oldMin)
if reverseInput:
portion = (oldMax-x)*(newMax-newMin)/(oldMax-oldMin)
result = portion + newMin
if reverseOutput:
result = newMax - portion
if result < nMin:
result = 0
if result > nMax:
result = 100
return result
provide a loop to display analog data count value on the screen
try:
print “Loop Start”
while True:
#setup array of readings
reading = remap( analog_read(), 15, 105, 0, 100 )
readings.append(reading)
vol = highestNum(readings)
print"vol ", vol
if (vol > (oldVol+ 5)) or(vol < (oldVol - 5)):
oldVol = vol
print "changed"
socketIO.emit('volume', vol);
socketIO.wait(1)
subprocess.call(socketIO.emit(‘volume’,vol))
if len(readings) == max_samples:
readings.pop(0)
time.sleep(sleepTime)
except:
# this catches ALL other exceptions including errors.
# You won’t get any error messages for debugging
# so only use it once your code is working
print “Other error or exception occurred!”
finally:
GPIO.cleanup() # this ensures a clean exit
[/code]
Thanks in advance, Spriggsy