-->
Page 1 of 2

Attaching interrupts (Solved - it was an RCSwitch problem)

PostPosted: Sun Jun 11, 2017 7:38 pm
by alex_g
I have a very simple project which I want to implement in Arduino. (See tl;dr below** for boring reasons why)

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
Code: Select all  attachInterrupt(interruptPin, stoptx, RISING);
  attachInterrupt(interruptPin, transmit, FALLING);
the first one gets "overwritten" so only the second one works. Is there some way to have both?
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



Code: Select all// ********************************************************
// ********* 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 :)

Re: Attaching interrupts to pins (some problems)

PostPosted: Sun Jun 11, 2017 9:07 pm
by alex_g
OK, I found this for debouncing, https://gist.github.com/erikpena/a01ace32f7bef4a5ec52
so unless there's something wrong with it, or there's a better recommended "standard" way, I'm happy to go with it for now.

Still looking for solutions to lighting a LED in parallel to the button press.

Re: Attaching interrupts to pins (some problems)

PostPosted: Sun Jun 11, 2017 9:45 pm
by alex_g
Oh dear, I don't know what's wrong, even after adding this code I still get two button presses registering each time!

Code: Select allvolatile long lastDebounceTime = 0;
const int debounceDelay = 100;
void transmit() { 
   // 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) return; 
 
  mySwitch.sendTriState("00000FFF0FFF");  // = 5397
  // mySwitch.send(5397, 24);
}


I know this because I am monitoring the transmissions on an ESP-433MHz-snooper-MQTT module running alongside.

===

Yup, it's really peculiar: I can put the debounceDelay time up to 200ms, and it still does it!
That doesn't seem like a debounce problem. It seems like the transmit() function got called twice (and ignored the ebounceDelay values).

I'm a bit stumped now, all ideas welcome!

Re: Attaching interrupts to pins (some problems)

PostPosted: Mon Jun 12, 2017 5:48 am
by QuickFix
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