Post topics, source code that relate to the Arduino Platform

User avatar
By Amorphous
#92893 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); 
}
User avatar
By Inq720
#92898 I did not read your entire code and I have no idea what an Easybutton is. I tend to roll my own. I don't want to insult you. You may be a years expert and this is like when the Dell tech support asks you if your computer is plugged in. So this is assuming your are just starting out.

One thing that stood out looking at your code is ... Is you're
#define SWITCH 0

For ESP8266 boards the numbers stenciled on the board are not the same as code values. There are constants set up under the covers for you depending on what board type you're using that are taken care of when you use the proper board setting in the Arduino IDE.

The standard way of handling this is... if you're switch is physically connected to the pin label D0, set your
#define SWITCH D0 // D0 is actually mapped to GPIO16.

The non-standard way if you really want to use #define SWITCH 0, is make sure your switch wire is connected to pin marked D3. Here is a user friendly chart about 1/3rd the way down the page...
https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/

If I'm all wrong, reply and I'll take a harder look at your code.

Good luck
User avatar
By btidey
#92906 The pin labelling depends on which board is being used. NodeMCU / Wemos do use Dn numbering and one needs to use the Dn reference as suggested or map them manually to the GPIO numbering. Boards like ESP-!2F label their pins with the real GPIO numbers and then it is quite legitimate to use #define SWITCH 0 which then refers to GPIO00.

The code as written attempts to use GPIO10 for MOSFET. That is a complete no-no as GPIO10 is part of the flash ROM interface where code is read from.

The code shown is also not complete as there are several variables not declared.

There is also the use of 'and' to perform logic rather than && which should produce compilation errors unless 'and' itself has been defined which would be strange.
User avatar
By rpiloverbd
#92940 May be it's debouncing problem which usually can be solved by adding a small delay. Or may be the microcontroller cannot differentiate between logic low and high. From your code only, I could not understand whether your button is Pulled up or pulled down. It may help if you share the diagram of the button part of your circuit.