Issue Reading Button
Posted: Tue Nov 23, 2021 4:08 am
Here is my code below. I am using the Easybutton library for reading the button state. Problem is that in the loop function, even when I press my button for 2 seconds, it does not detect that and takes more time to detect, and even the detection is unreliable. I am sure there is a problem in my code, just can't figure it out. Please help.
Code: Select all
#include <ESP8266WiFi.h>
#include "ESP8266Ping.h"
#include <EasyButton.h>
#define MOSFET 10
#define SWITCH 0
#define DETECT A0
#define LED 13
#define GLED 5
#define DEBUG True
EasyButton button(SWITCH);
// REMOVED SOME PART OF CODE FOR READABILITY
const char* google_ip= "www.google.com";
bool isButtonPressed = false;
void onPressedForDuration();
void led_blink();
void setup() {
pinMode(SWITCH, INPUT); // Initialize the GPIO2 pin as an output
pinMode(MOSFET, OUTPUT); // Initialize the GPIO0 pin as an output
pinMode(DETECT, INPUT);
pinMode(LED, OUTPUT);
pinMode(GLED, OUTPUT);
digitalWrite(MOSFET, LOW); // Turn MOSFET ON (Note that LOW or HIGH is the voltage level
digitalWrite(LED, LOW);
digitalWrite(GLED, HIGH);
button.begin();
button.onPressedFor(2000, onPressedForDuration);
}
void loop() {
button.read();
digitalWrite(GLED, HIGH);
while (!isButtonPressed)
{
while(wifiManager.autoConnect() == false)
{
button.read();
digitalWrite(LED, HIGH);
delay(2000);
digitalWrite(LED, LOW);
delay(2000);
cicle_MOSFET();
}
while(googleFails < googleFailsMax and yahooFails < yahooFailsMax){
button.read();
//delay between two check cicle
delay_check();
#ifdef DEBUG
Serial.print("Pinging vpn Server ");
Serial.println(google_ip);
#endif
if(Ping.ping(IPAddress(8,8,8,8))){
button.read();
digitalWrite(GLED, LOW);
#ifdef DEBUG
Serial.println(Ping.averageTime());
button.read();
Serial.print("Success! Will try ping again in ");
Serial.print(minuteBetweenCheck);
Serial.println(" minutes");
button.read();
#endif
}
else {
digitalWrite(GLED, HIGH);
button.read();
#ifdef DEBUG
Serial.println("PING FAILED!");
Serial.print("Incrementing vpnFail count to ");
button.read();
Serial.println(googleFails);
#endif
++googleFails; // Increment googleFails counter
if (!Ping.ping(IPAddress(1,1,1,1))){
button.read();
yahooFails++;
}
}
}
cicle_MOSFET();
}
}
void onPressedForDuration()
{
isButtonPressed = true;
Serial.println("BUTTON PRESSED . BUTTON PRESSED FOR 5 SECONDS.");
delay(2000);
led_blink();
Serial.println("Button pressed");
}
void led_blink()
{
int i = 0;
while(i < 3)
{
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
digitalWrite(LED, HIGH);
i = i + 1;
}
}
void delay_check() {
while(eslapedCheck < minuteBetweenCheck*60) {
eslapedCheck++;
delay(100);
}
eslapedCheck = 0;
}
void cicle_MOSFET() {
Serial.println("CIRCLING MOSFET. CIRCLING MOSFET.");
digitalWrite(MOSFET, HIGH); // Turn MOSFET OFF
while(flashing < 5) {
delay(50); // Delay 100 ms * 100 = 10 seconds
flashing++;
delay(50); // Delay 100 ms * 100 = 10 seconds
led_blink();
Serial.println("CIRCLING MOSFET. CIRCLING MOSFET.");
}
digitalWrite(MOSFET, LOW); // Turn MOSFET ON
digitalWrite(LED, LOW);
}