1 GPIO pin on esp8266 for some Physical Button and set up some LEDs?

Why do you need analog pin?

Do you need to identify each button/switch? Does each button/switch need to turn on a specific led? Do they need to function independent of each other (i.e. be able to turn on more then one led at a time)?

I think you need to make clear what you are trying to accomplish, and how it should function first. Otherwise we will not be able to provide accurate help.

Also, you should take some time to understand some of the fundamentals of BLYNK

Also, depending on your level of programming knowledge, you may benefit from watching a few YouTube videos on Arduino programing Basics.

oh sorry wkwkwkwk, this is just a misunderstanding of language, I don’t really understand English well and correctly. thank you I have understood a little what you are talking about. I have modified my code.

I have succeeded, thank you all. : D

@projecttrinof: should you try this one below as I revised it a bit for you. Then if it is the one you want or you could continue to adjust as per your requirement … then go further with Blynk vitual pins. Hope this helps and good luck !

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "asasas"; //insert here your token generated by Blynk
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "asasa";
char pass[] = "asasa";

//Constants
const int led1 = 12;
const int led2 = 14;

//Variables
int buttonValue; //Stores analog value when button is pressed

BlynkTimer timer1;
bool btnAct = false;

void physicalbtnPressed()
{
  
  buttonValue = analogRead(A0); //Read analog value from A0 pin

  if (buttonValue <= 60) btnAct = false;

  //For 1st button:
  if (buttonValue >= 532 && buttonValue <= 545 && btnAct == false){
    btnAct = true;
    digitalWrite(led1, !digitalRead(led1));
  }
  //For 2nd button:
  else if (buttonValue >= 441 && buttonValue <= 498 && btnAct == false){
    digitalWrite(led2, !digitalRead(led2));
  }
  else {
    btnAct = true;
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
  }
  Serial.println(buttonValue);
}


void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

  digitalWrite(led1, LOW); // set defalt value for led1
  digitalWrite(led2, LOW); // set defalt value for led2

  timer1.setInterval(100L, physicalbtnPressed);

}

void loop()
{
  timer1.run();
  Blynk.run();
}

The above code I suppose you are doing pull down for ADC pin it means its value fluctuates less 80 …but if you want to pull it up then you could revise the variable btnAct as follows:

if (buttonValue > 975) btnAct = false;

replaces this one

if (buttonValue <= 60) btnAct = false;

Please edit your code and delete the custom server. I have managed to make this with my own code, but I will try it and will be very grateful. and I will tell you the results.

Saya kahabisan PIN jiga akan menggunakan GPIO digital PIN

@dunghnguyen It turns out that the code you provided was unsuccessful - but thank you very much

If this is useful for others, I have successfully used this code

    #define BLYNK_PRINT Serial

    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "sempak"; //insert here your token generated by Blynk
    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "sempak";
    char pass[] = "sempak";

    const int led1 = 12;
    const int led2 = 14;

    BlynkTimer timer;
    void checkPhysicalButton();

    int led1state = LOW;
    int led2state = LOW;
    int buttonState = 0;
    int buttonValue1;
    int buttonValue2;

    // Every time we connect to the cloud...
    BLYNK_CONNECTED() {
      // Request the latest state from the server
      Blynk.syncVirtual(V0);
      Blynk.syncVirtual(V1);
      // Alternatively, you could override server state using:
      //Blynk.virtualWrite(V0, led1state);
      //Blynk.virtualWrite(V1, led2state);
    }

    // When App button is pushed - switch the state
    BLYNK_WRITE(V0) {
      led1state = param.asInt();
      digitalWrite(led1, led1state);
    }
    BLYNK_WRITE(V1) {
      led2state = param.asInt();
      digitalWrite(led2, led2state);
    }
    void checkPhysicalButton()
    {
    buttonValue1  = analogRead(A0);
    buttonValue2  = analogRead(A0);

      if (buttonValue1>=532 && buttonValue1<=545) {
        // btnState is used to avoid sequential toggles
        if (buttonState != 0) {

          // Toggle LED state
          led1state = !led1state;
          digitalWrite(led1, led1state);

          // Update Button Widget
          Blynk.virtualWrite(V0, led1state);
        }
        buttonState = LOW;
      }
      else if (buttonValue2>=441 && buttonValue2<=498) {
        // btnState is used to avoid sequential toggles
        if (buttonState != 0) {

          // Toggle LED state
          led2state = !led2state;
          digitalWrite(led2, led2state);

          // Update Button Widget
          Blynk.virtualWrite(V1, led2state);
        }
        buttonState = LOW;
      } else {
        buttonState = HIGH;
      }
    }

    void setup()
    {
      // Debug console
      Serial.begin(9600);

      
      // You can also specify server:
      Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
      //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

      pinMode(led1, OUTPUT);
      pinMode(led2, OUTPUT);
      digitalWrite(led1, led1state);
      digitalWrite(led2, led2state);
      // Setup a function to be called every 100 ms
      timer.setInterval(100L, checkPhysicalButton);
    timer.setInterval(100L, []() {  // Run every 100 milliseconds
    Serial.println(analogRead(A0));  // Display the value
     });
    }

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

This should be replace with a timer instead… delay() of any sort is not a good idea in the main loop

Give you an example? :smiley:


void checkPhysicalButton()
    {
    buttonValue1  = analogRead(A0);
    buttonValue2  = analogRead(A0);
    Serial.print("buttonValue1 =");
    Serial.println(buttonValue1);
    Serial.print("buttonValue2 =");
    Serial.println(buttonValue2);
    Serial.print("analogRead(A0) =");
    Serial.println(analogRead(A0));

...


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

although I suspect analogRead(A0), buttonValue1 and buttonValue2 will always be the same.

I have lots of timer references here…

Even a compact Lambda option…

  // Timed Lambda Function - Run in Void Setup()
  timer.setInterval(100L, []() {  // Run every 100 milliseconds
    Serial.println(analogRead(A0));  // Display the value
  });  // END Timer Function
1 Like

ohh thank you very much @Gunner

I have just revised the main part for you. The rest job you should work to continue “fine-tuning” it. I have done for this with ADC pin running well for 2 years already. The above is the basic that if you understand its code then you can be revising it mapping with your schematic. There will be much job ahead that only you get it if some further experiment to be done.

Last but not least, you dont just copy the threshold values from the link I tagged in the above post … did you? If you did it is incorrect threshold for your board. Those threshold values should be developed by you and your schematic.

Yes they are since they belong to the same loop at that moment buddy or because of your schematic.

@dunghnguyen I am very thankful to you friends, but I still cannot modify my schematic to be juxtaposed with the code that you made, while I enjoy my own code, but if you would like to provide an example schematic that matches your code, I would be very happy to try it.

@projecttrinof: no issue at all. Let enjoy what you have then next step you / yourself finds out something new then you will know how to move. Everyone is the same at the first step as long as we start something haha :grinning:

@dunghnguyen oh, guys, usually you always show me a bright path, but why this time you make me even more curious, please give me a little clear path about the schematic that matches the code from you. I haven’t applied this for a long time when I made the topic here, I was afraid of many problems with the problem of the resistor value. So I also want to try the code from you

Let continue with your code since it is ok now for your case until you cannot move next with further requirement.

I spent minutes to see your resistors and their wiring as you pull the ADC pin down. Hence I suggest the code that I sent you. Let spend your time to get it thru then applying with your schematic. It is just a basic for you to start with ADC but when doing you will get how the whole story to be and going on and it is not all …there will be a lot issues ahead if you want to be “master” on this ADC application.

SOLVED! Thanks All. :pray::pray::pray::pray: