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

Can you share the code here. Thanks.

So which is it? Switch or momentary button?

The example I had pointed you to, works with a momentary button. Press once led ON, press again led OFF.

#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[] = "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


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

}

void loop()
{
  buttonValue = analogRead(A0); //Read analog value from A0 pin
  
  //For 1st button:
  if (buttonValue>=532 && buttonValue<=545){
    digitalWrite(led1, HIGH);
  }
  //For 2nd button:
  else if (buttonValue>=441 && buttonValue<=498){
    digitalWrite(led2, HIGH);
  }
  else{
    digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
  }
  Serial.println(analogRead(A0));
  delay(100);
  Blynk.run();
}

Yes, I succeeded with the example you showed, when I used Digital Pin, and it didn’t work when I switched to Analog Pin.

—> blynk.iot-cm.com <—

An unsupported cloud-based private server system running an unknown version of the Blynk server software. Exposing all of your data to the website owner, and anyone else he may choose to sell it to.

Good luck with that :roll_eyes:

Pete.

1 Like

sorry I have nothing to do with that, I deleted it.

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.