i need Help in using millis as delay, combining two codes

hi all, im a newbie in arduino programming, my problem is when i combine a 2 code the other code is not working, but they are working perfectly if not combine.
the 1st code is infrared sensor counter (for basketball hoop score) (this code is not working when combine)
the 2nd code is a countdown timer, turns on/off a stepper and a solenoid
i know what causes them not to work if combine, its the “delay()” function. i spend three nights to study the millis function but i still dont get it.

#define NUMB_OF_PRESET          6       /* Available preset                 */
#define NUMB_OF_SEGMENTS        21       /* Available segments               */
#define MAX_NUMB                999       /* High counting range limit        */

#define DISP_PIN_0              0        
#define DISP_PIN_1              1        
#define DISP_PIN_2              2        
#define DISP_PIN_3              3        
#define DISP_PIN_4              4       
#define DISP_PIN_5              5       
#define DISP_PIN_6              6        
#define DISP_PIN_7              7       
#define DISP_PIN_8              8      
#define DISP_PIN_9              9        
#define DISP_PIN_10             10       
#define DISP_PIN_11             11        
#define DISP_PIN_12             12       
#define DISP_PIN_13             13        
#define DISP_PIN_14             14        
#define DISP_PIN_15             15        /* digital pin input for segment */
#define DISP_PIN_16             16        
#define DISP_PIN_17             17       
#define DISP_PIN_18             18       
#define DISP_PIN_19             19        
#define DISP_PIN_20             20        

#define ledPin1                 21         /* timer led start here */
#define ledPin2                 22
#define ledPin3                 24
#define ledPin4                 26
#define ledPin5                 28
#define ledPin6                 30
#define ledPin7                 32

#define ledPin8                 33
#define ledPin9                 35
#define ledPin10                37
#define ledPin11                39
#define ledPin12                41
#define ledPin13                43
#define ledPin14                45

#define IR1                     50        /* digital pin input for ir sensor  */

#define dirPin                   47
#define stepPin                  46
#define relayPin                 48
#define solenoidPin              49  
#define stepsPerRevolution      400

int time = 300;

const byte displaySEG[NUMB_OF_PRESET][NUMB_OF_SEGMENTS] = {
  /*  A     B     C     D     E     F     G            */
 { LOW, LOW, LOW, LOW, LOW, LOW, HIGH, LOW, LOW, LOW, LOW, LOW, LOW, HIGH, LOW, LOW, LOW, LOW, LOW, LOW, HIGH}, // ---
  {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH}, // 002
  {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH}, // 004
  {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH}, // 006
  {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}, // 008
  {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH, HIGH, LOW, LOW, LOW, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW }, // 010
  
 
 
};


int IR1_out = HIGH; /* Avoiding initial false detections.    */

int counter = 0;
int i = 0;

void setDisplayValue(int n) {
  for (i = 0; i < NUMB_OF_SEGMENTS; i++) {
    digitalWrite(DISP_PIN_0 + i, displaySEG[n % 6][i]);
  }
}
#define initDisplay()     setDisplayValue(0)

/**
     This function increase counter and update display.
     Note that if counter become greater than MAX_NUMB it wraps to 1.
*/
void increaseAndDisplay() {
  counter++;
  if (counter > MAX_NUMB) {
    counter = 1;
  }
  setDisplayValue(counter);
}




