Page 1 of 1

Potentiometer Lagging

Posted: Wed Jan 15, 2020 10:12 pm
by almsotcarbon14
I'm working on a project where I'm using a potentiometer to increment a timer. But the potentiometer (brand new) seems to have difficulty picking up turns. I will turn it 1/4 or 1/2 way and it will only pick up maybe one increment. It will also often register a forward turn as a backwards turn. I'm not sure if this is due to my coding, or if it is due to the piece itself. I've not worked with a potentiometer before so I'm more inclined to believe that it's the actual code itself.

Code: Select all

minute = 0
sec = 0

clk = 13
dt = 19
sw = 6

GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
clk_last_state = GPIO.input(clk)

try:
    while True:
        clk_state = GPIO.input(clk)
        dt_state = GPIO.input(dt)
        sw_state = GPIO.input(sw)
        
        if clk_state != clk_last_state:
            if dt_state != clk_state:
                value += 1
                if sec < 45:
                    sec += 15
                else:
                    sec = 0
                    minute += 1
            else:
                value -= 1
                if sec > 0:
                    sec -= 15
                else:
                    sec = 45
                    if minute > 0:
                        minute -= 1
                        
        clk_last_state = clk_state
        display.display(package(minute,sec))
        
        if sw_state != 1:
            motor.start_motor((60*minute) + (sec))
            minute = 0
            sec = 0

        sleep(.01)
finally:
    GPIO.cleanup()
My first thought is that the sleep time is messed up, and somehow preventing it from registering turns, but I've tried both larger and smaller values and have as bad, if not worse, results. The pins are set the way they are because I was following a tutorial so I'm not sure if that's the optimal way to do it.
Any help y'all can provide would be extremely appreciated, thanks!

Re: Potentiometer Lagging

Posted: Fri Jan 17, 2020 2:19 pm
by paddyg
I would have thought potentiometer would be read with AtoD. Is it an encoder? Do you have a part number or link to docs? If it's something like an encoder then it's best to trigger a function on change of GPIO and you have to assume that each 'edge' of the square wave consist of hundred of tiny open/close circuits and filter accordingly.

Re: Potentiometer Lagging

Posted: Fri Jan 17, 2020 3:56 pm
by B.Goode
Is this the complete story and the complete script?


Or is there some add-on hardware and associated python library involved?


What is the significance of the apparently undefined functions display.display(package(minute,sec)) and motor.start_motor((60*minute) + (sec))