So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Narfel
#70998 I just found philbowles wonderful https://github.com/philbowles/esparto project. I'll likely use it for the final switch as it needs to be as foolproof as possible because the switch will be for a impatient senior that loves nothing more than flicking switches. But in the meantime i'll just use the feature list as a challenge (or wishlist). After all i'm here to learn and the bespoke senior can manually flick a bit longer :) Debouncing is coming along nicely.
I'm afraid the setting up the timer properly would be touching on callbacks and multithreading, wouldn't it? I read about it, but that's what i meant with a bit out of my league. In a earlier version of my code i tried using yield() to have the hardware button working while connecting. But my esp just flipped the table and got very upset, so i ... erm ... yielded :? :)
User avatar
By rudy
#71016 There are lots of libraries. Some good and some not as good. Most of the time they will do what you expect without problems.

When I started with Arduino I thought it was the way to go since there was so much available. I found libraries that worked and did what I wanted them to do. The problem came when I had to combine the operation of those libraries into one program. It was a struggle. But with ongoing work I learned more and more. And there is plenty more that I still need to learn.
User avatar
By oxurane
#71128 Bouncing, can be solved by adding a small value capacitor (to GND) near the switch, in parallel. I think in general a 10nF or 1nF will be OK.

Whenever you use a loop to check a status of a pin, introduce a variable, which acts like a flag. Use this flag to see if a part of sourcecode should be processed. This flag is also used to prevent a process to be run multiple times (i.e. because bouncing occurs).
Because programming can done in several language, I give a general example.

Define a variable "BtnPushed" and give it default the value "0". What I write below as "sourcecode" is not official. The code is merely Basic for AVR microcontrollers, but you can understand how it operates.

Code: Select allProces CheckBtn()
  IF BtnPushed = 0 Then    'Only check if needed : avoid bouncing
     IF Input = 1 Then     '=Vcc
        BtnPushed = 1      'modify variable
     Else
       'Do nothing
     End IF
  End IF
End Process CheckBtn()

Proces DoSomething()
  For i = 1 to 3
      Print "Hello World"
  Next
  Wait 10 ms               'Wait 10 ms
  BtnPushed = 0            'Set it back, so button may be checked again.
End Process DoSomething()

Proces Main()
  Call Proces CheckBtn()
  IF BtnPushed = 1 Then
     Call Proces DoSomething()
  Else
    'Do Something else
  End IF
End Proces Main()

The Main is the loop you always run. One proces is checking if a button is pushed, unless it was recently pushed. The other proces is started to do something when a button was pushed.


There's also something about statusses for a digital INPUT-pin. It's important to have 2 distinct statusses available : Vcc and GND
This means for example, the input is connected to :
1) GND and when pushing the button, it goes to Vcc.
2) Vcc and when pushing a button, it goes to GND.

Image

In many designs, I've seen a button is kept floating (no Gnd nor Vcc), and when pushed it connects to GND or Vcc. This is not a safe design ! Always try to prevent such situation. When an INPUT is kept floating, the wiring between the INPUT and a button acts merely like an "antenna". In other words, a device has to decide itself what the status is. Normally it won't matter much, but if you like it to operate reliable, avoid this situation as a prevention to odd results.
Advise : Make sure there are 2 well defined statusses available for a digital INPUT and don't introduce a third one (floating).
Last edited by oxurane on Mon Oct 23, 2017 10:48 am, edited 1 time in total.
User avatar
By rudy
#71131
In many designs, I've seen a button is kept floating (no Gnd nor Vcc), and when pushed it connects to GND or Vcc. This is not a safe design and always try to prevent this situation.


Most inputs on CPUs have the option of using an internal pull up current source. And while you may not see it in the schematic can be in the electrical circuit. As long as the connection to the switch is relatively short the internal pull up is sufficient.

If the length becomes longer it is highly advisable to use a lower resistance as the pull up (or down). Additionally it is desirable to place a series resistance in just before the input pin. This will help to prevent transient signals from affecting the chip.