[SOLVED] Set interval with millis

Hello everbody,
i am trying to set a pin HIGH for a given time by using the millis()-function.
My Problem is that the millis() keep running all the time. At the moment i set previousMillis = currentMillis; at the end whitch “resets” the millis() but if i press the Button1 to start the if- function again for example 1000ms after the pin set LOW the next repeat the pin will be HIGH 1000ms shorter than the first time. I assume the reason is that the milli()-function already counted 1000ms.

I would love to have a constant interval for the “HIGH-time”.
I hope you understand my problem.
Thanks a lot for your help
Thomas

#define BLYNK_PRINT Serial1
#include <BlynkSimpleStream.h>

char auth[] = "d1d.........................................f22e";


int Button1; //Auslöser
int Slider1; //Cola
int Slider2; //Korn
int Display = Slider1 + Slider2; // sum of slider 1 and slider 2



 unsigned long previousMillis = 0;


void setup()
{ 
  Serial1.begin(9600);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  Blynk.syncVirtual(V1); //Cola
  Blynk.syncVirtual(V2); //Korn
  Blynk.syncVirtual(V6); //Auslöser
  pinMode(7,OUTPUT);  // Cola pin
  pinMode(6,OUTPUT); // Korn pin
}

void loop()
{ 
  Blynk.run();
  int zeit1 = 5000; //(Slider1/2)/0.0893)
  int zeit2 = 2500;
  
  unsigned long currentMillis = millis();
  
  if (Display <= 2500000 && Button1 == 1)
  {
     

        if ((unsigned long)(currentMillis - previousMillis) <= zeit1){
        digitalWrite(7, HIGH);
        }
        
        if ((unsigned long)(currentMillis - previousMillis) <= zeit2){
        digitalWrite(6, HIGH);
        }
        
              if ((unsigned long)(currentMillis - previousMillis) > zeit1){
                digitalWrite(7,LOW);
                }
                
              if ((unsigned long)(currentMillis - previousMillis) > zeit2){
                digitalWrite(6,LOW);
                }
         
      
                  if(zeit1>zeit2 && (currentMillis - previousMillis) > zeit1){
                    Blynk.virtualWrite(V6,0);
                    Button1= 0;
                    previousMillis = currentMillis;
                    }
                    
                  if(zeit1<zeit2 && (currentMillis - previousMillis) > zeit2){
                    Blynk.virtualWrite(V6,0);
                    Button1= 0;
                    previousMillis = currentMillis;
                  } 
                  
}

}


BLYNK_WRITE(V1)
{
  Slider1 = param.asInt();
  Display = Slider1 + Slider2;
  if(Slider2 != 0){                   // just a hack for initial boot when 2nd slider doesn't yet have a value
    Blynk.virtualWrite(V3, Display);
    Blynk.virtualWrite(V5, Slider1);
  }  
}

BLYNK_WRITE(V2) {
  Slider2 = param.asInt();
  Display = Slider1 + Slider2;
  Blynk.virtualWrite(V4, Slider2);
  Blynk.virtualWrite(V3, Display);
}

BLYNK_WRITE(V6) {
  Button1 = param.asInt();
}

millis() how to’s are not really a Blynk specific question… we tend to use the BlynkTimer for timing requirements.

http://docs.blynk.cc/#blynk-firmware-blynktimer.

Here is one way to have a timed action:

BLYNK_WRITE(V1)  // Button Widget on V1 to activate LED
{
  int ACTION = param.asInt();
  if (ACTION == 1) {
    digitalWrite(13, HIGH);  // Turn on LED on pin 13 (Arduino)
    led.on(); // Turn "on" LED Widget
    timer.setTimeout(1000L, ACTIONOFF);  // Run ACTIONOFF routine in 1000ms (1 Second)
  }  
}

void ACTIONOFF()
{
  digitalWrite(13, LOW);    // Turn off LED on pin 13 (Arduino)
  led.off(); // Turn "off" LED Widget
}
1 Like