Please a little help regarding the millis or timer code

So you have several states and the system should only execute the code for one of them at a time

0 wait for 10 minutes then turn servo to 180, turn sparker high then move to state 1
1 wait for 10 seconds then check for flame
if flame is established move to state 2
if the flame has not been established move to state 3
2 wait 1 minute then move to state 3
3 move servo to 90, bring the sparker low and move to state 0

Does that look right ?

UKHeliBob:
So you have several states and the system should only execute the code for one of them at a time

0 wait for 10 minutes then turn servo to 180, turn sparker high then move to state 1
1 wait for 10 seconds then check for flame
if flame is established move to state 2
if the flame has not been established move to state 3
2 wait 1 minute then move to state 3
3 move servo to 90, bring the sparker low and move to state 0

Does that look right ?

Nope , cuz i want all of that to occur one by one .

1.when the board starts or the (loop is re executed) the servo (to 180 degrees) and the sparker should go high.

2.the board waits for 10 secs and if a flame is established in that time period then it holds the servo at 180
degrees and pulls the sparker high for 1 min ,if the flame wasn't established in that period then the servo and
the sparker are pull to low without the 1 min delay

3.After this happening the board waits for 10 min to start again from step 1 .

The steps will be executed one by one
If you want the system to start on power up or reset then start in state 1 in the revised list below

0 wait for 10 minutes then move to state 1
1 turn servo to 180, turn sparker high then move to state 2
2 wait for 10 seconds then check for flame
if flame is established move to state 3
if the flame has not been established move to state 4
3 wait 1 minute then move to state 4
4 move servo to 90, bring the sparker low and move to state 0

UKHeliBob:
The steps will be executed one by one
If you want the system to start on power up or reset then start in state 1 in the revised list below

0 wait for 10 minutes then move to state 1
1 turn servo to 180, turn sparker high then move to state 2
2 wait for 10 seconds then check for flame
if flame is established move to state 3
if the flame has not been established move to state 4
3 wait 1 minute then move to state 4
4 move servo to 90, bring the sparker low and move to state 0

Yes thats exactly what i want to happen

UKHeliBob:
The steps will be executed one by one
If you want the system to start on power up or reset then start in state 1 in the revised list below

0 wait for 10 minutes then move to state 1
1 turn servo to 180, turn sparker high then move to state 2
2 wait for 10 seconds then check for flame
if flame is established move to state 3
if the flame has not been established move to state 4
3 wait 1 minute then move to state 4
4 move servo to 90, bring the sparker low and move to state 0

Well the only problem i am facing is of timing
and rest i can figure out by myself

I am away from PC at the moment but I will write a framework into which you can insert your code and post it later

UKHeliBob:
I am away from PC at the moment but I will write a framework into which you can insert your code and post it later

thnks

Here is a framework into which you need to insert your code for each state. I have started it for you by putting in the code for the 10 minute wait.

Over to you

enum states
{
  WAIT_10_MINS,
  SPARKER_TO_HIGH,
  WAIT_10_SECONDS,
  WAIT_1_MINUTE,
  SPARKER_TO_LOW
};

unsigned long currentTime;
unsigned long stateStartTime;
unsigned long statePeriod;
byte currentState;

void setup()
{
  Serial.begin(115200);
  statePeriod = 10000;  //10 seconds for testing
  currentState = WAIT_10_MINS;  //set to SPARKER_TO_HIGH after testing to bypass initial 10 min wait
  stateStartTime = millis();
}

void loop()
{
  currentTime = millis();
  switch (currentState) //choose which code to execute based on the current state
  {
    case WAIT_10_MINS:
      if (currentTime - stateStartTime >= statePeriod)  //time's up
      {
        Serial.println("moving to SPARKER_TO_HIGH");
        currentState = SPARKER_TO_HIGH;
      }
      break;
      
    case SPARKER_TO_HIGH:
      //code here to turn on sparker, move servo to 180
      currentState = WAIT_10_SECONDS;
      statePeriod = 10000;
      stateStartTime = currentTime;   //ready for the next state
      break;
      
    case WAIT_10_SECONDS:
      //code here to wait 10 seconds
      //test sparker after 10 seconds
      //if flame is established
      //currentState = WAIT_1_MINUTE;
      //statePeriod = 60000;
      //stateStartTime = currentTime;   //ready for the next state
      //else currentState = SPARKER_TO_LOW;
      break;
      
    case WAIT_1_MINUTE:
      //code here to wait 1 minute then
      //stateStartTime = currentTime;   //ready for the next state
      //else currentState = SPARKER_TO_LOW;
      break;
      
    case SPARKER_TO_LOW:
      //code here to set sparker low and servo to 90 then
      //currentState = WAIT_10_MINUTES;
      //statePeriod = 10000;  //for testing only
      //stateStartTime = currentTime;   //ready for the next state
      break;
  }
}