void setup () {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin8, OUTPUT);
  pinMode(ledPin9, OUTPUT);
  pinMode(ledPin10, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin14, OUTPUT);
 pinMode(solenoidPin, OUTPUT);
 
  pinMode(IR1, INPUT);
  /* Setting Display related PIN as OUTPUT */
  for (i = 0; i < NUMB_OF_SEGMENTS; i++) {
    pinMode(DISP_PIN_0 + i, OUTPUT);
  }
  initDisplay();
  
}
void loop () {  
  IR1_out = digitalRead(IR1);
  if (IR1_out == LOW) {
    increaseAndDisplay();
    delay(400);
  }
  digitalWrite(solenoidPin, HIGH);      //high is open // sending +v to solenoid..i use a tip122 transistor here
  digitalWrite(relayPin, LOW);          // LOW signal will make the relay to activate
  delay(300);


  digitalWrite(dirPin, HIGH); // direction of stepper

  for (int i = 0; i < stepsPerRevolution; i++) {

    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }
  digitalWrite(relayPin, HIGH);
digitalWrite(solenoidPin, LOW);
  
  // timer code here

  digitalWrite(ledPin1, HIGH);                   ////
  digitalWrite(ledPin2, LOW);                  //
  digitalWrite(ledPin3, HIGH);                /////
  digitalWrite(ledPin4, HIGH);               //  //
  digitalWrite(ledPin5, HIGH);               ////
  digitalWrite(ledPin6, HIGH);
  digitalWrite(ledPin7, HIGH);
  digitalWrite(ledPin8, HIGH);
  digitalWrite(ledPin9, HIGH);                    ////
  digitalWrite(ledPin10, HIGH);                  //   //
  digitalWrite(ledPin11, HIGH);                  //   //
  digitalWrite(ledPin12, HIGH);                  //   //
  digitalWrite(ledPin13, HIGH);                   ////
  digitalWrite(ledPin14, LOW);
delay(time);

  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);               //////
  digitalWrite(ledPin3, HIGH);             //
  digitalWrite(ledPin4, HIGH);            //////
  digitalWrite(ledPin5, LOW);                //
  digitalWrite(ledPin6, HIGH);           //////
  digitalWrite(ledPin7, HIGH);
  digitalWrite(ledPin8, HIGH);                   ////
  digitalWrite(ledPin9, HIGH);                 //   //
  digitalWrite(ledPin10, HIGH);                //////
  digitalWrite(ledPin11, LOW);                    //
  digitalWrite(ledPin12, LOW);                ////
  digitalWrite(ledPin13, HIGH);
  digitalWrite(ledPin14, HIGH);
delay(time);

 // and so on, countdown timer is 60 - zero


   digitalWrite(solenoidPin, HIGH);   // HIGH will activate the solenoid and will open the lock
  digitalWrite(relayPin, LOW);      // LOW will activate the relay
  delay(300);
  
  for (int i = 0; i < stepsPerRevolution; i++) {
    digitalWrite(dirPin, LOW);

    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }
    
   digitalWrite(relayPin, HIGH);  // HIGH will turn off the relay
digitalWrite(solenoidPin, LOW);   // LOW will deactivate the solenoid and will close the lock
delay(50);

  delay(5000000);

}

Is this any help?

i spend three nights to study the millis function

Did you read Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

Do you understand the examples given ?

Start by understanding that you cannot simply drop in code using millis() to replace a delay() and make timing non blocking. Using millis() for timing is fundamentally very easy. Save the value of millis() when a start action occurs then check each time through loop() whether the required period has elapsed. If not then go round again. If the period has elapsed then do what you need and save the current value of millis() as the start time for the next period

hello. im making a progress regarding this millis… i manage to combine two codes…
here is my code. i know there is much simplier way to do this with millis, but this is all i can do for now, (its working and i dont know why lol)

