-->
Page 1 of 1

New Noob

PostPosted: Thu Jan 05, 2017 1:49 pm
by Drpepper
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);
}

Re: New Noob

PostPosted: Fri Jan 06, 2017 10:39 am
by Ribeiro Santos
Why use a timer instead of just use the loop()?

Re: New Noob

PostPosted: Fri Jan 06, 2017 3:46 pm
by Drpepper
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 :)