Blynk_write and millis() problem

Hello everyone, I have a problem that it wouldn’t go into if loop after millis() are greater than in code: if(millis() > currentMillis_1 + (perioda * koraki_1)). So what happens is gor_1 stays HIGH all the time.

So my question is, does the program process my code in BLYNK_WRITE(V0); only when there is change with int VirtualB or does it checks it all the time and if so how can I fix it?

I know there exists some kind of Blynk timer, but if there is any way of fixing this I would prefere using this. Also my IF statement may be wrong.

Thanks for any kind of help.
Žan


BLYNK_WRITE(V0)
{
  /*---------------------------------BRANJE VIRTUALNEGA STIKALA NA BLYNK---------------------------------*/
  int VirtualB = param.asInt();

  if(VirtualB != stanje_1)
  {
    if(VirtualB > stanje_1)
    {
      koraki_1 = VirtualB - stanje_1;
      gor_state_1 = digitalRead(gor_1); 
      if(gor_state_1 == 0)
      {
        currentMillis_1 = millis();
      }
      digitalWrite(gor_1,HIGH);
      if(millis() > currentMillis_1 + (perioda * koraki_1))
      {
        digitalWrite(gor_1,LOW);
        stanje_1 = VirtualB;
      }
    }
    if(VirtualB < stanje_1)
    {
      koraki_1 = stanje_1 - VirtualB;
      digitalWrite(dol_1,HIGH);
      delay(perioda * koraki_1);
      digitalWrite(dol_1,LOW);
      stanje_1 = VirtualB;
    }
  }
}

I honestly do not understand your question as it refers to millis()… but…

… a BLYNK_WRITE(Vpin) function is only processed when the widget (assigned to the Vpin) in the App experiences a state/data change.

The only way to run a Blynk function via code is to cause a change in the state/data via code - with a Blynk.virtualWrite(vPin, value) followed by a Blynk.syncVirtual(vPin) which then causes the function to run with new value.

Or, if just wanting the function to use the current vPin state, then the Blynk.syncVirtual(vPin) by itself will cause the function to run with last known state/data.

Yup :slight_smile:

1 Like

Ok, so will Blynk timer work in my current program? I just want to make a delay betwen gor_1 HIGH and going back to LOW.

Or what about if I run the timer in the void loop() and then send this value to the BLYNK_WRITE(V0); code?

You can’t use BLYNK_WRITE as a loop. It’s a special function, like a callback, that triggers automatically when the Blynk server detects a chance in the widget attached to the relevant virtual pin, or when a Blynk.syncVirtual call is made.

If you want a continuous loop then create a function that is called with a timer. Otherwise use a timeout timer.

Your void loop should always be as simple as possible, preferably with just Blynk.run and timer.run

Pete.

1 Like

Ok I understand now… so im gonna have to call a function that then watches the timer and sets digital output to low, correct?

Thanks for your time.

No, you don’t ‘watch’ the timer, you tell the timer which function to call and how often, then it does it automatically.

Maybe if you have a more complete description of what it is that you are trying to achieve (without turning that into a coding solution) then we’d be able to guide you better.

Pete.

I have multiple motors that I want to control with sliders on Blynk. So when I set the slider to value 5 then motor needs to run for 5 seconds.

For example: I set all the sliders to 3 and all the motors need to work at the same time for 3 seconds.

I dont want to use delay() because then I would have to wait for each motor to work for example 3 seconds and then next one for 3 seconds.

Im quite bad at explaining tell me if you need more information. :smile:

Okay, in that case you should use a timeout timer for each slider. When the BLYNK_WRITE callback for motor 1 is triggered then it starts the timer and starts the motor moving. When the timer ends it will stop the motor.
Same for the other two callbacks, each with separate timeout timers.

Pete.

Okay that is what I thought as well. Thanks for your help Pete. Is there any way of tagging you if I will run into other problems with this same project, so I wont have to open another question?

You tag forum users by putting an @ symbol in front of their username, but I tend to read all the posts anyway, so probably not necessary.

Pete.