void loop() {  

  /*------------------------------1st code, continuously reads code from the sensor-----------------------*/
  unsigned long currentTime = millis();
  
  IR1_out = digitalRead(IR1);
  if (IR1_out == LOW) 
   if(currentTime - previousTime >= irInterval)
   {
    increaseAndDisplay();
    previousTime = currentTime;
  }
    /*------------------------------1st code, continuously reads code from the sensor-----------------------*/
    
  /*-----------2nd code the countdown timer stepper motor, solenoid -------------------*/
  if(millis() == 1000UL){
    digitalWrite(relayPin, LOW);
    digitalWrite(solenoidPin, HIGH);
    
  }
   if(millis() == 1300UL) {
     digitalWrite(dirPin, HIGH);
     for(int i = 0; i < stepsPerRevolution; i++){

    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }
   }
  if(millis() == 3300UL){
    digitalWrite(solenoidPin, LOW);
    digitalWrite(relayPin, HIGH);
  }
  /*-------------------------start of timer code-------------------*/
 if(millis() == 3400UL){ 
  digitalWrite(ledPin1, HIGH);                  //   03
  digitalWrite(ledPin2, LOW);                
  digitalWrite(ledPin3, LOW);               
  digitalWrite(ledPin4, HIGH);                   
  digitalWrite(ledPin5, LOW);                 
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, HIGH);
  digitalWrite(ledPin8, HIGH);                 
  digitalWrite(ledPin9, HIGH);               
  digitalWrite(ledPin10, HIGH);               
  digitalWrite(ledPin11, HIGH);                    
  digitalWrite(ledPin12, LOW);               
  digitalWrite(ledPin13, LOW);
  digitalWrite(ledPin14, HIGH);
    }
  
  if(millis() == 4200UL){ 
  digitalWrite(ledPin1, LOW);                  //   02
  digitalWrite(ledPin2, LOW);                
  digitalWrite(ledPin3, LOW);               
  digitalWrite(ledPin4, HIGH);                   
  digitalWrite(ledPin5, LOW);                 
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, HIGH);
  digitalWrite(ledPin8, HIGH);                 
  digitalWrite(ledPin9, HIGH);               
  digitalWrite(ledPin10, LOW);               
  digitalWrite(ledPin11, HIGH);                    
  digitalWrite(ledPin12, HIGH);               
  digitalWrite(ledPin13, LOW);
  digitalWrite(ledPin14, HIGH);
    }

     if(millis() == 5000UL){ 
  digitalWrite(ledPin1, LOW);                  //   01
  digitalWrite(ledPin2, LOW);                
  digitalWrite(ledPin3, LOW);               
  digitalWrite(ledPin4, HIGH);                   
  digitalWrite(ledPin5, LOW);                 
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, LOW);
  digitalWrite(ledPin8, LOW);                 
  digitalWrite(ledPin9, HIGH);               
  digitalWrite(ledPin10, HIGH);               
  digitalWrite(ledPin11, LOW);                    
  digitalWrite(ledPin12, LOW);               
  digitalWrite(ledPin13, LOW);
  digitalWrite(ledPin14, LOW);
    }
  
  if(millis() == 5800UL){ 
  digitalWrite(ledPin1, HIGH);                  //GO
  digitalWrite(ledPin2, LOW);                
  digitalWrite(ledPin3, HIGH);               
  digitalWrite(ledPin4, HIGH);                    
  digitalWrite(ledPin5, HIGH);                 
  digitalWrite(ledPin6, HIGH);
  digitalWrite(ledPin7, HIGH);
  digitalWrite(ledPin8, HIGH);               
  digitalWrite(ledPin9, HIGH);             
  digitalWrite(ledPin10, HIGH);         
  digitalWrite(ledPin11, HIGH);                   
  digitalWrite(ledPin12, HIGH);                 
  digitalWrite(ledPin13, HIGH);
  digitalWrite(ledPin14, LOW);
    }
    
    if(millis() == 6600UL){  
  digitalWrite(ledPin1, HIGH);                   ////
  digitalWrite(ledPin2, LOW);                  //
  digitalWrite(ledPin3, HIGH);                /////
  digitalWrite(ledPin4, HIGH);               //  //
  digitalWrite(ledPin5, HIGH);               ////
  digitalWrite(ledPin6, HIGH);
  digitalWrite(ledPin7, HIGH);
  digitalWrite(ledPin8, HIGH);
  digitalWrite(ledPin9, HIGH);                    ////
  digitalWrite(ledPin10, HIGH);                  //   //
  digitalWrite(ledPin11, HIGH);                  //   //
  digitalWrite(ledPin12, HIGH);                  //   //
  digitalWrite(ledPin13, HIGH);                   ////
  digitalWrite(ledPin14, LOW);
    }
    if(millis() == 7500UL){
       digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);               //////
  digitalWrite(ledPin3, HIGH);             //
  digitalWrite(ledPin4, HIGH);            //////
  digitalWrite(ledPin5, LOW);                //
  digitalWrite(ledPin6, HIGH);           //////
  digitalWrite(ledPin7, HIGH);
  digitalWrite(ledPin8, HIGH);                   ////
  digitalWrite(ledPin9, HIGH);                 //   //
  digitalWrite(ledPin10, HIGH);                //////
  digitalWrite(ledPin11, LOW);                    //
  digitalWrite(ledPin12, LOW);                ////
  digitalWrite(ledPin13, HIGH);
  digitalWrite(ledPin14, HIGH);
    }
    if(millis() == 8400UL){
       digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);               //////
  digitalWrite(ledPin3, HIGH);             //
  digitalWrite(ledPin4, HIGH);            //////
  digitalWrite(ledPin5, LOW);                //
  digitalWrite(ledPin6, HIGH);           //////
  digitalWrite(ledPin7, HIGH);
  digitalWrite(ledPin8, HIGH);             ////
  digitalWrite(ledPin9, HIGH);          //    //
  digitalWrite(ledPin10, HIGH);          ////
  digitalWrite(ledPin11, HIGH);       //    //
  digitalWrite(ledPin12, HIGH);        ////
  digitalWrite(ledPin13, HIGH);
  digitalWrite(ledPin14, HIGH);
    }
    if(millis() == 9300UL){
      
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);               //////
  digitalWrite(ledPin3, HIGH);             //
  digitalWrite(ledPin4, HIGH);            //////
  digitalWrite(ledPin5, LOW);                //
  digitalWrite(ledPin6, HIGH);           //////
  digitalWrite(ledPin7, HIGH);
  digitalWrite(ledPin8, HIGH);          //////
  digitalWrite(ledPin9, HIGH);             //
  digitalWrite(ledPin10, HIGH);           //
  digitalWrite(ledPin11, LOW);           //
  digitalWrite(ledPin12, LOW);          //
  digitalWrite(ledPin13, LOW);
  digitalWrite(ledPin14, LOW);
    }
    if(millis() == 10200UL){
      
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);               //////
  digitalWrite(ledPin3, HIGH);             //
  digitalWrite(ledPin4, HIGH);            //////
  digitalWrite(ledPin5, LOW);                //
  digitalWrite(ledPin6, HIGH);           //////
  digitalWrite(ledPin7, HIGH);
  digitalWrite(ledPin8, HIGH);
  digitalWrite(ledPin9, LOW);             //////
  digitalWrite(ledPin10, HIGH);          //
  digitalWrite(ledPin11, HIGH);         //////
  digitalWrite(ledPin12, HIGH);        //  //
  digitalWrite(ledPin13, HIGH);       //////
  digitalWrite(ledPin14, HIGH);
    }
    if(millis() == 11100UL) //and so on

The value returned by millis() starts at 0 when the Arduino is powered up or reset then increments by 1 every millisecond for 49 and a bit days before rolling over to zero.

if(millis() == 1000UL)

So this will be true for one millisecond every 49 and a bit days. The same goes for each of your tests for an explicit value of millis(). Is that what you want to test for ?

hi, accuracy of time is not really necessary, i just want the sensor code to run everytime i need it run while there is another code is running, and we cannot do this with delay function. and this is design to be shut down and turn on every day. so im not worrying about the 49 and a bit days. this code is ok for me , but if there is a much simplier way or much better way, i really appreciate that, note, this is the best that my brain can do, lol