SOLVED - Threshold value not updating from Numeric Input in Blynk App?

Hey!

I am after some help if anyone can. I have a NodeMCU board I am using to learn some basics. I have currently got everything set up with a rain detector, so it reads the A0 pin and sends the data to Blynk. I also have it trigger an event (notification) if the current value drops x amount below the previously measured value.

I would now like to implement the use of the Numeric Input in the Blynk App. Which I have managed to get set up and connected to my NodeMCU to read and write the value as it changes. However, this value does not seem to be used as part of my code, when called up in the if (sensorValue < oldValue - threshold) line.

Am I missing something basic here? I have tried a few things to no avail. Thank you in advance!


// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "XXXXX"
#define BLYNK_DEVICE_NAME "XXXX"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD

#include "BlynkEdgent.h"

int oldValue; //assigning oldValue as an integer.
int threshold; //assigning threshold as an integer.

BlynkTimer testing; //creating a BlynkTimer called testing.

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  int sensorValue = analogRead(A0);
  Blynk.virtualWrite(V5, sensorValue);
  if (sensorValue < oldValue - threshold)
    Blynk.logEvent("wet_detected");
    oldValue = sensorValue;
}

void setup()
{
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();

  testing.setInterval(1000L, myTimerEvent);
}

// This function will be called every time the Wetness Threshold
// in Blynk app writes values to the Virtual Pin 1
BLYNK_WRITE(V1)
{
  int threshold = param.asInt(); // assigning incoming value from pin V1 to a variable (threshold).
  Serial.println(threshold);
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  // Serial.print("V1 Slider value is: "); (example)
  // Serial.println(pinValue); (example)
}

void loop() 
{
  BlynkEdgent.run();
  testing.run(); // Initiates BlynkTimer
}

The int at the beginning of this line is re-declaring the variable as a local variable, so it’s value can’t be seen outside of that function.
Remove the int (and the other cases where you’ve done this within functions) and it will fix your problem.

Also, I think I’d change this:

to this:

if (sensorValue < (oldValue - threshold))

Pete.

Thanks, Pete!

This worked perfectly. I didn’t realise that I was re-declaring within the function and that the value wouldn’t be seen outside. All part of me learning. Thank you so much.

1 Like