Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Drpepper
#60489 Hello Everyone.
I wanted to say thank you to those that helped me get started.
Anyway, hopefully once I can get up to speed, I can start contributing...
I have just spun up an OpenHab, and MTQQ servers. My first node is going to be the Three Gang Switch box at the front door. Right now I am working with the ESP8266 ESP-1 but I have a 12 in the mail for the production build. One of the plans is to make some small 25mm plastic disks into pressure switches, so I will have to deal with Button Bounce.
This is my first shoot at a Software Button Bounce, Please critique it, and let me know what you think.
I did not see anyone else take this approach.


Code: Select all#include "user_interface.h"

#define BOARD_LED_PIN   2
#define BOARD_BUTTON_PIN   0

os_timer_t myTimer;

// start of timerCallback
void timerCallback(void *pArg) {
      attachInterrupt(BOARD_BUTTON_PIN, SwitchLight, FALLING );
}

// Process interrupt
void SwitchLight() {
   Serial.println("Button Pressed...");
   detachInterrupt(BOARD_BUTTON_PIN);
   digitalWrite(BOARD_LED_PIN,!digitalRead(BOARD_LED_PIN));
   os_timer_arm(&myTimer, 1000, false);
}

void setup() {               
  os_timer_setfn(&myTimer, timerCallback, NULL);
  pinMode(BOARD_LED_PIN, OUTPUT);
  pinMode(BOARD_BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Still Alive...");
  attachInterrupt(BOARD_BUTTON_PIN, SwitchLight, FALLING);
  Serial.print("BOARD_BUTTON_PIN ");
  Serial.println(digitalRead(BOARD_BUTTON_PIN));
  Serial.print("BOARD_LED_PIN ");
  Serial.println(digitalRead(BOARD_LED_PIN));
  delay(2000);
}
User avatar
By Drpepper
#60562 What I have seen as examples had the Software Delay in the Interrupt Function, And I think that's a Bad Idea.
The reason I did not put it in the loop is I wanted better control of the time.
if the loop is midway thru it's pulse, then the attach could be as much as 50%+- from the desired wait.
Plus, I treat the Interrupt as a Multithread event. and you should never cross the streams :)