Reading Multiple Pin status to Blynk2.0 app

My question, this part of code works 100% for One pin read to app, I can not get to adding more pins tom read sucessfully.
I am stil very new to this.

Here is the code:


int prevState = -1;

int currState = -1;

long lastChangeTime = 0;

void checkPin()

{

    // Invert state, since button is "Active LOW"

      int state = !digitalRead(5);

   // Debounce mechanism

      long t = millis();

   if (state != prevState) {

      lastChangeTime = t;

   }

    if (t - lastChangeTime > 50) {

      if (state != currState) {

         currState = state;

    Blynk.virtualWrite(V1, state);

    }

    }

   prevState = state;

}

// This function sends Arduino's uptime every second to Virtual Pin 2.

void myTimerEvent()

{

  // You can send any value at any time.

  // Please don't send more that 10 values per second.

  Blynk.virtualWrite(V10, millis()/60 / 1000);

}

BLYNK_WRITE(V0)

{

  int pinValue = param.asInt(); 

  digitalWrite(16,!pinValue);  //D0 OUTPUT

}

BLYNK_WRITE(V4)

{

  int pinValue = param.asInt(); 

  digitalWrite(2,!pinValue);  //D4 OUTPUT

}

BLYNK_WRITE(V7)

{

  int pinValue = param.asInt(); 

  digitalWrite(13,!pinValue);  //D7 OUTPUT

}

BLYNK_WRITE(V8)

{

  int pinValue = param.asInt(); 

  digitalWrite(15,!pinValue);  //D8 OUTPUT

}

void setup()

{

  // Debug console

  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

  pinMode(16,OUTPUT);  //D0

  pinMode(2,OUTPUT);   //D4

  pinMode(13,OUTPUT);  //D7

  pinMode(15,OUTPUT);   //D8

  pinMode(5,INPUT_PULLUP);    //D1

  pinMode(4,INPUT_PULLUP);    //D2

  pinMode(14,INPUT_PULLUP);    //D5

  pinMode(12,INPUT_PULLUP);    //D6

  timer.setInterval(1000L, myTimerEvent);

  }

void loop()

{

  Blynk.run();

  timer.run();

  checkPin();
}

It looks like you need to add another pin (V2?) To your checkpin function.

I have added in various manners,
this is the code that does atleast let me read the V1, other ways fail.
Tried duplicating the checkPin section, apic fail.

Any advice?

To start with i would just copy the virtualWrite line, paste it right after the first and change V1 to V2.

That would only write the same value to V2 as V1 as it still the same PIN being read

Eureka!!!
Added this before Void Setup:

int prevState = -1;
int currState = -1;
long lastChangeTime = 0;

int prevState2 = -1;
int currState2 = -1;
long lastChangeTime2 = 0;

int prevState3 = -1;
int currState3 = -1;
long lastChangeTime3 = 0;

int prevState4 = -1;
int currState4 = -1;
long lastChangeTime4 = 0;

Then I moved the code into Void loop and made changes as follow:

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

  //Reads Pin5 to V1 in Value Display within Blynk
  {
    // Invert state, since button is "Active LOW"
      int state = !digitalRead(5);
   // Debounce mechanism
      long t = millis();
   if (state != prevState) {
      lastChangeTime = t;
   }
    if (t - lastChangeTime2 > 50) {
      if (state != currState) {
         currState = state;
    Blynk.virtualWrite(V1, state);
    }
    }
   prevState = state;
  }

  //Reads Pin4 to V2 in Value Display within Blynk
  {
    // Invert state, since button is "Active LOW"
      int state = !digitalRead(4);
   // Debounce mechanism
      long t = millis();
   if (state != prevState2) {
      lastChangeTime2 = t;
   }
    if (t - lastChangeTime2 > 50) {
      if (state != currState2) {
         currState2 = state;
    Blynk.virtualWrite(V2, state);
    }
    }
   prevState2 = state;
  }

  //Reads Pin14 to V5 in Value Display within Blynk
  {
    // Invert state, since button is "Active LOW"
      int state = !digitalRead(14);
   // Debounce mechanism
      long t = millis();
   if (state != prevState3) {
      lastChangeTime3 = t;
   }
    if (t - lastChangeTime3 > 50) {
      if (state != currState3) {
         currState3 = state;
    Blynk.virtualWrite(V5, state);
    }
    }
   prevState3 = state;
  }

    //Reads Pin12 to V6 in Value Display within Blynk
  {
    // Invert state, since button is "Active LOW"
      int state = !digitalRead(12);
   // Debounce mechanism
      long t = millis();
   if (state != prevState4) {
      lastChangeTime4 = t;
   }
    if (t - lastChangeTime4 > 50) {
      if (state != currState4) {
         currState4 = state;
    Blynk.virtualWrite(V6, state);
    }
    }
   prevState4 = state;
  }
}


That was the point. Prove that you can do two virtual pins first, irrespective of the values.

However, I see that you figured it out - nicely done!

Thank you, been at this for days, persistence paid off.
I see you can add multiple virtual Pins through a voltage divider circuit on a single Input, do you have any knowledge of it maybe?

You can use analogRead on one analog pin to get a voltage reading from several buttons where you connect each one with a different resistance to tell which is which.

It gets trickier though if you plan to press more than one at a time.

Okey,so it is only usable on the analog then... So if in theory those buttons are 'interlocked' it could unlock a few more pins.
I am using an WeMos D1 R2 board which uses the ESP8266 chip, are there any other boards available with WiFi integrated which could provide more IO's?

I have never used one, but the ESP32 comes up here frequently, maybe take a look.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.