guzu
Posts: 19
Joined: Thu Nov 10, 2016 7:25 pm

Delay in incrementing with Python script

Fri Dec 02, 2016 7:57 pm

Hey.
I built a circuit, using the Raspberry Pi, to increment the value on a 7 segment display using 2 buttons.
When I press button one, the value on the display increases by 1.
When I press button two, the value on the display decreases by 1.
This works fine with only one problem:
If i press one of the buttons and after that I press the other (no matter how long I wait in between the button presses) the value does not change at the first press.
Only if I press the button a second time or I long press the button (0.5 sec button depressed) the value changes.

Why is this? I need for it to change when I push the button.
Here is the code I wrote:
BTW any improvements to the code are welcomed :D

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode (GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(31, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(32, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

segments = (11, 12, 13, 15, 16, 18, 22, 29)
for segment in segments:
    GPIO.setup(segment, GPIO.OUT)
    GPIO.output(segment, 0)


gears = {0 :[13, 18],
         00:[11,12,15,22,29],
         1 :[13, 18],
         10:[11,12,15,22,29],
         2 :[29, 18, 15, 11, 12],
         20:[13,22],
         3 :[29, 18, 15, 13, 12],
         30:[11, 22],
         4 :[22, 15, 18, 13],
         40:[11, 12, 29],
         5 :[29, 22, 15, 13, 12],
         50:[11, 18],
         6 :[29, 22, 15, 13, 12, 11],
         60:[18]}
i=1
x=10
while(1):
    if (GPIO.input(31)==1):
        GPIO.output(gears[i],1)
        GPIO.output(gears[x],0)
        i+=1
        x+=10
        time.sleep(.2)

    if (GPIO.input(32)==1):
        GPIO.output(gears[i-1],1)
        GPIO.output(gears[x-10],0)
        i-=1
        x-=10
        time.sleep(.2)

User avatar
Paeryn
Posts: 2987
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Delay in incrementing with Python script

Sat Dec 03, 2016 3:51 am

guzu wrote:Hey.
I built a circuit, using the Raspberry Pi, to increment the value on a 7 segment display using 2 buttons.
When I press button one, the value on the display increases by 1.
When I press button two, the value on the display decreases by 1.
This works fine with only one problem:
If i press one of the buttons and after that I press the other (no matter how long I wait in between the button presses) the value does not change at the first press.
Only if I press the button a second time or I long press the button (0.5 sec button depressed) the value changes.

Why is this? I need for it to change when I push the button.
Here is the code I wrote:
BTW any improvements to the code are welcomed :D

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode (GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(31, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(32, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

segments = (11, 12, 13, 15, 16, 18, 22, 29)
for segment in segments:
    GPIO.setup(segment, GPIO.OUT)
    GPIO.output(segment, 0)


gears = {0 :[13, 18],
         00:[11,12,15,22,29],
         1 :[13, 18],
         10:[11,12,15,22,29],
         2 :[29, 18, 15, 11, 12],
         20:[13,22],
         3 :[29, 18, 15, 13, 12],
         30:[11, 22],
         4 :[22, 15, 18, 13],
         40:[11, 12, 29],
         5 :[29, 22, 15, 13, 12],
         50:[11, 18],
         6 :[29, 22, 15, 13, 12, 11],
         60:[18]}
i=1
x=10
while(1):
    if (GPIO.input(31)==1):
        GPIO.output(gears[i],1)
        GPIO.output(gears[x],0)
        i+=1
        x+=10
        time.sleep(.2)

    if (GPIO.input(32)==1):
        GPIO.output(gears[i-1],1)
        GPIO.output(gears[x-10],0)
        i-=1
        x-=10
        time.sleep(.2)
Because of how you've coded it.
Start : i=1, x=10
Press up: output gears on, gears[x=10] off, i+=1 (i = 2) x+=10 (x = 20)
Press up: output gears on, gears[x=20] off, i+=1 (i = 3) x+=10 (x = 30)
Press down: output gears[i-1 (3-1) =2] on, gears[(x-10) =(30-10) =20] off, i-=1 (i = 2) x-=10 (x = 20)

So you can see you show a different gear setup to the one the counters are showing.

on button 1 you output the current states then increment the counters. (so show 1, counters now hold 2)
on button 2 you output the states of the (counters-1) then decrement the counters (so show 2-1=1, counters now hold 1)

If you change the code to do

Code: Select all

    if (GPIO.input(31)==1):
        i+=1
        x+=10
        GPIO.output(gears[i],1)
        GPIO.output(gears[x],0)
        time.sleep(.2)

    if (GPIO.input(32)==1):
        i-=1
        x-=10
        GPIO.output(gears[i],1)
        GPIO.output(gears[x],0)
So now you consistently increment or decrement the counters and then output the current values (same in both) you'll have consistent behaviour.

Although note that your code for 0 breaks, you have dictionary entries for key=0 and key=00. The dictionary entry for key=00 will override the one for key=0 since 0 == 00, they are the same key!
She who travels light — forgot something.

guzu
Posts: 19
Joined: Thu Nov 10, 2016 7:25 pm

Re: Delay in incrementing with Python script

Sat Dec 03, 2016 6:19 am

Thank you very much Paeryn. You helped me a lot.
Now I understand what I did wrong. :D

guzu
Posts: 19
Joined: Thu Nov 10, 2016 7:25 pm

Re: Delay in incrementing with Python script

Sun Dec 04, 2016 11:04 am

So I have optimised the code a bit and removed the dictionary Key that was useless.
Now I am trying to make so the digits go on and off (so when I use more digits, I won't use more current that the GPIOs can handle).
Could anyone give me an idea on how to do this ? I have tried the code after the # but it has some weird flicker.

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode (GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(31, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(32, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

segments = (11, 12, 13, 15, 16, 18, 22, 29)
for segment in segments:
    GPIO.setup(segment, GPIO.OUT)
    GPIO.output(segment, 0)

gears = {1 :[13, 18],
         10:[11,12,15,22,29],
         2 :[29, 18, 15, 11, 12],
         20:[13,22],
         3 :[29, 18, 15, 13, 12],
         30:[11, 22],
         4 :[22, 15, 18, 13],
         40:[11, 12, 29],
         5 :[29, 22, 15, 13, 12],
         50:[11, 18],
         6 :[29, 22, 15, 13, 12, 11],
         60:[18]}
i=1
x=10
while(1):
#    GPIO.output(16,0)
#    time.sleep(.001)

    while (GPIO.input(31)==0 and i<2):
        GPIO.output(gears[1],1)
#        GPIO.output(16,0)
#        time.sleep(0.001)
#        GPIO.output(16,1)
#        time.sleep(0.001)
    if (GPIO.input(31)==1):
        i+=1
        x+=10
        if i==7:
            i=6
            x=60
        else:
            GPIO.output(gears[i],1)
            GPIO.output(gears[x],0)
            time.sleep(.2)

    if (GPIO.input(32)==1):
        i-=1
        x-=10
        if i==0:
            i=1
            x=10
        GPIO.output(gears[i],1)
        GPIO.output(gears[x],0)
        time.sleep(.2)
        
#    GPIO.output(16,1)
#    time.sleep(.001)

Return to “Troubleshooting”