Question about switch function.

Moderators: grovkillen, Stuntteam, TD-er, enesbcs

Post Reply
Message
Author
User avatar
budman1758
Normal user
Posts: 301
Joined: 15 Apr 2017, 05:13
Location: Riverside CA USA

Question about switch function.

#1 Post by budman1758 » 04 Apr 2021, 20:00

I'm a bit confused about how a switch setup is working. The switch is build into a rotary encoder so its a momentary switch. I have GPIO set up as "input-pullup" and in the device page for the switch I have the switch type as "push button active low". Setting for longpress is not checked.

When I press the switch quickly the state toggles from 0 to 1 or vise versa and stays that way.
When I press the switch for a couple seconds it toggles from one state to another and then back to where it was.
In my mind it should always be at state "1" when the switch is at rest and at state "0" whenever the switch is pressed on.

Change switch to "push button active high" and it gets more interesting... GPIO setting the same.
Quick press the switch and it toggles from 0 to 1.
Long press (or hold) the switch and no state change until you let go of the switch and it then toggles from 0 to 1 just like a short press.

Is this expected behavior? Seems very counter-intuitive to me.
"The glass is twice as big as it needs to be".

TD-er
Core team member
Posts: 8607
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Question about switch function.

#2 Post by TD-er » 04 Apr 2021, 20:30

"Normal" switch mode is that its output state reflects the GPIO state. (or inverted if "invert" was checked)

The other modes like "active on low" or "active on high" are toggle modes.
Meaning when you pull the GPIO low (pull to GND) you switch the state. (or pull to high, depending on the mode)
If you have some simple push button which is located on a lot of boards, those will return to the default state when you no longer press them.
So those are the best candidates for the "toggle" modes, as a single press just switches the state.

The "normal" switch mode is more likely to be used on some toggle switch of something to record a state that's likely to be in a specific state for longer time.
For example a level switch or a flip switch to turn on/off something, or detecting whether something is on/off (measuring the voltage as a binary value)

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Question about switch function.

#3 Post by enesbcs » 05 Apr 2021, 09:25

budman1758 wrote: 04 Apr 2021, 20:00 When I press the switch quickly the state toggles from 0 to 1 or vise versa and stays that way.
When I press the switch for a couple seconds it toggles from one state to another and then back to where it was.
If there are some unexpected effect between quick and slow methods, you can try to change debounce time.
budman1758 wrote: 04 Apr 2021, 20:00 In my mind it should always be at state "1" when the switch is at rest and at state "0" whenever the switch is pressed on.
It is the normal type, what you described here, with inverted gpio values.

bonti69
Normal user
Posts: 119
Joined: 08 Apr 2021, 12:16

Re: Question about switch function.

#4 Post by bonti69 » 06 May 2021, 18:32

Premise: gpio 12 is assigned as input switch, pushbutton active on high. Same gpio/pushbutton is used to wakeup LCD, as "Display button:". Short press wake up the LCD.Now we want to use longpress function of same gpio,something like that: 1st longpress enable task 1, 2nd longpress disable task 1, use longpress to toggle something...It's that possible?

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Question about switch function.

#5 Post by enesbcs » 06 May 2021, 23:04

bonti69 wrote: 06 May 2021, 18:32 Premise: gpio 12 is assigned as input switch, pushbutton active on high. Same gpio/pushbutton is used to wakeup LCD, as "Display button:". Short press wake up the LCD.Now we want to use longpress function of same gpio,something like that: 1st longpress enable task 1, 2nd longpress disable task 1, use longpress to toggle something...It's that possible?
with a dedicated rule and a dummy value which holds the counter of longpress events, i guess that it is possible.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests