Passing data from serial won't save into variable

I am sending integers from max msp via serial into arduino and store them into variable name uint8_t buttonPressCount = 0;

function of receiving the variable

void controlSequenceScene() {
  if (Serial.available() > 0) {
    char id = Serial.read();
    int buttonPressCount = Serial.parseInt();
    if (id == scene_id) {
      int buttonPressCount = Serial.parseInt();
      Serial.print("sequence: ");
      Serial.println(buttonPressCount);
    }
  }
}

then I have few function that should run if buttonPressCount is certain number:

for example:

if (buttonPressCount == 1)
  {
    Serial.println("Sequence 1 on");
    currentMillis = myMillis() - millisTimePress;
    for (uint8_t x = 0; x < NUM_SOLENOID; x++)
    {
      // Is this led active
      if (mySolenoid[x].startTime < currentMillis)
      {
        // Do we need to calculate a next pulse time?


        if (mySolenoid[x].nextPulseStop < currentMillis)
        {
          // Turn off led
          digitalWrite(mySolenoid[x].pin, LOW);
          Serial.print("solenoid: ");
          Serial.print(x);
          Serial.println("off");

          // Calculate nextPulseStart & nextPulseStop
          long  startOffset = random(mySolenoid[x].minPulse, mySolenoid[x].maxPulse);
          mySolenoid[x].nextPulseStart = startOffset + currentMillis;
          mySolenoid[x].nextPulseStop = mySolenoid[x].nextPulseStart + mySolenoid[x].pulseLength;
        }

        // Are we currently in the middle of a pulse?
        if (mySolenoid[x].nextPulseStart < currentMillis &&
            mySolenoid[x].nextPulseStop  > currentMillis)
        {
          //turn on solenoid
          digitalWrite(mySolenoid[x].pin, HIGH);
          Serial.print("solenoid: ");
          Serial.print(x);
          Serial.println("on");
        }
      }
    }
  }
}

and

//////****END SEQUENCE*****///////

void endSequence()
{
  if (buttonPressCount >= 3)
  {
    //   currentMillis = myMillis() - millisTimePress;
    for (uint8_t x = 0; x < NUM_SOLENOID; x++)
    {
      //    digitalWrite(EndPin[x], LOW);
      digitalWrite(mySolenoid[x].pin, LOW);
    }
  }

}


the problem is that it seems the data I'm receiving via serial is not save into my "buttonPresscount" variable and therefore sequence are not happening

full code:

#define NUM_SOLENOID 9

//start program at x minute//

#define OFFSET 0 //in minutes

const char scene_id = 'H';

uint8_t buttonPressCount = 0; // Note an uint8_t stores a number from 0-255, so after 255 the buttonPressCount will go back to 0 again.  If you want to store a larger integer, use uint16_t (0-65535), uint32_t (0-4294967295), or even uint64_t (0-18446744073709551615).

uint32_t millisTimePress = 0; // unsigned longs are uint32_t, an unsigned 32 bit integer.
uint32_t currentMillis;


//PULSE//
const uint16_t PULSE =  20;

//Solenoid structure//
struct Solenoid
{
  unsigned long startTime;       // The time this led is first active after a buttun is first pressed
  unsigned long minPulse;        // The minimum time the next pulse is generated
  unsigned long maxPulse;        // The maximum time the next pulse is generated
  unsigned long nextPulseStart;  // The time of the next pulse starts
  unsigned long nextPulseStop;   // The time of the next pulse  stops
  unsigned long pulseLength;     // The on time of the pulse
  uint8_t pin;                   // led GPIO pin number
};
// Start pulse min max
Solenoid mySolenoid[NUM_SOLENOID] = {
  0 min, 11 sec, 12 sec, 0, 0, PULSE, 10, //solenoid num 1
  3 min,  7 sec, 11 sec, 0, 0, PULSE, 2,//solenoid num 2
  5 min, 6 sec, 10 sec, 0, 0, PULSE, 9, //solenoid num 3
  9 min, 5 sec, 10 sec, 0, 0, PULSE, 3, //solenoid num 4
  11.5 min, 5 sec, 8 sec, 0, 0, PULSE, 8,//solenoid num 5
  13 min, 5 sec, 8 sec, 0, 0, PULSE, 4, // num 6
  15.5 min, 4 sec, 7 sec, 0, 0, PULSE, 7, // num 7
  17.5 min, 3 sec, 9 sec, 0, 0, PULSE, 5, // num 8
  18.5 min, 2 sec, 6 sec, 0, 0, PULSE, 6 // num 9

};




///middle solenoid struct//

struct SolenoidMiddle
{
  unsigned long startTime;       // The time this led is first active after a buttun is first pressed
  unsigned long stopTime;        // The time this led becomes inactive after a buttun is second time pressed
  unsigned long minPulse;        // The minimum time the next pulse is generated
  unsigned long maxPulse;        // The maximum time the next pulse is generated
  unsigned long nextPulseStart;  // The time of the next pulse starts
  unsigned long nextPulseStop;   // The time of the next pulse  stops
  unsigned long pulseLength;     // The on time of the pulse
  uint8_t pin;                   // led GPIO pin number
};
//                                       start   end    minpulse maxpulse
SolenoidMiddle mySolenoidMiddle[NUM_SOLENOID] = {
  0 sec, 100 min , 11 sec, 12 sec, 0, 0, PULSE, 10, //solenoid num 1
  0 sec, 1.5 min, 7 sec, 11 sec, 0, 0, PULSE, 2,//solenoid num 2
  0 sec, 3 min, 6 sec, 10 sec, 0, 0, PULSE, 9, //solenoid num 3
  0 sec, 100 min, 8 sec, 12 sec, 0, 0, PULSE, 3, //4
  0 sec, 100 min, 8 sec, 12 sec, 0, 0, PULSE, 8, //5
  0 sec, 5.5 min, 5 sec, 8 sec, 0, 0, PULSE, 4, //6
  0 sec, 7.5 min, 4 sec, 7 sec, 0, 0, PULSE, 7,  //7
  0 sec, 4.5 min, 6 sec, 11 sec, 0, 0, PULSE, 5, //8
  0 sec, 9 min, 2 sec, 6 sec, 0, 0, PULSE, 6  //9

};


// millis function for offseting (debug from a specific time)

uint32_t myMillis()
{
  return millis() + (OFFSET  * 60 * 1000UL);
}


void setup()
{
  Serial.setTimeout(10);

  Serial.begin(115200);

  // Setup the button.
  pinMode(switchPin, INPUT_PULLUP);


  // Setup the LED.
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  // Solenoid
  for (uint8_t i = 0; i < NUM_SOLENOID; i++)
  {
    pinMode(mySolenoid[i].pin, OUTPUT);
  }
}

void loop()
{

  controlSequenceScene();
  startSequence();
  middleSequence();
  endSequence();


}


void controlSequenceScene() {
  if (Serial.available() > 0) {
    char id = Serial.read();
    int buttonPressCount = Serial.parseInt();
    if (id == scene_id) {
      int buttonPressCount = Serial.parseInt();
      Serial.print("sequence: ");
      Serial.println(buttonPressCount);
    }
  }
}



//////****START SEQUENCE*****///////


void startSequence()
{

  if (buttonPressCount == 1)
  {
    Serial.println("Sequence 1 on");
    currentMillis = myMillis() - millisTimePress;
    for (uint8_t x = 0; x < NUM_SOLENOID; x++)
    {
      // Is this led active
      if (mySolenoid[x].startTime < currentMillis)
      {
        // Do we need to calculate a next pulse time?


        if (mySolenoid[x].nextPulseStop < currentMillis)
        {
          // Turn off led
          digitalWrite(mySolenoid[x].pin, LOW);
          Serial.print("solenoid: ");
          Serial.print(x);
          Serial.println("off");

          // Calculate nextPulseStart & nextPulseStop
          long  startOffset = random(mySolenoid[x].minPulse, mySolenoid[x].maxPulse);
          mySolenoid[x].nextPulseStart = startOffset + currentMillis;
          mySolenoid[x].nextPulseStop = mySolenoid[x].nextPulseStart + mySolenoid[x].pulseLength;
        }

        // Are we currently in the middle of a pulse?
        if (mySolenoid[x].nextPulseStart < currentMillis &&
            mySolenoid[x].nextPulseStop  > currentMillis)
        {
          //turn on solenoid
          digitalWrite(mySolenoid[x].pin, HIGH);
          Serial.print("solenoid: ");
          Serial.print(x);
          Serial.println("on");
        }
      }
    }
  }
}


//////****MIDDLE SEQUENCE*****///////

void middleSequence()
{
  if (buttonPressCount == 2)
  {
    currentMillis = myMillis() - millisTimePress;
    for (uint8_t x = 0; x < NUM_SOLENOID; x++)
    {

      // Is this led active
      if (mySolenoidMiddle[x].startTime < currentMillis && mySolenoidMiddle[x].stopTime > currentMillis)
      {
        // Do we need to calculate a next pulse time?


        if (mySolenoidMiddle[x].nextPulseStop < currentMillis)
        {
          // Turn off led
          digitalWrite(mySolenoidMiddle[x].pin, LOW);

          // Calculate nextPulseStart & nextPulseStop
          long  startOffset = random(mySolenoidMiddle[x].minPulse, mySolenoidMiddle[x].maxPulse);
          mySolenoidMiddle[x].nextPulseStart = startOffset + currentMillis;
          mySolenoidMiddle[x].nextPulseStop = mySolenoidMiddle[x].nextPulseStart + mySolenoidMiddle[x].pulseLength;
        }

        // Are we currently in the middle of a pulse?
        if (mySolenoidMiddle[x].nextPulseStart < currentMillis &&
            mySolenoidMiddle[x].nextPulseStop  > currentMillis)
        {
          //turn on led
          digitalWrite(mySolenoidMiddle[x].pin, HIGH);
        }
      }
    }
  }

}

//////****END SEQUENCE*****///////

void endSequence()
{
  if (buttonPressCount >= 3)
  {
    //   currentMillis = myMillis() - millisTimePress;
    for (uint8_t x = 0; x < NUM_SOLENOID; x++)
    {
      //    digitalWrite(EndPin[x], LOW);
      digitalWrite(mySolenoid[x].pin, LOW);
    }
  }

}






These are two separate variables. Look at scope rules to understand why.
Have just one global definition of buttonPressCount.

I changed that but still not working:

#define NUM_SOLENOID 9

//time translating//
#define sec * 1000
#define min * 60000
#define ms * 1


//start program at x minute//

#define OFFSET 0 //in minutes



uint8_t ledPin = A0;
uint8_t buttonPressCount = 0; // Note an uint8_t stores a number from 0-255, so after 255 the buttonPressCount will go back to 0 again.  If you want to store a larger integer, use uint16_t (0-65535), uint32_t (0-4294967295), or even uint64_t (0-18446744073709551615).
uint8_t PUSHED = 0;
unsigned long timePushed = 0;



const char scene_id = 'H';

uint32_t millisTimePress = 0; // unsigned longs are uint32_t, an unsigned 32 bit integer.
uint32_t currentMillis;


//timeIs//
unsigned long hours = 0;
uint8_t minutes = 0;
uint8_t seconds = 0;
unsigned long milliseconds = 0;
unsigned long interval = 10000;


//PULSE//
const uint16_t PULSE =  20;

//Solenoid structure//
struct Solenoid
{
  unsigned long startTime;       // The time this led is first active after a buttun is first pressed
  unsigned long minPulse;        // The minimum time the next pulse is generated
  unsigned long maxPulse;        // The maximum time the next pulse is generated
  unsigned long nextPulseStart;  // The time of the next pulse starts
  unsigned long nextPulseStop;   // The time of the next pulse  stops
  unsigned long pulseLength;     // The on time of the pulse
  uint8_t pin;                   // led GPIO pin number
};
// Start pulse min max
Solenoid mySolenoid[NUM_SOLENOID] = {
  0 min, 11 sec, 12 sec, 0, 0, PULSE, 10, //solenoid num 1
  3 min,  7 sec, 11 sec, 0, 0, PULSE, 2,//solenoid num 2
  5 min, 6 sec, 10 sec, 0, 0, PULSE, 9, //solenoid num 3
  9 min, 5 sec, 10 sec, 0, 0, PULSE, 3, //solenoid num 4
  11.5 min, 5 sec, 8 sec, 0, 0, PULSE, 8,//solenoid num 5
  13 min, 5 sec, 8 sec, 0, 0, PULSE, 4, // num 6
  15.5 min, 4 sec, 7 sec, 0, 0, PULSE, 7, // num 7
  17.5 min, 3 sec, 9 sec, 0, 0, PULSE, 5, // num 8
  18.5 min, 2 sec, 6 sec, 0, 0, PULSE, 6 // num 9

};




///middle solenoid struct//

struct SolenoidMiddle
{
  unsigned long startTime;       // The time this led is first active after a buttun is first pressed
  unsigned long stopTime;        // The time this led becomes inactive after a buttun is second time pressed
  unsigned long minPulse;        // The minimum time the next pulse is generated
  unsigned long maxPulse;        // The maximum time the next pulse is generated
  unsigned long nextPulseStart;  // The time of the next pulse starts
  unsigned long nextPulseStop;   // The time of the next pulse  stops
  unsigned long pulseLength;     // The on time of the pulse
  uint8_t pin;                   // led GPIO pin number
};
//                                       start   end    minpulse maxpulse
SolenoidMiddle mySolenoidMiddle[NUM_SOLENOID] = {
  0 sec, 100 min , 11 sec, 12 sec, 0, 0, PULSE, 10, //solenoid num 1
  0 sec, 1.5 min, 7 sec, 11 sec, 0, 0, PULSE, 2,//solenoid num 2
  0 sec, 3 min, 6 sec, 10 sec, 0, 0, PULSE, 9, //solenoid num 3
  0 sec, 100 min, 8 sec, 12 sec, 0, 0, PULSE, 3, //4
  0 sec, 100 min, 8 sec, 12 sec, 0, 0, PULSE, 8, //5
  0 sec, 5.5 min, 5 sec, 8 sec, 0, 0, PULSE, 4, //6
  0 sec, 7.5 min, 4 sec, 7 sec, 0, 0, PULSE, 7,  //7
  0 sec, 4.5 min, 6 sec, 11 sec, 0, 0, PULSE, 5, //8
  0 sec, 9 min, 2 sec, 6 sec, 0, 0, PULSE, 6  //9

};


// millis function for offseting (debug from a specific time)

uint32_t myMillis()
{
  return millis() + (OFFSET  * 60 * 1000UL);
}


void setup()
{
  Serial.begin(115200);

  // Setup the button.


  // Setup the LED.
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  // Solenoid
  for (uint8_t i = 0; i < NUM_SOLENOID; i++)
  {
    pinMode(mySolenoid[i].pin, OUTPUT);
  }
}

void loop()
{
  update_time();
  controlSequenceScene();
  startSequence();
  middleSequence();
  endSequence();

}





//////****START SEQUENCE*****///////


void startSequence()
{

  if (buttonPressCount == 1)
  {
    currentMillis = myMillis() - millisTimePress;
    for (uint8_t x = 0; x < NUM_SOLENOID; x++)
    {
      // Is this led active
      if (mySolenoid[x].startTime < currentMillis)
      {
        // Do we need to calculate a next pulse time?


        if (mySolenoid[x].nextPulseStop < currentMillis)
        {
          // Turn off led
          digitalWrite(mySolenoid[x].pin, LOW);

          // Calculate nextPulseStart & nextPulseStop
          long  startOffset = random(mySolenoid[x].minPulse, mySolenoid[x].maxPulse);
          mySolenoid[x].nextPulseStart = startOffset + currentMillis;
          mySolenoid[x].nextPulseStop = mySolenoid[x].nextPulseStart + mySolenoid[x].pulseLength;
        }

        // Are we currently in the middle of a pulse?
        if (mySolenoid[x].nextPulseStart < currentMillis &&
            mySolenoid[x].nextPulseStop  > currentMillis)
        {
          //turn on solenoid
          digitalWrite(mySolenoid[x].pin, HIGH);
        }
      }
    }
  }
}


//////****MIDDLE SEQUENCE*****///////

void middleSequence()
{
  if (buttonPressCount == 2)
  {
    currentMillis = myMillis() - millisTimePress;
    for (uint8_t x = 0; x < NUM_SOLENOID; x++)
    {

      // Is this led active
      if (mySolenoidMiddle[x].startTime < currentMillis && mySolenoidMiddle[x].stopTime > currentMillis)
      {
        // Do we need to calculate a next pulse time?


        if (mySolenoidMiddle[x].nextPulseStop < currentMillis)
        {
          // Turn off led
          digitalWrite(mySolenoidMiddle[x].pin, LOW);

          // Calculate nextPulseStart & nextPulseStop
          long  startOffset = random(mySolenoidMiddle[x].minPulse, mySolenoidMiddle[x].maxPulse);
          mySolenoidMiddle[x].nextPulseStart = startOffset + currentMillis;
          mySolenoidMiddle[x].nextPulseStop = mySolenoidMiddle[x].nextPulseStart + mySolenoidMiddle[x].pulseLength;
        }

        // Are we currently in the middle of a pulse?
        if (mySolenoidMiddle[x].nextPulseStart < currentMillis &&
            mySolenoidMiddle[x].nextPulseStop  > currentMillis)
        {
          //turn on led
          digitalWrite(mySolenoidMiddle[x].pin, HIGH);
        }
      }
    }
  }

}

//////****END SEQUENCE*****///////

void endSequence()
{
  if (buttonPressCount >= 3)
  {
    //   currentMillis = myMillis() - millisTimePress;
    for (uint8_t x = 0; x < NUM_SOLENOID; x++)
    {
      //    digitalWrite(EndPin[x], LOW);
      digitalWrite(mySolenoid[x].pin, LOW);
    }
  }

}




void controlSequenceScene() {
  if (Serial.available() > 0) {
    char id = Serial.read();
    buttonPressCount = Serial.parseInt();
    if (id == scene_id) {

      millisTimePress = currentMillis;
      Serial.print("TimePress: ");
      Serial.println(millisTimePress);
      Serial.print("sequence: ");
      Serial.println(buttonPressCount);
    }
  }
}

void update_time()   // modified by noiasca
{
  static unsigned long previous_millis = 0;
  static unsigned long previous_print = 0;
  uint32_t currentMillis = myMillis();
  previous_millis = currentMillis;

  //us fraction for milliseconds
  milliseconds = currentMillis % 1000;

  seconds = (currentMillis / 1000) % 60;
  minutes = (currentMillis / 1000 / 60) % 60;
  hours = (currentMillis / 1000 / 60 / 60 ) % 24;

  //Check if interval has elapsed.

  if (myMillis() - previous_print > interval)
  {
    //Save print time.
    previous_print = myMillis();
    //Print time in format HH:mm:ss.
#if PrintingTime
    Serial.print("Time is: ");
    if (hours <= 9)
      Serial.print("0");
    Serial.print(hours);
    Serial.print(":");
    if (minutes <= 9)
      Serial.print("0");
    Serial.print(minutes);
    Serial.print(":");
    if (seconds <= 9)
      Serial.print("0");
    Serial.print(seconds);
    Serial.println();
#endif
  }
}

Do you see any results from this print statement on the serial console ?

Yes I do see ```
buttonPressCount

increase or decrease based on integer I send

You tested the Serial for presence only one char, but reading at least a two after that. This way you can't sure that you read a real data and not a junk

What should I change?

Why did you send ID as a byte and the Presscount as a text? Why don't send it as byte too?

And why did you transfer an ID at all, you didn't use it after reading.

I'm passing integer to Arduino serial . ID is in case I will want to pass more data to serial

Did you passed ID and presscount to Serial by different ways? If not - why did you read them differently?

I pass it as follow:

Screen Shot 2023-07-13 at 10.50.07

I think the issue is in that line:

    currentMillis = myMillis() - millisTimePress;


void startSequence()
{

  if (buttonPressCount == 1)
  {
    currentMillis = myMillis() - millisTimePress;
    for (uint8_t x = 0; x < NUM_SOLENOID; x++)

What such the syntax "0 sec" means in the code?

Is this at least compiled?

This may well be because in different places in the program you calculate the currentMillis in different ways - somewhere like this

currentMillis = myMillis() - millisTimePress;

and somewhere just that:

currentMillis = myMillis();

I don't quite understand why you are doing this.

Also, note that the currentMillis in the procedure update_time() is completely different variable than the global currentMillis. Changing the one is not affect another.

I have other problems with my code

I try to pulse the first 3 solenoids at same interval with the following code but it won't pulse as it should (it phase slowly and won't pulse together)

///middle solenoid struct//

struct SolenoidMiddle2
{
  unsigned long startTime;       // The time this led is first active after a buttun is first pressed
  unsigned long stopTime;        // The time this led becomes inactive after a buttun is second time pressed
  unsigned long minPulse;        // The minimum time the next pulse is generated
  unsigned long maxPulse;        // The maximum time the next pulse is generated
  unsigned long nextPulseStart;  // The time of the next pulse starts
  unsigned long nextPulseStop;   // The time of the next pulse  stops
  unsigned long pulseLength;     // The on time of the pulse
  uint8_t pin;                   // led GPIO pin number
};
   //                                       start   end    minpulse maxpulse
SolenoidMiddle2 mySolenoidMiddle2[NUM_SOLENOID] = {
                                            0 sec, 100 min , 2 sec, 2 sec, 0, 0, PULSE, 3, //solenoid num 1
                                            0 sec, 0.5 min, 2 sec, 2 sec, 0, 0, PULSE, 4,//solenoid num 2
                                            0 sec, 1 min, 2 sec, 2 sec, 0, 0, PULSE, 5, //solenoid num 3
                                            0 sec, 100 min, 8 sec, 12 sec, 0, 0, PULSE, 6, //4 
                                            0 sec, 100 min, 8 sec, 12 sec, 0, 0, PULSE, 8, //5
                                            0 sec, 5.5 min, 5 sec, 8 sec, 0, 0, PULSE, 4, //6
                                            0 sec, 7.5 min, 4 sec, 7 sec, 0, 0, PULSE, 7,  //7
                                            0 sec, 4.5 min, 6 sec, 11 sec, 0, 0, PULSE, 5, //8
                                            0 sec, 9 min, 2 sec, 6 sec, 0, 0, PULSE, 6  //9


void middleSequence2()
{
  if(buttonPressCount == 3)
 {
   currentMillis = myMillis() - millisTimePress;
for (uint8_t x = 0; x < NUM_SOLENOID; x++)
  {
   
    // Is this led active
    if (mySolenoidMiddle2[x].startTime < currentMillis && mySolenoidMiddle2[x].stopTime > currentMillis)
    {
      // Do we need to calculate a next pulse time?
  

      if (mySolenoidMiddle2[x].nextPulseStop < currentMillis)
      {
        // Turn off led
        digitalWrite(mySolenoidMiddle2[x].pin, LOW);

        // Calculate nextPulseStart & nextPulseStop
        long  startOffset =(mySolenoidMiddle2[x].minPulse);
        mySolenoidMiddle2[x].nextPulseStart = startOffset + currentMillis;
        mySolenoidMiddle2[x].nextPulseStop = mySolenoidMiddle2[x].nextPulseStart + mySolenoidMiddle2[x].pulseLength;
      }

      // Are we currently in the middle of a pulse?
      if (mySolenoidMiddle2[x].nextPulseStart < currentMillis &&
          mySolenoidMiddle2[x].nextPulseStop  > currentMillis)
      {
        //turn on led
        digitalWrite(mySolenoidMiddle2[x].pin, HIGH);
      }
    }
  }
}

Take a textbook read and learn how to design a structured array.

why my timining is not accurate?

I have this struct:


// LED structure for button press 2 (middle sequence)
struct Led2
{
  unsigned long startTime;       // The time this LED is first active after button press 2
  unsigned long minPulse;        // The minimum time between pulses
  unsigned long maxPulse;        // The maximum time between pulses
   unsigned long nextPulseStart;  // The time of the next pulse starts
  unsigned long nextPulseStop;   // The time of the next pulse  stops
  unsigned long pulseLength;     // The on time of the pulse
  unsigned long stopTime;        // The time when the LED should stop
  uint8_t pin;                   // LED GPIO pin number
};
// LED array for button press 2 (middle sequence)
Led2 myLed2[NUM_LEDS] = {
    {0 sec, 1 sec, 2 sec,0,0, PULSE, 10 sec, 3},    // LED num 1
    {0 sec, 1 sec, 2 sec,0,0, PULSE, 10 sec, 7},    // LED num 2
    {0 sec, 1 sec, 2 sec,0,0, PULSE, 10 sec, 8},    // LED num 3
    {0 sec, 1 sec, 2 sec,0,0, PULSE, 10 sec, A0}, // LED num 4
};

and this middlesequence:

void middleSequence()
{
  if (buttonPressCount == 2)
  {
    currentMillis = millis() - millisTimePress;
    for (uint8_t x = 0; x < NUM_LEDS; x++)
    {
      // Is this LED active
      if (myLed2[x].startTime < currentMillis && myLed2[x].stopTime > currentMillis)
      {
        // Do we need to calculate a next pulse time?
        if (myLed2[x].nextPulseStop < currentMillis)
        {
          // Turn off LED
          digitalWrite(myLed2[x].pin, LOW);

          // Calculate nextPulseStart & nextPulseStop
          long startOffset = random(myLed2[x].minPulse, myLed2[x].maxPulse);
          myLed2[x].nextPulseStart = startOffset + currentMillis;
          myLed2[x].nextPulseStop = myLed2[x].nextPulseStart + myLed2[x].pulseLength;
        }

        // Are we currently in the middle of a pulse?
        if (myLed2[x].nextPulseStart < currentMillis && myLed2[x].nextPulseStop > currentMillis)
        {
          // Turn on LED
          digitalWrite(myLed2[x].pin, HIGH);
        }
      }
      else if (myLed2[x].stopTime <= currentMillis)
      {
        // LED should be stopped, turn it off
        digitalWrite(myLed2[x].pin, LOW);
      }
    }
  }
}

all the leds should stop pulsing after 10 seconds but I count and its took the 15 seconds. why is this difference?

full code:

#define NUM_LEDS 4
#define PULSE 150

//time translating//
#define sec * 1000
#define min * 60000
#define ms * 1


// LED structure for button press 1
struct Led1
{
  unsigned long startTime;       // The time this LED is first active after a button is first pressed
  unsigned long minPulse;        // The minimum time between pulses
  unsigned long maxPulse;        // The maximum time between pulses
   unsigned long nextPulseStart;  // The time of the next pulse starts
  unsigned long nextPulseStop;   // The time of the next pulse  stops
  unsigned long pulseLength;     // The on time of the pulse
  uint8_t pin;                   // LED GPIO pin number
};

// LED array for button press 1
Led1 myLed1[NUM_LEDS] = {
    {0 sec, 2 sec, 6 sec,0,0, PULSE, 3},    // LED num 1
    {5 sec, 3 sec, 7 sec, 0,0, PULSE, 7},   // LED num 2
    {8 sec, 5 sec, 7 sec, 0, 0, PULSE, 8},   // LED num 3
    {10 sec, 2 sec, 7 sec,0, 0, PULSE, A0},  // LED num 4
};

// LED structure for button press 2 (middle sequence)
struct Led2
{
  unsigned long startTime;       // The time this LED is first active after button press 2
  unsigned long minPulse;        // The minimum time between pulses
  unsigned long maxPulse;        // The maximum time between pulses
   unsigned long nextPulseStart;  // The time of the next pulse starts
  unsigned long nextPulseStop;   // The time of the next pulse  stops
  unsigned long pulseLength;     // The on time of the pulse
  unsigned long stopTime;        // The time when the LED should stop
  uint8_t pin;                   // LED GPIO pin number
};

// LED array for button press 2 (middle sequence)
Led2 myLed2[NUM_LEDS] = {
    {0 sec, 1 sec, 2 sec,0,0, PULSE, 10 sec, 3},    // LED num 1
    {0 sec, 1 sec, 2 sec,0,0, PULSE, 10 sec, 7},    // LED num 2
    {0 sec, 1 sec, 2 sec,0,0, PULSE, 10 sec, 8},    // LED num 3
    {0 sec, 1 sec, 2 sec,0,0, PULSE, 10 sec, A0}, // LED num 4
};

const uint8_t buttonPin = 11;
uint8_t buttonPressCount = 0;
unsigned long millisTimePress = 0;
uint32_t currentMillis;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);

  // Initialize LED pins
  for (uint8_t i = 0; i < NUM_LEDS; i++)
  {
    pinMode(myLed1[i].pin, OUTPUT);
    pinMode(myLed2[i].pin, OUTPUT);
  }
}

void loop()
{
  buttonPress();
  startSequence();
  middleSequence();
  update_time();
}

void buttonPress()
{
  static uint8_t lastButtonState = HIGH;
  uint8_t currentButtonState = digitalRead(buttonPin);

  if (currentButtonState != lastButtonState)
  {
    delay(20); // Debounce
    if (currentButtonState == LOW)
    {
      buttonPressCount++;
      millisTimePress = millis();
      Serial.print("Button Press Count: ");
      Serial.println(buttonPressCount);

      if (buttonPressCount == 2)
      {
        // Set stop times for LED2 based on button press time
        for (uint8_t i = 0; i < NUM_LEDS; i++)
        {
          myLed2[i].stopTime = millisTimePress + myLed2[i].stopTime;
        }
      }
    }
    lastButtonState = currentButtonState;
  }
}

void startSequence()
{
  if (buttonPressCount == 1)
  {
    currentMillis = millis() - millisTimePress;
    for (uint8_t x = 0; x < NUM_LEDS; x++)
    {
      // Is this LED active
      if (myLed1[x].startTime < currentMillis)
      {
        // Do we need to calculate a next pulse time?
        if (myLed1[x].nextPulseStop < currentMillis)
        {
          // Turn off LED
          digitalWrite(myLed1[x].pin, LOW);

          // Calculate nextPulseStart & nextPulseStop
          long startOffset = random(myLed1[x].minPulse, myLed1[x].maxPulse);
          myLed1[x].nextPulseStart = startOffset + currentMillis;
          myLed1[x].nextPulseStop = myLed1[x].nextPulseStart + myLed1[x].pulseLength;
        }

        // Are we currently in the middle of a pulse?
        if (myLed1[x].nextPulseStart < currentMillis && myLed1[x].nextPulseStop > currentMillis)
        {
          // Turn on LED
          digitalWrite(myLed1[x].pin, HIGH);
        }
      }
    }
  }
}

void middleSequence()
{
  if (buttonPressCount == 2)
  {
    currentMillis = millis() - millisTimePress;
    for (uint8_t x = 0; x < NUM_LEDS; x++)
    {
      // Is this LED active
      if (myLed2[x].startTime < currentMillis && myLed2[x].stopTime > currentMillis)
      {
        // Do we need to calculate a next pulse time?
        if (myLed2[x].nextPulseStop < currentMillis)
        {
          // Turn off LED
          digitalWrite(myLed2[x].pin, LOW);

          // Calculate nextPulseStart & nextPulseStop
          long startOffset = random(myLed2[x].minPulse, myLed2[x].maxPulse);
          myLed2[x].nextPulseStart = startOffset + currentMillis;
          myLed2[x].nextPulseStop = myLed2[x].nextPulseStart + myLed2[x].pulseLength;
        }

        // Are we currently in the middle of a pulse?
        if (myLed2[x].nextPulseStart < currentMillis && myLed2[x].nextPulseStop > currentMillis)
        {
          // Turn on LED
          digitalWrite(myLed2[x].pin, HIGH);
        }
      }
      else if (myLed2[x].stopTime <= currentMillis)
      {
        // LED should be stopped, turn it off
        digitalWrite(myLed2[x].pin, LOW);
      }
    }
  }
}

void update_time()
{
  static unsigned long previousTime = 0;
  unsigned long currentTime = millis();

  if (currentTime - previousTime >= 1000)
  {
    previousTime = currentTime;

    unsigned long seconds = currentTime / 1000;
    unsigned long minutes = seconds / 60;
    unsigned long hours = minutes / 60;

    seconds = seconds % 60;
    minutes = minutes % 60;
    hours = hours % 24;

    Serial.print("Time: ");
    if (hours < 10)
      Serial.print("0");
    Serial.print(hours);
    Serial.print(":");
    if (minutes < 10)
      Serial.print("0");
    Serial.print(minutes);
    Serial.print(":");
    if (seconds < 10)
      Serial.print("0");
    Serial.println(seconds);
  }
}