QuickFix wrote:Do you really have to use a software solution?
I'd rather solve the problem at the root and debounce the switching circuit itself (but hey: I'm originally an electronics guy).
http://www.ganssle.com/debouncing.htm
Well, yes and no.
For the sake of practicality, I will certainly end up adding a physical circuit as well, especially when the thing is ready to be deployed. Thanks very much for the link. (Nice read, just what I was looking for)
For the sake of understanding Arduino (and ESP8266 in general - I see Lua users with similar problems), I definitely want a software solution that works acceptably well, at least less than 0.1% error, < 0.01% even better!
===
I am really intrigued why my code (latest below) is working so poorly - sometimes not at all. I suspect some sort of asynchronous functioning for the interrupt functions, but even adding a global flag to simulate mutex does not help.
/
/ ********* Send a 433 MHz signal ***********************
// **** by triggering an interrupt on a pin ***************
const byte ledPin = 16;
const byte interruptPin = 4;
volatile long lastDebounceTime = 0;
const int debounceDelay = 500;
volatile boolean blockOthers = false;
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin,LOW); // HIGH by default
pinMode(interruptPin, INPUT_PULLUP);
// attachInterrupt(interruptPin, stoptx, RISING);
attachInterrupt(interruptPin, transmit, FALLING);
// Transmitter is connected to Arduino Pin #D5 (GPIO14)
mySwitch.enableTransmit(14);
}
void loop() {
/*
boolean pressed = !digitalRead(4);
if(pressed){
digitalWrite(ledPin,HIGH);
} else {
digitalWrite(ledPin,LOW);
}
*/
}
void transmit() {
if (blockOthers) return;
blockOthers=true;
// Check to see if the change is within a debounce delay threshold.
boolean debounce = ((millis() - lastDebounceTime) <= debounceDelay);
// This update to the last debounce check is necessary regardless of debounce state.
lastDebounceTime = millis();
// Ignore reads within a debounce delay threshold.
if(debounce) {
blockOthers = false;
return;
}
mySwitch.sendTriState("00000FFF0FFF"); // = 5397
// mySwitch.send(5397, 24);
blockOthers = false;
}
void stoptx() {
mySwitch.send(5396, 24);
}