Need some help with a bit of coding

Hi all
I could use some guidance with my project.
It is a Hydroponic reseviour level control system which is generally running OK however I need to use the value in a variable from the blynk slider for maximum level control.
I get it displayed in the variable once but I cannot get the variable to keep the value it only changes when the slider changes otherwise the variable reverts to zero have not bee very successful if keeping the value of the blynk slider set-point.
I am sure that there is a very simple way to do this but my simple brain has a lock out regarding the solution can someone please help here is a clip from the serial monitor.

BLYNK_WRITE(V5)
{
  int TargetVolume = param.asInt();
  Blynk.virtualWrite(13, TargetVolume); //write data to virtual pin 13
  int maxSetPoint = TargetVolume; 
  Serial.print("V5 Slider value is: ");
  Serial.println(TargetVolume);
  if (totalMilliLitres >= MaxSetPoint)
  {
    overFillProtection();
}
Many thanks

Because you are not globally initializing the variable, it is ONLY usable within this function (AKA, probably 0 elsewhere) Try putting int TargetVolume; in your presetup.

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

1 Like

-edit ^^ what he says, lol

Outside of any functions, declare it globally:

#define Stuffhere

int maxSetPoint;

BLYNK_WRITE(V2)
{
   maxSetPoint = param.asInt();
}

// other stuff with maxSetpoint
1 Like

You’re declaring the variable within the BLYNK_WRITE(V5) block of code, so it’s only useable within that code. This is known as a local variable.

If you want to use the value that you’ve assigned to the variable in other parts of the code then you need to declare the variable at the top of your code (before void setup). This makes it a global variable.

I’m surprised that you’re not getting a compile message that says ‘MaxSetPoint is not declared in this scope’ when you try to use it elsewhere, so my guess is that you’re re-declaring it by putting ‘int’ in front of it when you try to use it again.
To use the global variable correctly, the ‘int MaxSetPoint’ will only be used once (at the top of your code) and in all future usesof the variable it will just be ‘MaxSetPoint’.

If you want to assign a default value to it at startup then you can do this with ‘int MaxSetPoint = xxxxx’, but you’ll then need to write this back to the slider widget so that the two are in sync.

Pete.

Thanks guys for the help got it going .
I did declare it initally as global however I made it equal to the slide target value which was the problem changed that to a default value which now works OK Really appreciate the help am not really too familiar with coding but am trying and am slowly getting to be able to crawl.

1 Like