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)
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;
}
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.