DigitalRead problems

Hi, im trying to turn up a Lightbulb with a pushbotton that gives me digital input to my NodeMCU, it works but not perfectly, my problem is that deppending on the click i do to the botton i can get as an OUTPUT 2,3,4, maybe 5 changes of state, so if i get 3 changes that will be: ON,OFF,ON (so it stays ON) and some time i get 2 or 4 and it does turns OFF, its like a 50/50 prob.
I think there is a way i can use Simple.Timer to activate just 1 time the interruption, but i cant make it work.
Here is my code: (Cant make it look nice, sorry)

    #define BLYNK_PRINT Serial
    #include <ESP8266WiFi.h>
    #include <TimeLib.h>
    #include <BlynkSimpleEsp8266.h>
    BlynkTimer  timer;

   
    char auth[] = "********************";


    char ssid[] = "INFINITUMF810_2.4";
    char pass[] = "*********";




    volatile bool pinChanged2 = false;
    volatile int  pinValue2   = false;



    int pin1 = D1;
    int pin2 = D2;

      


    void checkPin2()
    {

      
      // Invert state, since button is "Active LOW"
      pinValue2 = ~pinValue2;//digitalRead(pin);

      // Mark pin value changed
      pinChanged2 = true;
    }


     
    void setup()
    {
      // Debug console

      pinMode(pin1, OUTPUT);
      pinMode(pin2, INPUT);

      
      // Debug console
      Serial.begin(9600);

      
      Blynk.begin(auth, ssid, pass);
      
      // Attach INT to our handler

      attachInterrupt(digitalPinToInterrupt(pin2), checkPin2, HIGH);

     
    }

    BLYNK_WRITE(V2) {
     int btnpin = param.asInt();
         digitalWrite(pin1, btnpin);
         
        
         
    }
    void TurnPin2(){
      Serial.println("Encender pin2");
        if (pinValue2) {
          
               digitalWrite(pin1, LOW);
               Blynk.virtualWrite(V2,LOW);
        } else {
          
          digitalWrite(pin1, HIGH);
          Blynk.virtualWrite(V2,HIGH);
        }
        pinChanged2 = false;
    }

    void loop()
    {
     Blynk.run();
     timer.run();

      if (pinChanged2) {
        
        timer.setTimeout(200L, TurnPin2);

      }
      
        

       
      
     
      
    }

@Rodrigo_Rochin with a lot of trial and error you could use BlynkTimer but easier to Google “Arduino button debounce”.

1 Like

Worked perfectly, thanks!!

I got a new problem related to this topic, now the push botton works correctly, but now i can´t turn on the pin with the app because the code that allows the botton to work goes in the loop, so if the state of the digital.Read botton is OFF, it doesn,t matter if i change it with the global variale, because it stays off.
i already tried in removing the code from de loop and just call it with an interruption to a new function but it doesn´t work, any ideas of how i could change de ledState depending on if i push the botton or if i click in the app?
CODE:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <TimeLib.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer  timer;

char auth[] = "******";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "INFINITUMF810_2.4";
char pass[] = "*****";

const int buttonPin = D2;    // the number of the pushbutton pin
const int ledPin = D1;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
void setup() {
 pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);

   Serial.begin(9600);

  
  Blynk.begin(auth, ssid, pass);

}

BLYNK_WRITE(V2) {
 int btnpin = param.asInt();
     digitalWrite(ledPin, btnpin);
}


 


void loop() {
   Blynk.run();
   

 int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);
  Blynk.virtualWrite(V2,ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}

You need to move it.

Look up attachInterrupt sketches here and on Arduino sites.

I got this, but it never enters the interruption. :

void setup() {
 pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);

   Serial.begin(9600);

  
  Blynk.begin(auth, ssid, pass);
attachInterrupt(digitalPinToInterrupt(buttonPin), checkPin2, HIGH);
}

BLYNK_WRITE(V2) {
 int btnpin = param.asInt();
     digitalWrite(ledPin, btnpin);
}

void checkPin2()
{
  Serial.println("CheckPin2");
 int reading = digitalRead(buttonPin); 

…

Try CHANGE instead of HIGH but with ESP’s they are very flakey with interrupts i.e. will crash your system.

detachInterrupt as the first line of checkPin2() and attach again as the last line of the function.

i got this:

void checkPin2()
{
  detachInterrupt(digitalPinToInterrupt(D2));
  Serial.println("CheckPin2");
 int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);
  Blynk.virtualWrite(V2,ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
  
 attachInterrupt(digitalPinToInterrupt(D2), checkPin2, HIGH);
}

But i got my module disconected with every push on the botton.