Implementing Multiple Widgets Properly in Arduino

Hi All,

I’ll try and make this clear. I’m looking to setup a little program that takes the value from a couple of sliders on the Blynk app and implements them on an embedded chip (NodeMCU ESP8266) while developing with the Arduino IDE.

I’ve been able to get the basic functionality of a slider widget outputting its values to the board which is then driving a circuit with an analogWrite(function). It looks something like this:

   #define BLYNK_PRINT Serial

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

    char auth[]= "#####################";
    char ssid[] = "###########";
    char pass[] = "#########";

    int ledPin= 16;

    BLYNK_WRITE(V3){
        analogWrite(ledPin,param.asInt());
    }

    void setup() {

      Serial.begin(115200);
      // put your setup code here, to run once:
      Blynk.begin(auth,ssid,pass);

    }

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

And that’s all well and good. But what I’m looking to do is add another slider widget, as well as some buttons, and really adjusting what I can do! I want one slider to control the amplitude (which is working great as the implementation above) while another controls the frequency of the function I choose to output!

My initial thought was something like this:

BLYNK_WRITE(V3){
  int amplitude = param.asInt();
}

BLYNK_WRITE(V6){
    int frequency = param.asInt();
}

void pulse(){
  int delayValue = 255-frequency;
  analogWrite(ledPin,amplitude);
  delay(delayValue*10);
  analogWrite(ledPin,0);
  delay(delayValue*10);
  }

But that’s throwing the error:

frequency was not declared in this scope

I think my main issue is that I’m struggling with ordering these functions so that I can use them properly. My end goal is to have several button widgets which will select the function pattern, a slider for the frequency, and a slider for the amplitude. As it stands, however, I’m kind of at a loss of how to order functions, what values I can and can’t reference (and where I can and can’t) within the complete code. Any help is hugely appreciated, let me know if I can provide any more information or clarify what I have here!

That is because you are not declaring the variable frequency as global. Thus when the IDE sees the same variable in the void pulse(), it doesn’t know what to make of it.

PS the same thing will happen with the variable amplitude

BTW this error is NOT a blynk specific issue… so I recommend you also study up on basic Arduino commands and structures…

1 Like

As for the order of things… those BLYNK_WRITE() functions will be called everytime the associated widget changes state, but in order to have your void pulse() run we recommend using timers instead of having those functions called directly from the void loop so as not to push all that data hundreds of times a second.

1 Like

Haha yup - shouldn’t be working on things so late at night. Silly mistake I caught almost immediately after posting this and heading off. Thanks.

Also, thank you for pointing me in the direction of the timer controlled function usage. Definitely had not stumbled across that info yet!