Please a little help regarding the millis or timer code

i am trying to write a code to execute a section of the code after every 5 mins.But if i increase the interval3 more than the interval2 or the interval the timing function stops and the loop is executed without the 5 min interval.can anyone please help me to make 5 mins intervals before the code is run again .

the orignal code

GEYSER SOURCE CODE

#include <SoftwareSerial.h>
#include <Blynk.h>
#include <Servo.h>
SoftwareSerial mySerial(11, 10);
Servo myservo ;

const long interval = 60000;
const long interval2 = 10000;
const long interval3 = 300000 ;

unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;

const int flame = 7 ;
const int sparker = 3 ;
int flamestat = 0;
void setup() {
pinMode (flame , INPUT);
pinMode (sparker ,OUTPUT);
myservo.attach(9);
Serial.begin(9600);
}

void loop() {

unsigned long currentMillis3 = millis();
unsigned long currentMillis = millis();
unsigned long currentMillis2 = millis();

if (currentMillis3 - previousMillis3 >= interval3) {
// Time save ker oye!
previousMillis3 = currentMillis3;
//timing

digitalWrite(sparker, HIGH);
myservo.write(180);

//timing
if (currentMillis2 - previousMillis2 >= interval2) {
// Time save ker oye!
previousMillis2 = currentMillis2;
//timing
flamestat = digitalRead(flame);
if (flamestat == 0) {

myservo.write(90);
digitalWrite(sparker, LOW);

} else {
//time ka scene
if (currentMillis - previousMillis >= interval) {
// Time save ker oye!
previousMillis = currentMillis;
//TIMING ka scene

myservo.write(0);
digitalWrite(sparker, LOW);

}

}
}

}
}

If you use the IDE’s autoformat tool on your code, it may show you where your problem lies.

Please remember to use code tags when posting code.

The first thing to do is to format your code properly and post it in code tags

#include <SoftwareSerial.h>
#include <Blynk.h>
#include <Servo.h>
SoftwareSerial mySerial(11, 10);
Servo myservo ;

const long interval = 60000;
const long interval2 = 10000;
const long interval3 = 300000 ;

unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;

const int flame   = 7 ;
const int sparker = 3 ;
int flamestat = 0;
void setup()
{
  pinMode (flame , INPUT);
  pinMode (sparker , OUTPUT);
  myservo.attach(9);
  Serial.begin(9600);
}

void loop()
{
  unsigned long currentMillis3 = millis();
  unsigned long currentMillis = millis();
  unsigned long currentMillis2 = millis();
  if (currentMillis3 - previousMillis3 >= interval3)
  {
    // Time save ker oye!
    previousMillis3 = currentMillis3;
    //timing
    digitalWrite(sparker, HIGH);
    myservo.write(180);
    //timing
    if (currentMillis2 - previousMillis2 >= interval2)
    {
      // Time save ker oye!
      previousMillis2 = currentMillis2;
      //timing
      flamestat = digitalRead(flame);
      if (flamestat == 0)
      {
        myservo.write(90);
        digitalWrite(sparker, LOW);
      }
      else
      {
        //time ka scene
        if (currentMillis - previousMillis >= interval)
        {
          // Time save ker oye!
          previousMillis = currentMillis;
          //TIMING ka scene
          myservo.write(0);
          digitalWrite(sparker, LOW);
        }
      }
    }
  }
}

Formatted like this it helps you see that the check for interval2 having passed only happens if interval3 has elapsed so no wonder making interval3 larger than interval2 causes problems because interval2 is guaranteed to have passed if interval3 has.

Incidentally, you only need one currentMillis variable

UKHeliBob:
The first thing to do is to format your code properly and post it in code tags

#include <SoftwareSerial.h>

#include <Blynk.h>
#include <Servo.h>
SoftwareSerial mySerial(11, 10);
Servo myservo ;

const long interval = 60000;
const long interval2 = 10000;
const long interval3 = 300000 ;

unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;

const int flame   = 7 ;
const int sparker = 3 ;
int flamestat = 0;
void setup()
{
 pinMode (flame , INPUT);
 pinMode (sparker , OUTPUT);
 myservo.attach(9);
 Serial.begin(9600);
}

void loop()
{
 unsigned long currentMillis3 = millis();
 unsigned long currentMillis = millis();
 unsigned long currentMillis2 = millis();
 if (currentMillis3 - previousMillis3 >= interval3)
 {
   // Time save ker oye!
   previousMillis3 = currentMillis3;
   //timing
   digitalWrite(sparker, HIGH);
   myservo.write(180);
   //timing
   if (currentMillis2 - previousMillis2 >= interval2)
   {
     // Time save ker oye!
     previousMillis2 = currentMillis2;
     //timing
     flamestat = digitalRead(flame);
     if (flamestat == 0)
     {
       myservo.write(90);
       digitalWrite(sparker, LOW);
     }
     else
     {
       //time ka scene
       if (currentMillis - previousMillis >= interval)
       {
         // Time save ker oye!
         previousMillis = currentMillis;
         //TIMING ka scene
         myservo.write(0);
         digitalWrite(sparker, LOW);
       }
     }
   }
 }
}



Formatted like this it helps you see that the check for interval2 having passed only happens if interval3 has elapsed so no wonder making interval3 larger than interval2 causes problems because interval2 is guaranteed to have passed if interval3 has.

Incidentally, you only need one currentMillis variable

