I have a (physical) button on a pin and when I press it I want the ESP to generate a 433 MHz rc signal and transmit it. I have got a basic version up and working,
BUT
1 - I need some way to debounce the key - a simple press usually generates two signals, sometimes more, very rarely a single one. This is not acceptable, as some signals are used for toggles. Is there a canonical way in Arduino to deal with debounce?
2 - I cannot attach two interrupts to a sigle pin (say, one for pressed and one for released) If I use this code
attachInterrupt(interruptPin, stoptx, RISING);
attachInterrupt(interruptPin, transmit, FALLING);
3- The basic reason I want to monitor both press and release, is so that I can have an LED switching on while I'm pressing. In the end I used a read in the main loop, as shown in code below, but the response times are pathetically slow. A quick press won't show at all, a longer has about a half a second delay, although I use no delay() calls, as you can see. What would be a good way to have the LED reflect my pressing in real time? Any example code?
Many, many thanks
// ********************************************************
// ********* Send a 433 MHz signal ***********************
// **** by triggering an interrupt on a pin ***************
// **** AG - June 2017 ***************
// ********************************************************
//
// (using crappy reject WeMOS D1 (1.0) board, rather than chucking)
const byte ledPin = 16;
const byte interruptPin = 4;
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
// attachInterrupt(interruptPin, stoptx, RISING);
attachInterrupt(interruptPin, transmit, FALLING);
// Transmitter is connected to Arduino/NodeMCU pin GPIO14/D5 ; WeMOS D1 - pin 13/SCK
mySwitch.enableTransmit(14);
digitalWrite(ledPin,LOW);
}
void loop() {
boolean pressed = !digitalRead(4);
if(pressed){
digitalWrite(ledPin,HIGH);
} else {
digitalWrite(ledPin,LOW);
}
}
void transmit() {
mySwitch.sendTriState("00000FFF0FFF"); // = 5397
// mySwitch.send(5397, 24);
}
void stoptx() {
mySwitch.send(5396, 24);
}
======
tl;dr **
Normally I would implement this in NodeMCU/Lua without too much thought.
rc-transmit libraries are available for NodeMCU (unlike rc-receive where I had to use Arduino)
However, I happen to have one of those ancient WeMOS D1 bagatelles -- which will not work with NodeMCU/Lua. I wasted too much time trying... It does works under Arduino, though, so I thought rather than chucking it away, I'd use it for a simple task like this. Waste not, want not, or so they say. Hmmm...?
Also, I thought this way I'd have an opportunity gain a bit more Arduino experience.
Ultimately, I would like to have a list of signal-codes stored in SPIFFS, have the current one displayed on one of those I2C mini-OLED displays, and be able to select between them with a couple of scrolling buttons. This would be a sort of portable test unit for my main project (which is a 433MHz/MQTT security application) so it [i]must[/have] physical buttons.
So, clearly, I need to get some mastery of buttons and interrupts