There is no “Arduino Standard” and “Blynk Standard” when it comes to Local versus Global variables (I assume that’s what you mean when you refer to “fix variables instead of dynamic”).
Many people who write “Arduino code” choose to put everything in the void loop, and if they don’t want it to execute hundreds of times per second they throw a delay in at the end to make the processor wait before it executes the void loop again.
This isn’t exactly what you’d call structured programming, but with simple pieces of code you can get away with it, and many people don’t know any better.
A much nicer approach is to write blocks of code that perform a certain task - such as taking a temperature reading, calculating a complex formula, outputting a set of results to the serial monitor etc. These blocks of code are known as functions, and your void lecturaBlynk()
function is an example.
Any variables declared within a function is local to that function - it’s not visible outside of the function o in any other function.
There are two ways around this, passing parameters into a function and back out again, or using global variables.Passing parameters is a neater way to do do things, but in most situations people (myself included) don’t bother. Instead we use global variables that are accessible from anywhere.
A few comments on your code…
This function isn’t very efficient in my opinion…
void lecturaBlynk()
{
int lecpor1 = map(analogRead(humedad1), 0, 1021, 100, 0);
int lecpor2 = map(analogRead(humedad2), 0, 1021, 100, 0);
float lux = lightMeter.readLightLevel();
Blynk.virtualWrite(V1, dht.readHumidity());
Blynk.virtualWrite(V2, dht.readTemperature());
Blynk.virtualWrite(V3, lecpor1);
Blynk.virtualWrite(V4, lecpor2);
Blynk.virtualWrite(V5, (analogRead(humedad1)));
Blynk.virtualWrite(V6, (analogRead(humedad2)));
Blynk.virtualWrite(V7, lux);
}
The slowest part of this process, and therefore the thing you want to do the least, is taking a reading from your analog sensors. You do two analogReads in each sensor within this function, so one option would be to simply take those analog sensor readings once, at the beginning of the function and store them in variables that are then used later…
void lecturaBlynk()
{
float humidity1 = analogRead(humedad1; //Take your reading once and store it
float humidity2 = analogRead(humedad2;
int lecpor1 = map(humidity1), 0, 1021, 100, 0); // Use your stored reading here
int lecpor2 = map(humidity2), 0, 1021, 100, 0);
float lux = lightMeter.readLightLevel();
Blynk.virtualWrite(V1, dht.readHumidity());
Blynk.virtualWrite(V2, dht.readTemperature());
Blynk.virtualWrite(V3, lecpor1);
Blynk.virtualWrite(V4, lecpor2);
Blynk.virtualWrite(V5, humidity1); // and use your stored reading here again
Blynk.virtualWrite(V6, humidity2);
Blynk.virtualWrite(V7, lux);
}
However, you also take readings from these sensors in other places within your code…
BLYNK_WRITE(V8)
{
int pinValue = param.asInt();
Germina = pinValue;
Serial.println(Germina);
Serial.println(analogRead(humedad1)); // you're reading that sensor again here
}
void RiegoON()
{
unsigned long currentTime = millis();
if(Germina <= analogRead(humedad1) && lightMeter.readLightLevel() <= Luzriego) //Variante para riego solo de Noche) // and reading the sensor again here
{
if( currentTime - previousTime2 >= TpropRiego && digitalRead(bomba1) == HIGH)
{
digitalWrite(bomba1, LOW);
previousTime2 = currentTime;
}
if( currentTime - previousTime2 >= Triego && digitalRead(bomba1) == LOW)
{
digitalWrite(bomba1, HIGH);
previousTime2 = currentTime;
}
}
}
So maybe a better solution would be to have a timed function which reads your sensors (including your DHT and Lux sensor) and stores those readings as global variables that you can use anywhere within your code. You already seem to have done some of that with your void sensorAmbiente()
and void sensorLuz()
functions, except they use local variables, and those functions are never called from within your code.
Pete.