How can I set state machine run 4 times than break?

I wrote this code,it's working.

But how to run 4 times than stop every thing?

I tried "for loop",but not working....

#include <EEPROM.h>


byte led1 = 10; 
byte led2 = 11; 
byte led3 = 12;
byte button1 = 2;
byte button2 = 3;

//define the available states that we can have for this sketch
enum States{
  stateOne, stateTwo};
//add more states as needed
States mState = stateOne;             //we start out in this machine state

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT); 
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);  
  digitalWrite(led3,LOW);
} //END of setup()


void loop()
{
  switch (mState)
  {  
   case stateOne:
    {   
      if(digitalRead(button1))       
      {
        digitalWrite(led1,HIGH);
        delay(1000);
        digitalWrite(led1,LOW);
        delay(2000); 
        digitalWrite(led2,HIGH);
        delay(1000);
        digitalWrite(led2,LOW);
        delay(1000);  
        mState = stateTwo;    
      } 
    }
    break;

   
  case stateTwo:
    {
      if(digitalRead(button2))
      {
        digitalWrite(led3,HIGH);
        delay(1000);
        digitalWrite(led3,LOW); 
        delay(1000);      
        mState = stateOne;
      }
    }      
    break;

  default: 
    // default code goes here
    break;

  } //END of switch(mState)

}
enum States{
  stateOne, stateTwo};

How about making the state names mean something. stateOne doesn't mean anything. stateTwo means just as little. stateEnd could be added, in which nothing happens. At the appropriate point, transition to stateEnd, whose case statement causes nothing to happen visibly.

Please describe your project?

If you are serious about programming, delay() is not the way to do delays.
See BlinkWithoutDelay.

the project is

I want control two solenoid valve with 2 magnetic sensor

beginning,magnet at position1(sensor1)

sensor1 on

than led1(coil1) on

delay 1 sec.

than led1 off

delay 2 sec.

than led2(coil2) on

delay 1 sec.

led2 off

the magnet will touch sensor2

until sensor2 on

led3(coil3) on

dealy 1 sec

led3 off

than magnet come back to touch sensor1

I want the loop run 4 times than stop every thing

thank you~

I want the loop run 4 times than stop every thing

Then create a for loop in setup(), and leave loop() empty.

PaulS:
Then create a for loop in setup(), and leave loop() empty.

like this?

#include <EEPROM.h>


byte led1 = 10; 
byte led2 = 11; 
byte led3 = 12;
byte button1 = 2;
byte button2 = 3;


//define the available states that we can have for this sketch
enum States{
  stateOne, stateTwo};
//add more states as needed
States mState = stateOne;             //we start out in this machine state

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT); 
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);  
  digitalWrite(led3,LOW);
  int i=0
  for(i=0;i<4;i++){}
} //END of setup()


void loop()
{
  switch (mState)
  {  
   case stateOne:
    {   
      if(digitalRead(button1))       
      {
        digitalWrite(led1,HIGH);
        delay(1000);
        digitalWrite(led1,LOW);
        delay(2000); 
        digitalWrite(led2,HIGH);
        delay(1000);
        digitalWrite(led2,LOW);
        delay(1000);  
        mState = stateTwo;    
      } 
    }
    break;

   
  case stateTwo:
    {
      if(digitalRead(button2))
      {
        digitalWrite(led3,HIGH);
        delay(1000);
        digitalWrite(led3,LOW); 
        delay(1000);      
        mState = stateOne;
      }
    }      
    break;

  default: 
    // default code goes here
    break;
 
  } //END of switch(mState)
  
}

Empty for() loop is empty.

If you want to do something 4 times, then put it into the body of that for() loop. Don't put it into the loop() function.

The loop() function can be empty (but it must exist.)

bm6dzr:
like this?

Did you leave loop empty, as instructed?