Hi I have previous been using the Bounce2 library for my Arduino projects and am now trying to move some of them to the Opta platform where I have an Arduino Opta RS485 and one expansion module.
But I have some diffucults on how to be able to read if an input is going from LOW to HIGH and from HIGH to LOW because I want to be able to call some functions on each shift.
Do anyone have a solution for that ore know of a simular library that forks on the Opta PLC ?
Thanks.
Why can't you use hardware debounce? It's always the best solution instead of the software one...
Try this:
I didn't know that there was an hardware debounce, do I need anything special to use that, because I am just using the Arduini IDE, not the PLC Part just to be sure.
But I still need some way to detect if the Input is Rising or Falling so that I can call function their and and not the whole time while an input is high or low, only once on the change.
Witch I normally do with
if ( debouncer.fell() ) { // Call code if button transitions from HIGH to LOW
My Function
}
if ( debouncer.rose() ) { // Call code if button transitions from LOW to HIGH
My Function
}
This only for some parts solved my problem. It wil continue to run the function as long as the input is High, I need something so that its only run once each time the state has changed
The tutorial teaches you how to debounce buttons and detect state changes, which is exactly what you asked for. There are other tutorials you can find on the same subject if mine isn't what you want. However, I am confident that my tutorial contains enough information to learn to debounce a button and detect state change ether when a button becomes pressed or becomes not pressed. Further into the tutorial there is code for detecting short and long presses, if you so wish.
You need just a couple of resistors and a capacitor, regardless of the device where it must be used with...
This example is for a pull-up input (and what I used before if software debounce can't be used for any reason):

A more complete and powerful version (providing a hysteresis) includes a Schmitt Trigger (74HC14, containing six invertes), but the one above is the most common solution, and enough for almost every project.
Let me also say the debounce solution depends on what your code must do, and how, and multiple approaches are possible.
If the task to be performed elsewhere in the code aren't much time criticals, you can just use a delay to get over the (short) time where the contacts are "bouncing".
Example:
int curBtnState;
int prevBtnState;
...
bool curBtnState = digitalRead(PIN_BUTTON);
// Pull-down button: HIGH when pressed
if (curBtnState == HIGH && prevBtnState== LOW) {
delay(50); // Basic software debounce
// do what you need to do...
...
}
prevBtnState = curBtnState ;
If you need to save CPU time and/or can't even use a 50 ms delay, you could use millis() to check the button status avoiding the bounces. Example:
#define DEBOUNCE_DELAY 50
unsigned long tmrLastDebounce;
int curBtnState;
int prevBtnState;
...
curBtnState = digitalRead(PIN_BUTTON);
if (curBtnState != prevBtnState) {
tmrLastDebounce = millis();
prevBtnState = curBtnState;
}
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
// do what you need to do...
...
}