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();
}