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
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)