How can i switch between function?

Continuing the discussion from How can I run BLYNK_WRITE in a loop?:

Before creating the topic

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "MAX30100_PulseOximeter.h"
#define REPORTING_PERIOD_MS     100
#include <SoftwareSerial.h>
SoftwareSerial portTwo(15, 13); //TX,RX

char auth[] = "zfdfML-j0uRZGO2OZMI5H87B1vUS1zSD";    // You should get Auth Token in the Blynk App.
char ssid[] = "Ajay_Home";                       // Your WiFi credentials.
char pass[] = "Alph@##1307";

BlynkTimer timer;

PulseOximeter pox;

uint32_t tsLastReport = 0;
int SpO2 = 0;
int bt_status ;
char sbuffer[30], ch;
unsigned char pos;
int read1 = 0;
int read2 = 0;
int read3 = 0;
int pinValue ;

//BLYNK_CONNECTED() {
//  //get data stored in virtual pin V0 from server
//  Blynk.syncVirtual(V6);
//}

BLYNK_WRITE(V6)
{
  int pinValue = param.asInt();
  Serial.println(pinValue);
}

char mygetchar(void)
{
  while (!portTwo.available());
  return portTwo.read();
}

void onBeatDetected()
{
  Serial.println("Beat!");
}

void Spo2(void) {
  pox.update();
  SpO2 = pox.getSpO2();
  if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
    Serial.print("SpO2:");
    Serial.print(SpO2);
    Serial.print("%");
    Serial.println();
    Blynk.virtualWrite(V4, SpO2);

    tsLastReport = millis();
  }
}

void Blood(void) {

  ch = mygetchar();
  if (ch == 0x0A)
  {
    pos = 0; // buffer position reset for next reading
    read1 = ((sbuffer[1] - '0') * 100) + ((sbuffer[2] - '0') * 10) + (sbuffer[3] - '0');
    read2 = ((sbuffer[6] - '0') * 100) + ((sbuffer[7] - '0') * 10) + (sbuffer[8] - '0');
    read3 = ((sbuffer[11] - '0') * 100) + ((sbuffer[12] - '0') * 10) + (sbuffer[13] - '0');

    Serial.println("*****************");
    Serial.print("SYSTOLIC:");
    Serial.print(read1);
    Serial.print('\n');
    Serial.print("DYSTOLIC:");
    Serial.print(read2);
    Serial.print('\n');
    Serial.print("PULSE");
    Serial.print(read3);
    Serial.print('\n');
    Serial.println("*****************");

    Blynk.virtualWrite(V1, read1);
    Blynk.virtualWrite(V2, read2);
    Blynk.virtualWrite(V3, read2);

  }
  else { //store serial data to buffer
    sbuffer[pos] = ch;
    pos++;

  }

}

void Both_sens(void) {

  if (pinValue == 0) {
    Spo2();
  }
  else {
    Blood();
  }
}


void setup() {
  Serial.begin(9600);
  portTwo.begin(9600);
  Blynk.begin(auth, ssid, pass);

  if (!pox.begin()) {
    Serial.println("FAILED");
    for (;;);
  } else {
    Serial.println("SUCCESS");

  }

  pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
  pox.setOnBeatDetectedCallback(onBeatDetected);


  //  timer.setInterval(1000, Blood);
    timer.setInterval(10, Both_sens);
}

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


}


hello there I am working a project I wanted to switch between two functions. in the above code I used button widget for switching but it executing once only … see the above “’'Both_sens” function it excuites once when I dump the code to esp , and I even tried Blynk.syncVirtual(); it was showing correct status of the button but in both_sens function if and else condition not working … please help me with this

If you added a line of code before the if/else condition to print the value of PinValue to the serial monitor, you would see that pinValue is always zero.
That’s because your BLYNK_WRITE(V6) function is re-declaring pinValue as a local variable. Remove the int before pinValue in that function and the if/else function will work correctly.

However, the likelihood of your code working as desired is quite slim, as calling these nested functions at 10ms intervals is probably beyond the capabilities of the hardware.

Pete.

hello peteknight sir
i changed as you say but its between 0 and 1 once a time … let assume the V6 at state 0 on that my if statement worked and when I turn that button it become 1 . basically what I want is it should switch between 1 and 0 simultaneously

and i am not getting anyother way to switch between them .

BLYNK_WRITE(vPin) triggers once only when the value of the widget changes, that’s how it’s designed to work.

You are calling Both_sens every 10ms with a timer, which should result in a loop effect, but as I said I think 10ms is too frequent.

Pete.

no sir i simply kept 10ms . actually it was 1000L …
could you please tell me which are the other possible ways that I can make this project efficient …