mnishiguchi
Nerves Core Team
I detect the button push by using Circuits.GPIO.set_interrupts(gpio_ref, :rising); however it is too sensitive that I want to debounce it. I am thinking about setting a condition using the second data item in the message tuple {:circuits_gpio, 17, 1233456, 1} for controlling the sensitivity. Is it good? Is there a better way?
My target is Rpi4. My button is a 6mm mini button.
defmodule LedBlinker.Button do
use GenServer, restart: :temporary
@debounce_time 200_000_000
def start_link({gpio_pin, on_push_fn}) when is_number(gpio_pin) and is_function(on_push_fn) do
GenServer.start_link(__MODULE__, {gpio_pin, on_push_fn})
end
def init({gpio_pin, on_push_fn}) do
# Get a ref to the GPIO pin.
{:ok, gpio_ref} = Circuits.GPIO.open(gpio_pin, :input)
# Get messages every time the button is pushed.
Circuits.GPIO.set_interrupts(gpio_ref, :rising)
{
:ok,
%{
gpio_pin: gpio_pin,
gpio_ref: gpio_ref,
last_pushed_at: 0,
on_push_fn: on_push_fn
}
}
end
def handle_info({:circuits_gpio, _, at, 1} = message, state) do
%{last_pushed_at: last_pushed_at, on_push_fn: on_push_fn} = state
# Debounce the message.
if @debounce_time < at - last_pushed_at do
on_push_fn.(message)
end
{:noreply, %{state | last_pushed_at: at}}
end
end
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
ondrej-tucek
I’d use send_after & cancel_timer or sleep functions instead of if statement. For example:
Process.send_after(self(), {:debounce, on_push_fn, msg}, @debounce_time)+handle_infofor:debounce.mattludwigs
I think either way should get you what you need and probably more up to person prefference.
I like the
.
ifstatement appoarch. Speaking from my expereince, having to manage timers and sleeps normally does not work out too well for me. However, that might just be user error on my partmnishiguchi
@ondrej-tucek @mattludwigs Thank you for your input. It was helpful.
After all, I realized that I could use Rpi’s internal pull-down resister, which I had not known is used for at that time.
Circuits.GPIO.set_pull_mode(gpio, pull_mode)handles it all without adding any physical resister! I mean sensitivity feels right without any tuning. It makes my genserver simpler.It was still a great opportunity to learn how I could control the sensitivity of incoming data.
dominicletz
Cool, I think you’ve found the best solution. Just in case though I wanted to provider another option. I’ve had a couple of cases where I needed debounce behaviour and implement a module:
if you add this module to your dependencies:
you can just do:
And it will ensure that the event identified by the first argument (
:circuits_gpio) is not triggered more often than every 5_000 millisecond.There is actually a range of different debounce behaviors I needed in some of my projects so the module implements four different functions. All of them take 3 argument: (event_key, function, timeout)
Or here in a text graph:
Hope this helps some readers.
mnishiguchi
Wow, nice! Thanks. I will definitely keep that in my toolbox.