UKHeliBob:
Here is a framework into which you need to insert your code for each state. I have started it for you by putting in the code for the 10 minute wait.

Over to you

enum states

{
  WAIT_10_MINS,
  SPARKER_TO_HIGH,
  WAIT_10_SECONDS,
  WAIT_1_MINUTE,
  SPARKER_TO_LOW
};

unsigned long currentTime;
unsigned long stateStartTime;
unsigned long statePeriod;
byte currentState;

void setup()
{
  Serial.begin(115200);
  statePeriod = 10000;  //10 seconds for testing
  currentState = WAIT_10_MINS;  //set to SPARKER_TO_HIGH after testing to bypass initial 10 min wait
  stateStartTime = millis();
}

void loop()
{
  currentTime = millis();
  switch (currentState) //choose which code to execute based on the current state
  {
    case WAIT_10_MINS:
      if (currentTime - stateStartTime >= statePeriod)  //time's up
      {
        Serial.println("moving to SPARKER_TO_HIGH");
        currentState = SPARKER_TO_HIGH;
      }
      break;
     
    case SPARKER_TO_HIGH:
      //code here to turn on sparker, move servo to 180
      currentState = WAIT_10_SECONDS;
      statePeriod = 10000;
      stateStartTime = currentTime;  //ready for the next state
      break;
     
    case WAIT_10_SECONDS:
      //code here to wait 10 seconds
      //test sparker after 10 seconds
      //if flame is established
      //currentState = WAIT_1_MINUTE;
      //statePeriod = 60000;
      //stateStartTime = currentTime;  //ready for the next state
      //else currentState = SPARKER_TO_LOW;
      break;
     
    case WAIT_1_MINUTE:
      //code here to wait 1 minute then
      //stateStartTime = currentTime;  //ready for the next state
      //else currentState = SPARKER_TO_LOW;
      break;
     
    case SPARKER_TO_LOW:
      //code here to set sparker low and servo to 90 then
      //currentState = WAIT_10_MINUTES;
      //statePeriod = 10000;  //for testing only
      //stateStartTime = currentTime;  //ready for the next state
      break;
  }
}

Thanks a lot for your help and the code ,may GOD bless you

I await with interest to see your code

UKHeliBob:
I await with interest to see your code

are these values to be changed
and what do they stand for
unsigned long currentTime;
unsigned long stateStartTime;
unsigned long statePeriod;
byte currentState;

Don't change any of the names, or if you do then change them throughout the code. The values assigned to teh variables will, however, change in the code

As to what they do I deliberately used descriptive names but here is that section of code with added comments

unsigned long currentTime;  //the current time at the start of each iteration of the loop() function
unsigned long stateStartTime;  //the value of millis() when the current state started
unsigned long statePeriod;  //the timing period for the current state if needed
byte currentState;  //the current state.  Used by switch/case to determine which portion of code to execute

UKHeliBob:
Don't change any of the names, or if you do then change them throughout the code. The values assigned to teh variables will, however, change in the code

As to what they do I deliberately used descriptive names but here is that section of code with added comments

unsigned long currentTime;  //the current time at the start of each iteration of the loop() function

unsigned long stateStartTime;  //the value of millis() when the current state started
unsigned long statePeriod;  //the timing period for the current state if needed
byte currentState;  //the current state.  Used by switch/case to determine which portion of code to execute

ok

finally the code is done.

//Written by Hasham Ghuffary.Not to be publish else where with out
//the authers permission.
//Special Thanks to UkHeli Bob

#define BLYNK_PRINT Serial
#include <elapsedMillis.h>
#include <SoftwareSerial.h>
#include <Blynk.h>
#include <Servo.h>
#include <BlynkSimpleSerialBLE.h>

char auth[] = "48bb8da229da4d3bb4d473cfc16fbc13";

SoftwareSerial SwSerial(10, 11); // RX, TX
Servo myservo ;

SoftwareSerial SerialBLE(10, 11); // RX, TX

int bluetoothpin;
BLYNK_WRITE(V1)
{   
  int value = param.asInt(); // Get value as integer

  // The param can contain multiple values, in such case:
 if (value == 1) { bluetoothpin = 1;
 } else { bluetoothpin = 0;}
}

const int flame   = 7 ;
const int sparker = 3 ;
int flamestat = 0;

//virtual send 
WidgetLED led3(V3);

BlynkTimer timer;

// V3 LED Widget represents the physical button state
boolean btnState = false;
void buttonLedWidget()
{
  // Read button
  boolean isPressed = (digitalRead(flame) == HIGH);

  // If state has changed...
  if (isPressed != btnState) {
    if (isPressed) {
      led3.on();
    } else {
      led3.off();
    }
    btnState = isPressed;
  }
}
//virtual send
enum states
{
  FLAME_IS_ON,
  WAIT_10_MINS,
  SPARKER_TO_HIGH,
  WAIT_10_SECONDS,
  WAIT_1_MINUTE,
  SPARKER_TO_LOW
};

