RasveDK
Posts: 1
Joined: Tue Mar 14, 2017 10:30 am

Count button push

Tue Mar 14, 2017 12:07 pm

Hello

This is my first post, and I'm new to Raspberry and Python.
I have tried to search on this the subject, but haven't found a solution yet.

I have tried fiddling with the example from viewtopic.php?f=32&t=104140 without luck.
Therefor I'm asking here :)

Basically i have a switch button, which should define different combinations of pinsettings, by the number of pushes.

So i have to create a counter that counts number og pushs on the button, and that counter has to reset wehn the number of 6 exceeds.
I believe that the counter just have to start at 1, as there are no option at 0.

I also want a 7 segment one digit display to show the counter number.

i have created a flowchart, to simplify needs.

Does anyone have any clever thoughts to at totally newbie?? :)
Thanks!
buttons.png
buttons.png (23.05 KiB) Viewed 1272 times

papatrexasgre
Posts: 24
Joined: Thu Nov 13, 2014 2:52 pm

Re: Count button push

Tue Mar 14, 2017 3:34 pm

Hello,

have a look at this:

Code: Select all

if (GPIO.input(gpioButton) == 0 and buttonIsPressed == 0 and time.time()-buttonDebugTime>.300):
		buttonDebugTime = time.time()
		buttonIsPressed=1
		laneSelect += 1
		if(laneSelect==1):
			buttonPressTime = time.time()
	if (GPIO.input(gpioButton) == 1 and buttonIsPressed == 1):
		buttonIsPressed=0
		
	if (time.time()-buttonPressTime>1 and laneSelect>0):
		
		if (laneSelect ==1):
			laneSelect = 0
			GPIO.output(Relay[1],False)
			relayFlag[1] = False
			#print(str(time.strftime("%H:%M:%S", time.localtime(time.time())) + "." + str(time.time()).split(".",1)[1])+"Starting Lane One")
			errorFlag[1][1]=False
			errorFlag[1][2]=False
		if (laneSelect >1):
			laneSelect = 0
			GPIO.output(Relay[2],False)
			relayFlag[2] = False
			#print(str(time.strftime("%H:%M:%S", time.localtime(time.time())) + "." + str(time.time()).split(".",1)[1])+"Starting Lane Two")
			errorFlag[2][1]=False
			errorFlag[2][2]=False
		laneSelect = 0
I raise a flag when the button is pressed and at least 300ms passed since the last press and lower that flag when I release the button. I have a window of 1 second to press the button twice "time.time()-buttonPressTime>1". You can raise that timer and add more IFs. Hope it helps.

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: Count button push

Tue Mar 14, 2017 8:31 pm

I think that a "for" statement would work also.
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

Return to “Python”