How to control to start and stop the Loop

What statement or function should i use to control the program in the loop?
My goal is to start the program from the time the microcontroller is on after 7 mins the program inside the void loop (MULTIPLE LED ON and OFF) will run for 1 min then after 1 min is finished the program in the void loop will stop. and then again after 7 mins the void loop program will run again for 1 min. and again and again.

Thanks in advance.

int ledPin1 = 27;             // LED 1 connected to digital pin 27
int ledPin2 = 26;             // LED 2 connected to digital pin 26
int ledPin3 = 25;             // LED 3 connected to digital pin 25
int ledPin4 = 14;             // LED 4 connected to digital pin 14
int ledPin5 = 13;             // LED 5 connected to digital pin 13
int ledPin6 = 12;             // LED 6 connected to digital pin 12


int ledState1 = LOW;       // LED 1 is initially set to off
int ledState2 = HIGH;      // LED 2 is initially set to off
int ledState3 = LOW;       // LED 3 is initially set to off
int ledState4 = LOW;       // LED 4 is initially set to off
int ledState5 = LOW;       // LED 5 is initially set to off
int ledState6 = LOW;       // LED 6 is initially set to off

unsigned long millis1;        // initialises millis marker for LED 1
unsigned long millis2;        // initialises millis marker for LED 2
unsigned long millis3;        // initialises millis marker for LED 3
unsigned long millis4;        // initialises millis marker for LED 4
unsigned long millis5;        // initialises millis marker for LED 5
unsigned long millis6;        // initialises millis marker for LED 6

void setup() {
  pinMode(ledPin1, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin2, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin3, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin4, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin5, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin6, OUTPUT);      // sets the digital pin as output
}

void loop() {

  // Update LED States
  digitalWrite(ledPin1, ledState1);
  digitalWrite(ledPin2, ledState2);
  digitalWrite(ledPin3, ledState3);
  digitalWrite(ledPin4, ledState4);
  digitalWrite(ledPin5, ledState5);
  digitalWrite(ledPin6, ledState6);

  // Toggle the LED states if the duration has passed
// LED -1
  if ( (millis() - millis1) > 2000) {
    millis1 = millis();
    if (ledState1 == LOW) {
      ledState1 = HIGH;
    } else if (ledState1 == HIGH) {
      ledState1 = LOW;
    }
  }

  // LED -2
  if ( (millis() - millis2) > 2000) {
    millis2 = millis();
    if (ledState2 == LOW) {
      ledState2 = HIGH;
    } else if (ledState2 == HIGH) {
      ledState2 = LOW;
    }
  }
  
  // LED -3
if ( (millis() - millis3) > 1500) {
    millis3 = millis();
    if (ledState3 == LOW) {
      ledState3 = HIGH;
    } else if (ledState3 == HIGH) {
      ledState3 = LOW;
    }
  } 
  
  // LED -4
  if ( (millis() - millis4) > 3000) {
    millis4 = millis();
    if (ledState4 == LOW) {
      ledState4 = HIGH;
    } else if (ledState4 == HIGH) {
      ledState4 = LOW;
    }
  } 
  
  // LED -5
  if ( (millis() - millis5) > 6000) {
    millis5 = millis();
    if (ledState5 == LOW) {
      ledState5 = HIGH;
    } else if (ledState5 == HIGH) {
      ledState5 = LOW;
    }
  }

// LED -6  
  if ( (millis() - millis6) > 2500) {
    millis6 = millis();
    if (ledState6 == LOW) {
      ledState6 = HIGH;
    } else if (ledState6 == HIGH) {
      ledState6 = LOW;
    }
  }
}

What's the issue you are having?

You should probably read how to use the forum first.
At the top of each section, there’s a header that explains what each section is for.

Your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that e.g. you write, not for questions. Feel free to write a tutorial once you have solved your problem :wink:

I think that you will have to read up on finite state machines. In the first state, you wait till 7 minutes have passed and then go to the second state. In the second state you run your pattern for one minute; once the minute is passed you go back to the first state (as you're using millis() you will have to record the end time of the second state.

Move the contents of 'loop()' into a function called 'DoPattern()'. Then change 'loop()' to:

const unsigned long Minutes = 60ul * 1000ul;
const unsigned long SevenMinutes = Minutes * 7;
const unsigned long EightMinutes = Minutes * 8;

void loop()
{
  static unsigned long startTime = 0;
  unsigned long elapsedInterval = millis() - startTime;

  // If more than seven minutes in, run the pattern
  if (elapsedInterval >= SevenMinutes)
    DoPattern();

  // If more than eight minutes in, re-start the sequence
  if (elapsedInterval >= EightMinutes)
    startTime = millis();
}

Dear John,

Thank you very much for the idea it helps a lot.

Regards,
Keven,

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.