const long Timeperiod = 1000;
unsigned long currentTime;  //the current time at the start of each iteration of the loop() function
unsigned long stateStartTime;  //the value of millis() when the current state started
unsigned long statePeriod;  //the timing period for the current state if needed
byte currentState;  //the current state.  Used by switch/case to determine which portion of code to execute

void setup()
{
  pinMode (flame , INPUT);
  pinMode (sparker , OUTPUT);
  myservo.attach(9);
  Serial.begin(57600);
  bluetoothpin = 1;

  pinMode(flame, INPUT_PULLUP);
  timer.setInterval(500L, buttonLedWidget);

  SerialBLE.begin(57600);
  Blynk.begin(SerialBLE, auth);
  
  statePeriod = Timeperiod ;  //10 seconds for testing
  currentState = WAIT_10_MINS;  //set to SPARKER_TO_HIGH after testing to bypass initial 10 min wait
  stateStartTime = millis();
}

void loop() {
   Blynk.run();
   timer.run();
   
   flamestat = digitalRead(flame);
   
  if (bluetoothpin == 1) {
    
       flamestat = digitalRead(flame);

  currentTime = millis();
  switch (currentState) //choose which code to execute based on the current state
   
  {
    case WAIT_10_MINS:
      if (currentTime - stateStartTime >= statePeriod)  //time's up
      {
        Serial.println("moving to SPARKER_TO_HIGH and servo to 180");
        currentState = FLAME_IS_ON;
      }
      break;

    case FLAME_IS_ON:
      flamestat = digitalRead(flame);
      if (flamestat == 0) 
      {
       currentState = SPARKER_TO_HIGH;
      }
       break;
      
      
    case SPARKER_TO_HIGH:
      //code here to turn on sparker, move servo to 180
      
    digitalWrite(sparker, HIGH);
    myservo.write(180);
      
      currentState = WAIT_10_SECONDS;
      statePeriod = 10000;
      stateStartTime = currentTime;   //ready for the next state
      break;
      
    case WAIT_10_SECONDS:
    
      if (currentTime - stateStartTime >= statePeriod) 
      {
      
      flamestat = digitalRead(flame);
      
      if (flamestat == 1 )
      {
        currentState = WAIT_1_MINUTE;
        statePeriod = 60000;
        stateStartTime = currentTime;  
      } else {
        currentState = SPARKER_TO_LOW;
       }
      }
      
      break;
      
    case WAIT_1_MINUTE:
      //code here to wait 1 minute then
      flamestat = digitalRead(flame);
      if (currentTime - stateStartTime >= statePeriod)  //time's up
      {
        currentState = SPARKER_TO_LOW;
        stateStartTime = currentTime;   //ready for the next state
      }
      break;
      
    case SPARKER_TO_LOW:
      //code here to set sparker low and servo to 90 then
      myservo.write(90);
      digitalWrite(sparker, LOW);
      
        currentState = WAIT_10_MINS;
        statePeriod = Timeperiod ;  //for testing only
        stateStartTime = currentTime;   //ready for the next state
      break;
  }
 } else {
  
       myservo.write(90);
      digitalWrite(sparker, LOW);
 }
}
      case FLAME_IS_ON:
        flamestat = digitalRead(flame);
        if (flamestat == 0)
        {
          currentState = SPARKER_TO_HIGH;
        }
        break;

What happens if the flame does not fire and the digitalRead() returns HIGH ?

UKHeliBob:

      case FLAME_IS_ON:

flamestat = digitalRead(flame);
        if (flamestat == 0)
        {
          currentState = SPARKER_TO_HIGH;
        }
        break;



What happens if the flame does not fire and the digitalRead() returns HIGH ?

It wont happen cuz the pin whose value is being red over here is hooked up to a flame sensor

It wont happen cuz the pin whose value is being red over here is hooked up to a flame sensor

If you are so certain that the pin being tested will be LOW then why are you testing it ?

UKHeliBob:
If you are so certain that the pin being tested will be LOW then why are you testing it ?

i am testing to see whether the flame was already on because i dont want the servo and the sparker to actuate
when the flame is already up

i am testing to see whether the flame was already on

OK, but what if it is already on, will digitalRead(flame) return zero or something else ? If it returns anything but zero then how will the system ever leave the FLAME_IS_ON state ?

UKHeliBob:
OK, but what if it is already on, will digitalRead(flame) return zero or something else ? If it returns anything but zero then how will the system ever leave the FLAME_IS_ON state ?

If the flame is on the flamestat will Be 1 and take a close look at the case statement

case FLAME_IS_ON:
      flamestat = digitalRead(flame);
      if (flamestat == 0) 
      {
       currentState = SPARKER_TO_HIGH;
      }
       break;

see that i applied the condition that if flame is off then turn the currentstate = SPARKER_TO_HIGH