can you please tell me how to solve this problem as i am a newbee in this arduino space kindly

Please describe exactly what you want to happen and under what circumstances ?
What should happen after 5 minutes and how long should it go on for ?

hashambot:
can you please tell me how to solve this problem as i am a newbee in this arduino space kindly

You have the test for interval2 in the block of code that follows the test for interval3. They should almost certainly be separate.

By the way, you only need one currentMillis because it should be the same for everything.

…R

UKHeliBob:
Please describe exactly what you want to happen and under what circumstances ?
What should happen after 5 minutes and how long should it go on for ?

the code should run and the there should be a 5 min interval in the code being executed again in a loop

Robin2:
You have the test for interval2 in the block of code that follows the test for interval3. They should almost certainly be separate.

By the way, you only need one currentMillis because it should be the same for everything.

...R

Can you please elaborate your point

Bob and Robin provided you with a suggestion. Now it is your turn to implement this into your code, we will not write the code for you. It might also help, if you draw yourself a flow chart.

hashambot:
Can you please elaborate your point

You're checking interval2 inside the check for interval3.

LightuC:
Bob and Robin provided you with a suggestion. Now it is your turn to implement this into your code, we will not write the code for you. It might also help, if you draw yourself a flow chart.

You're checking interval2 inside the check for interval3.

i actually didnt understood what they said but i am very thankful to you for providing the guidance in easy words

i want to run the code every 10 mins but if i increase interval3 more than interval 2 or 1 the code runs without the expected delay . Please help me with the problem .

GEYSER SOURCE CODE

#include <SoftwareSerial.h>
#include <Blynk.h>
#include <Servo.h>
SoftwareSerial mySerial(11, 10);
Servo myservo ; 

const long interval = 60000;  
const long interval2 = 10000;
const long interval3 = 9000;

unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;

const int flame   = 7 ;
const int sparker = 3 ;
int flamestat = 0;
void setup() {
  pinMode (flame , INPUT);
  pinMode (sparker ,OUTPUT);
  myservo.attach(9);
  Serial.begin(9600);
}

void loop() {

  unsigned long currentMillis3 = millis();
   unsigned long currentMillis = millis();
  unsigned long currentMillis2 = millis();
  
  if (currentMillis3 - previousMillis3 >= interval3) {
    // Time save ker oye!
    previousMillis3 = currentMillis3;
    //timing 
   
 
 digitalWrite(sparker, HIGH);
 myservo.write(180);
 
   //timing
if (currentMillis2 - previousMillis2 >= interval2) {
    // Time save ker oye!
    previousMillis2 = currentMillis2;
    //timing
    flamestat = digitalRead(flame);
 if (flamestat == 0) {
  
  myservo.write(90);
  digitalWrite(sparker, LOW);
  
  } else {
    //time ka scene
  if (currentMillis - previousMillis >= interval) {
    // Time save ker oye!
    previousMillis = currentMillis;
    //TIMING ka scene
    
    myservo.write(0);
  digitalWrite(sparker, LOW);
  
 
  }
  
    }
   }
  
 }
}

Please don't start new topics for essentially the same question .

Threads merged.

And now that it is merged, go back and read reply #2. UKHeliBob nailed the problem for you there.

wildbill:
And now that it is merged, go back and read reply #2. UKHeliBob nailed the problem for you there.

so shall i store it in a variable and have the check for interval 2 outside the interval 3 condition ?

You have to wait five minutes between posts.
Why not use those five minutes productively, and TRY IT?

i want to run the code every 10 mins

unsigned long previousMillis = 0;

const unsigned long interval = 1000UL * 60 * 10;

void setup()
{
}

void loop()
{
  unsigned long currentTime = millis();
  if (currentTime - previousMillis >= interval)
  {
    previousMillis = currentTime;
    //code here will run every 10 minutes
  }
}

UKHeliBob:

unsigned long previousMillis = 0;

const unsigned long interval = 1000UL * 60 * 10;

void setup()
{
}

void loop()
{
  unsigned long currentTime = millis();
  if (currentTime - previousMillis >= interval)
  {
    previousMillis = currentTime;
    //code here will run every 10 minutes
  }
}

thanks alot

UKHeliBob:

unsigned long previousMillis = 0;

const unsigned long interval = 1000UL * 60 * 10;

void setup()
{
}

void loop()
{
  unsigned long currentTime = millis();
  if (currentTime - previousMillis >= interval)
  {
    previousMillis = currentTime;
    //code here will run every 10 minutes
  }
}

but it encounters the same problem that if i increase the interval 3 more than interval 2 or interval 1
the code dosent work as intended

It's about time that you explained exactly what you want the program to do. It will almost certainly need to be restructured, perhaps changed to a state machine to keep the timings for each state separate

UKHeliBob:
It's about time that you explained exactly what you want the program to do. It will almost certainly need to be restructured, perhaps changed to a state machine to keep the timings for each state separate

I want to turn a water geyser by using the following conditions.

1.The board will turn the servo to 180 degres and turn the sparker high

2.There should be a delay of 10 sec and after that the board will check if a flame has been established in these
10 sec, if the flame is established then the board will wait another 1 min before releasing the servo (to 90
degrees) and bringing the sparker to low and if the flame wasn't made in the 10 sec then the board will pull the
sparker to low and bring the servo to 90 degrees with out waiting for 1 min.

  1. This should happen every 10 mins