Trying to add IR sensor in loop.

Hi to all genius,
Everything was working prior to adding the IR sensor. What I want to achieve in the loop is sequence stop at IR, If it is high the rest of the code should work.
Please help me out!!

#define STEP 11
#define DIR 10
#define Pin1 8  // Relay 1  
#define Pin2 7  // Relay 2  
#define Pin3 6  // Relay 3  
#define IR 3  
#define home_switch 2 // Pin 12 connected to Home Switch (MicroSwitch)
int direction;         // Variable to set Rotation (CW-CCW) of the motor
int steps;             // Used to set HOME position after Homing is completed
byte processState;  // 0 is OFF, 1 is ON
byte pin4prevState;
byte pin5prevState;
byte readState;
void setup() {
  
  // Sets the two pins as Outputs
  pinMode(STEP,OUTPUT);
  pinMode(DIR,OUTPUT);
  pinMode(Pin1,OUTPUT);
  pinMode(Pin2,OUTPUT);
  pinMode(Pin3,OUTPUT);
  pinMode(IR,INPUT_PULLUP);
  digitalWrite(Pin1,HIGH);
  digitalWrite(Pin2,HIGH);
  digitalWrite(Pin3,HIGH);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(home_switch, INPUT_PULLUP);
  digitalWrite(STEP, HIGH);  // Wake up EasyDriver
  digitalWrite(DIR,HIGH);
  delay(10);  // Wait for EasyDriver wake up

  // Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switch)) {  // Do this until the switch is activated   
    digitalWrite(DIR, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(STEP, HIGH);
    delay(3);                       // Delay to slow down speed of Stepper
    digitalWrite(STEP, LOW);
    delay(3);   
  }
  while (!digitalRead(home_switch)) { // Do this until the switch is not activated
    digitalWrite(DIR, HIGH); 
    digitalWrite(STEP, HIGH);
    delay(3);                       // More delay to slow even more while moving away from switch
    digitalWrite(STEP, LOW);
    delay(3);
  }
  steps=0;  // Reset position variable to zero
}
 
void loop() {
readState = digitalRead( 4 );
 if ( pin4prevState != readState )
 {
  pin4prevState = readState;
  if ( readState == LOW ) // button down with INPUT_PULLUP drains the pin to ground.
  {
   processState = 1;
  }
 }
 readState = digitalRead( 5 ); // note that the stop button is read last. press both makes stop
 if ( pin5prevState != readState )
 {
  pin5prevState = readState;
  if ( readState == LOW ) // button down with INPUT_PULLUP drains the pin to ground.
  {
   processState = 0;
  }
 }
 if ( processState )
 {

while (digitalRead(home_switch)) {  // Do this until the switch is activated   
      digitalWrite(DIR, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(STEP, HIGH);
      delay(1);                       // Delay to slow down speed of Stepper
      digitalWrite(STEP, LOW);
      delay(1);   
  }
   while (!digitalRead(home_switch)) { // Do this until the switch is not activated
      digitalWrite(DIR, HIGH); 
      digitalWrite(STEP, HIGH);
      delay(1);                       // More delay to slow even more while moving away from switch
      digitalWrite(STEP, LOW);
      delay(1);
   
   
      digitalWrite(Pin1,HIGH); // locker & cutter
      delay(300);
      digitalWrite(Pin1,LOW);
      delay(300);
   }
if(digitalRead(IR)==HIGH){
      digitalWrite(Pin2,HIGH); // punch
      delay(600);
      digitalWrite(Pin3,HIGH); // release
      delay(300);
      digitalWrite(Pin3,LOW); 
      delay(100);
      digitalWrite(Pin2,LOW);
      delay(300);
}
 }
 else {
  // 
 }
}

Please, always format your sketches before you show them to us. (Use CTRL T in the Arduino IDE.)

#define STEP 11
#define DIR 10
#define Pin1 8  // Relay 1 
#define Pin2 7  // Relay 2 
#define Pin3 6  // Relay 3 
#define IR 3
#define home_switch 2 // Pin 12 connected to Home Switch (MicroSwitch)
int direction;         // Variable to set Rotation (CW-CCW) of the motor
int steps;             // Used to set HOME position after Homing is completed
byte processState;  // 0 is OFF, 1 is ON
byte pin4prevState;
byte pin5prevState;
byte readState;
void setup()
{
  // Sets the two pins as Outputs
  pinMode(STEP, OUTPUT);
  pinMode(DIR, OUTPUT);
  pinMode(Pin1, OUTPUT);
  pinMode(Pin2, OUTPUT);
  pinMode(Pin3, OUTPUT);
  pinMode(IR, INPUT_PULLUP);
  digitalWrite(Pin1, HIGH);
  digitalWrite(Pin2, HIGH);
  digitalWrite(Pin3, HIGH);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(home_switch, INPUT_PULLUP);
  digitalWrite(STEP, HIGH);  // Wake up EasyDriver
  digitalWrite(DIR, HIGH);
  delay(10);  // Wait for EasyDriver wake up

  // Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switch))    // Do this until the switch is activated
  {
    digitalWrite(DIR, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(STEP, HIGH);
    delay(3);                       // Delay to slow down speed of Stepper
    digitalWrite(STEP, LOW);
    delay(3);
  }
  
  while (!digitalRead(home_switch))   // Do this until the switch is not activated
  {
    digitalWrite(DIR, HIGH);
    digitalWrite(STEP, HIGH);
    delay(3);                       // More delay to slow even more while moving away from switch
    digitalWrite(STEP, LOW);
    delay(3);
  }
  
  steps = 0; // Reset position variable to zero
}

void loop()
{
  readState = digitalRead( 4 );
  
  if ( pin4prevState != readState )
  {
    pin4prevState = readState;
    if ( readState == LOW ) // button down with INPUT_PULLUP drains the pin to ground.
    {
      processState = 1;
    }
  }
  
  readState = digitalRead( 5 ); // note that the stop button is read last. press both makes stop
  
  if ( pin5prevState != readState )
  {
    pin5prevState = readState;
    if ( readState == LOW ) // button down with INPUT_PULLUP drains the pin to ground.
    {
      processState = 0;
    }
  }
  
  if ( processState )
  {
    while (digitalRead(home_switch))    // Do this until the switch is activated
    {
      digitalWrite(DIR, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(STEP, HIGH);
      delay(1);                       // Delay to slow down speed of Stepper
      digitalWrite(STEP, LOW);
      delay(1);
    }
    
    while (!digitalRead(home_switch))   // Do this until the switch is not activated
    {
      digitalWrite(DIR, HIGH);
      digitalWrite(STEP, HIGH);
      delay(1);                       // More delay to slow even more while moving away from switch
      digitalWrite(STEP, LOW);
      delay(1);
      digitalWrite(Pin1, HIGH); // locker & cutter
      delay(300);
      digitalWrite(Pin1, LOW);
      delay(300);
    }
    
    if (digitalRead(IR) == HIGH)
    {
      digitalWrite(Pin2, HIGH); // punch
      delay(600);
      digitalWrite(Pin3, HIGH); // release
      delay(300);
      digitalWrite(Pin3, LOW);
      delay(100);
      digitalWrite(Pin2, LOW);
      delay(300);
    }
  }
  
} //END of loop()

What is not working.

BTW
Suggest you avoid 'while and do' loops and learn to use 'State Machine programming' techniques.

IR sensor part is not working

if (digitalRead(IR) == HIGH) {
digitalWrite(Pin2, HIGH); // punch
delay(600);
digitalWrite(Pin3, HIGH); // release
delay(300);
digitalWrite(Pin3, LOW);
delay(100);
digitalWrite(Pin2, LOW);
delay(300);
}

Tell us 'exactly' what is it to do ?

Is the IR code supposed to be inside of if ( processState == 1 ) { . . . }?

if ( processState == 1 )
 {
   . . .
   
   if (digitalRead(IR) == HIGH)
   {
     digitalWrite(Pin2, HIGH);         // punch
     delay(600);
     digitalWrite(Pin3, HIGH);         // release
     delay(300);
     digitalWrite(Pin3, LOW);
     delay(100);
     digitalWrite(Pin2, LOW);
     delay(300);
   }
 }

} //END of loop()

Yes exactly, You got it. Help me out please.

What is supposed to happen when digitalRead(IR) == HIGH ?


Explain this statement more:

" If it is high the rest of the code should work."


BTW
1 millisecond seems very fast
You are using a EasyDriver so probably okay.

yes, code under IR should only start unless and until IR goes HIGH.
And the loop goes again and again.

Question:

You always have digitalWrite(DIR, HIGH); you never have digitalWrite(DIR, LOW);

Does the motor ever reverse ?


Please explain what this code does.

if (digitalRead(IR) == HIGH) {
      digitalWrite(Pin2, HIGH); // punch
      delay(600);
      digitalWrite(Pin3, HIGH); // release
      delay(300);
      digitalWrite(Pin3, LOW);
      delay(100);
      digitalWrite(Pin2, LOW);
      delay(300);
    }

Please show us a good schematic for the project too.

Stepper rotates always clockwise.
Home_switch stops it after 1 revolution.
Relay 1 start/ stop with delay.
check IR status if it LOW do nothing
If IR HIGH rest Relay 2 & 3 start/ stop with delay.
Loop ends
Start the sequence again and again

Don't really understand what the project is about but here is a start of putting your code into a State Machine format.

You will have to test things out to see what works and what doesn't.

//VERSION 2.00

#define STEP 11
#define DIR  10
#define Pin1 8         // Relay 1 
#define Pin2 7         // Relay 2 
#define Pin3 6         // Relay 3 
#define IR   3
#define home_switch 2  // Pin 12 connected to Home Switch (MicroSwitch)

int direction;         // Variable to set Rotation (CW-CCW) of the motor
int steps;             // Used to set HOME position after Homing is completed
byte processState;     // 0 is OFF, 1 is ON
byte pin4prevState;
byte pin5prevState;
byte readState;

unsigned long currentMillis;
unsigned long switchMillis;

enum STATES {Startup, Homing, ScanSwitches};

STATES mSTATE = Startup;

//************************************************************************************
void setup()
{
  // Sets the two pins as Outputs
  pinMode(STEP, OUTPUT);
  pinMode(DIR,  OUTPUT);
  pinMode(Pin1, OUTPUT);
  pinMode(Pin2, OUTPUT);
  pinMode(Pin3, OUTPUT);

  pinMode(IR, INPUT_PULLUP);

  digitalWrite(Pin1, HIGH);
  digitalWrite(Pin2, HIGH);
  digitalWrite(Pin3, HIGH);

  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(home_switch, INPUT_PULLUP);

  digitalWrite(STEP, HIGH);           // Wake up EasyDriver
  digitalWrite(DIR, HIGH);

  delay(10);                          // Wait for EasyDriver wake up

} //END of setup()

//************************************************************************************
void loop()
{
  currentMillis = millis();

  stateMachine();

} //END of loop()

//************************************************************************************
void stateMachine()
{
  switch (mSTATE)
  {
    //*****************************
    case Startup:
      {
        // Start Homing procedure of Stepper Motor at startup
        //*****************************
        while (digitalRead(home_switch))    // Do this until the switch is activated
        {
          digitalWrite(DIR, HIGH);          // (HIGH = anti-clockwise / LOW = clockwise)
          digitalWrite(STEP, HIGH);
          delay(3);                         // Delay to slow down speed of Stepper
          digitalWrite(STEP, LOW);
          delay(3);
        }

        //*****************************
        while (!digitalRead(home_switch))   // Do this until the switch is not activated
        {
          digitalWrite(DIR, HIGH);
          digitalWrite(STEP, HIGH);
          delay(3);                         // More delay to slow even more while moving away from switch
          digitalWrite(STEP, LOW);
          delay(3);
        }

        steps = 0;                          // Reset position variable to zero

        mSTATE = ScanSwitches;
      }
      break;

    //*****************************
    case Homing:
      {
        if ( processState == 1 )
        {
          //*****************************
          while (digitalRead(home_switch))    // Do this until the switch is activated
          {
            digitalWrite(DIR, HIGH);          // (HIGH = anti-clockwise / LOW = clockwise)
            digitalWrite(STEP, HIGH);
            delay(1);                         // Delay to slow down speed of Stepper
            digitalWrite(STEP, LOW);
            delay(1);
          }

          //*****************************
          while (!digitalRead(home_switch))   // Do this until the switch is not activated
          {
            digitalWrite(DIR, HIGH);
            digitalWrite(STEP, HIGH);
            delay(1);                         // More delay to slow even more while moving away from switch
            digitalWrite(STEP, LOW);
            delay(1);
            digitalWrite(Pin1, HIGH);         // locker & cutter
            delay(300);
            digitalWrite(Pin1, LOW);
            delay(300);
          }

          //*****************************
          if (digitalRead(IR))
          {
            digitalWrite(Pin2, HIGH);         // punch
            delay(600);
            digitalWrite(Pin3, HIGH);         // release
            delay(300);
            digitalWrite(Pin3, LOW);
            delay(100);
            digitalWrite(Pin2, LOW);
            delay(300);
          }

        } //END of   if ( processState == 1 )

        mSTATE = ScanSwitches;
      }
      break;

    //*****************************
    case ScanSwitches:
      {
        if (currentMillis - switchMillis >= 50)
        {
          //restart the TIMER
          switchMillis = currentMillis;

          //*****************************
          readState = digitalRead(4);

          if (pin4prevState != readState)
          {
            pin4prevState = readState;

            if (readState == LOW)     // button down with INPUT_PULLUP drains the pin to ground.
            {
              processState = 1;
            }
          }

          //*****************************
          readState = digitalRead(5); // note that the stop button is read last. press both makes stop

          if (pin5prevState != readState)
          {
            pin5prevState = readState;

            if (readState == LOW)     // button down with INPUT_PULLUP drains the pin to ground.
            {
              processState = 0;
            }
          }

        } //END of  if (currentMillis - switchMillis >= 50)

        mSTATE = Homing;
      }
      break;



  } //END of switch/case

} //END of stateMachine()

//************************************************************************************

EDIT:
Fixed a bug, Version 2.00

1 Like

Hi,
How have you got the IR sensor wired?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Do you have a DMM?

Have you tried code that JUST checks the IR and send message to IDE serial monitor?

Thanks.. Tom... :slight_smile:

Here is how IR attached.

Do you realize having delay(XXX); stops program execution for that period of time ?

Shifting relay 1 code under IR sensor.
Now when IR HIGH stepper starts rotating continuously. There is no effect of IR over relays.


//VERSION 2.00

#define STEP 11
#define DIR 10
#define Pin1 8 // Relay 1
#define Pin2 7 // Relay 2
#define Pin3 6 // Relay 3
#define IR 3
#define home_switch 2 // Pin 12 connected to Home Switch (MicroSwitch)

int direction; // Variable to set Rotation (CW-CCW) of the motor
int steps; // Used to set HOME position after Homing is completed
byte processState; // 0 is OFF, 1 is ON
byte pin4prevState;
byte pin5prevState;
byte readState;

unsigned long currentMillis;
unsigned long switchMillis;

enum STATES {Startup, Homing, ScanSwitches};

STATES mSTATE = Startup;

//************************************************************************************
void setup()
{
// Sets the two pins as Outputs
pinMode(STEP, OUTPUT);
pinMode(DIR, OUTPUT);
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);

pinMode(IR, INPUT_PULLUP);

digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, HIGH);

pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(home_switch, INPUT_PULLUP);

digitalWrite(STEP, HIGH); // Wake up EasyDriver
digitalWrite(DIR, HIGH);

delay(10); // Wait for EasyDriver wake up

} //END of setup()

//************************************************************************************
void loop()
{
currentMillis = millis();

stateMachine();

} //END of loop()

//************************************************************************************
void stateMachine()
{
switch (mSTATE)
{
//*****************************
case Startup:
{
// Start Homing procedure of Stepper Motor at startup
//*****************************
while (digitalRead(home_switch)) // Do this until the switch is activated
{
digitalWrite(DIR, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(STEP, HIGH);
delay(3); // Delay to slow down speed of Stepper
digitalWrite(STEP, LOW);
delay(3);
}

//*****************************
while (!digitalRead(home_switch)) // Do this until the switch is not activated
{
digitalWrite(DIR, HIGH);
digitalWrite(STEP, HIGH);
delay(3); // More delay to slow even more while moving away from switch
digitalWrite(STEP, LOW);
delay(3);
}

steps = 0; // Reset position variable to zero

mSTATE = ScanSwitches;
}
break;

//*****************************
case Homing:
{
if ( processState == 1 )
{
//*****************************
while (digitalRead(home_switch)) // Do this until the switch is activated
{
digitalWrite(DIR, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(STEP, HIGH);
delay(1); // Delay to slow down speed of Stepper
digitalWrite(STEP, LOW);
delay(1);
}

//*****************************
while (!digitalRead(home_switch)) // Do this until the switch is not activated
{
digitalWrite(DIR, HIGH);
digitalWrite(STEP, HIGH);
delay(1); // More delay to slow even more while moving away from switch
digitalWrite(STEP, LOW);
delay(1);

}

//*****************************
if (digitalRead(IR) == 1)
{
digitalWrite(Pin1, HIGH); // locker & cutter
delay(300);
digitalWrite(Pin1, LOW);
delay(300);
digitalWrite(Pin2, HIGH); // punch
delay(600);
digitalWrite(Pin3, HIGH); // release
delay(300);
digitalWrite(Pin3, LOW);
delay(100);
digitalWrite(Pin2, LOW);
delay(300);
}

} //END of if ( processState == 1 )

mSTATE = ScanSwitches;
}
break;

//*****************************
case ScanSwitches:
{
if (currentMillis - switchMillis >= 50)
{
//restart the TIMER
switchMillis = currentMillis;

//*****************************
readState = digitalRead(4);

if (pin4prevState != readState)
{
pin4prevState = readState;

if (readState == LOW) // button down with INPUT_PULLUP drains the pin to ground.
{
processState = 1;
}
}

//*****************************
readState = digitalRead(5); // note that the stop button is read last. press both makes stop

if (pin5prevState != readState)
{
pin5prevState = readState;

if (readState == LOW) // button down with INPUT_PULLUP drains the pin to ground.
{
processState = 0;
}
}

} //END of if (currentMillis - switchMillis >= 50)

mSTATE = Homing;
}
break;

} //END of switch/case

} //END of stateMachine()

//************************************************************************************

We have no idea what the relays do, how the motor is used, or anything about this project.

It’s up to you to supply good information and educate us on this project.

It would help if you would explain what this project is to doing (supposed to do)


For example:
When the start switch is pressed, a stepping motor constantly rotates.
The motor can be stopped with the stop switch.
Each revolution, a cam on the motor shaft cuts a piece of wire . . .
Attached is a full schematic of my circuit . . .


If we had a good description of how things are interconnected and what happens when something is energized etc., we could better help you.

  1. Power on Motor homing takes place. Proximity sensor attached to the motor shaft for position control.
  2. When push button 4 pressed cycle start.
    a. Motor rotates till the proximity sensor activates and stop.
    b. If IR sensor not activated nothing happens.
    c. if IR activated then all the relays go high and low.

all this in loop.

Now the situation is "enum STATES {Startup, Homing, ScanSwitches};"
The startup is ok.
Homing (Motor rotates and stops with all the relay goes high and low in sequence). when IR detects an obstacle, motor continuously running.

Homing (Motor rotates and stops with all the relay goes high and low in sequence). when IR detects an obstacle, motor continuously running.

We will assume motor continuously running is not what you wanted.


Try this.

//VERSION 3.00

#define enabled            true
#define disabled           false

#define STEP 11
#define DIR  10
#define Pin1 8          // Relay 1
#define Pin2 7          // Relay 2
#define Pin3 6          // Relay 3
#define IR   3
#define home_switch 2   // Pin 12 connected to Home Switch (MicroSwitch)

bool IRflag = disabled; //when enabled, we can check the IR sensor

int direction;          // Variable to set Rotation (CW-CCW) of the motor
int steps;              // Used to set HOME position after Homing is completed

byte processState;      // 0 is OFF, 1 is ON
byte pin4prevState;
byte pin5prevState;
byte readState;

unsigned long currentMillis;
unsigned long switchMillis;

enum STATES {Startup, Homing, IRstate, ScanSwitches};

STATES mSTATE = Startup;

//************************************************************************************
void setup()
{
  // Sets the two pins as Outputs
  pinMode(STEP, OUTPUT);
  pinMode(DIR,  OUTPUT);
  pinMode(Pin1, OUTPUT);
  pinMode(Pin2, OUTPUT);
  pinMode(Pin3, OUTPUT);

  pinMode(IR, INPUT_PULLUP);

  digitalWrite(Pin1, HIGH);
  digitalWrite(Pin2, HIGH);
  digitalWrite(Pin3, HIGH);

  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(home_switch, INPUT_PULLUP);

  digitalWrite(STEP, HIGH);           // Wake up EasyDriver
  digitalWrite(DIR, HIGH);

  delay(10);                          // Wait for EasyDriver wake up

} //END of setup()

//************************************************************************************
void loop()
{
  currentMillis = millis();

  stateMachine();

} //END of loop()

//************************************************************************************
void stateMachine()
{
  switch (mSTATE)
  {
    //*****************************
    case Startup:
      {
        // Start Homing procedure of Stepper Motor at startup
        //*****************************
        while (digitalRead(home_switch))    // Do this until the switch is activated
        {
          digitalWrite(DIR, HIGH);          // (HIGH = anti-clockwise / LOW = clockwise)
          digitalWrite(STEP, HIGH);
          delay(3);                         // Delay to slow down speed of Stepper
          digitalWrite(STEP, LOW);
          delay(3);
        }

        //*****************************
        while (!digitalRead(home_switch))   // Do this until the switch is not activated
        {
          digitalWrite(DIR, HIGH);
          digitalWrite(STEP, HIGH);
          delay(3);                         // More delay to slow even more while moving away from switch
          digitalWrite(STEP, LOW);
          delay(3);
        }

        steps = 0;                          // Reset position variable to zero

        mSTATE = ScanSwitches;
      }
      break;

    //*****************************
    case Homing:
      {
        if ( processState == 1 )
        {
          //*****************************
          while (digitalRead(home_switch))    // Do this until the switch is activated
          {
            digitalWrite(DIR, HIGH);          // (HIGH = anti-clockwise / LOW = clockwise)
            digitalWrite(STEP, HIGH);
            delay(1);                         // Delay to slow down speed of Stepper
            digitalWrite(STEP, LOW);
            delay(1);
          }

          //*****************************
          while (!digitalRead(home_switch))   // Do this until the switch is not activated
          {
            digitalWrite(DIR, HIGH);
            digitalWrite(STEP, HIGH);
            delay(1);                         // More delay to slow even more while moving away from switch
            digitalWrite(STEP, LOW);
            delay(1);
            digitalWrite(Pin1, HIGH);         // locker & cutter
            delay(300);
            digitalWrite(Pin1, LOW);
            delay(300);
          }

          //the motor has rotated to the proximity sensor and stopped

          //we can now check the IR sensor
          IRflag = enabled;

          //check the IR sensor
          mSTATE = IRstate;

        } //END of   if ( processState == 1 )

        else
        {
          //processState == 1      was false

          //check the switches again
          mSTATE = ScanSwitches;
        }
      }
      break;

    //*****************************
    case IRstate:
      {
        //when you get here, IRflag will be enabled

        //*****************************
        if (digitalRead(IR))
        {
          digitalWrite(Pin2, HIGH);         // punch
          delay(600);
          digitalWrite(Pin3, HIGH);         // release
          delay(300);
          digitalWrite(Pin3, LOW);
          delay(100);
          digitalWrite(Pin2, LOW);
          delay(300);
        }

        //check the switches again
        mSTATE = ScanSwitches;
      }
      break;

    //*****************************
    case ScanSwitches:
      {
        if (currentMillis - switchMillis >= 50)
        {
          //restart the TIMER
          switchMillis = currentMillis;

          //*****************************     >>>>---->   s w i t c h   4
          readState = digitalRead(4);

          if (pin4prevState != readState)
          {
            pin4prevState = readState;

            if (readState == LOW)     // button down with INPUT_PULLUP drains the pin to ground.
            {
              processState = 1;
            }
          } //END of this switch

          //*****************************     >>>>---->   s w i t c h   5
          readState = digitalRead(5); // note that the stop button is read last. press both makes stop

          if (pin5prevState != readState)
          {
            pin5prevState = readState;

            if (readState == LOW)     // button down with INPUT_PULLUP drains the pin to ground.
            {
              processState = 0;

              //cancel IR sensor checking
              IRflag = disabled;
            }
          } //END of this switch

        } //END of  if (currentMillis - switchMillis >= 50)

        //decide where to go now
        if (IRflag == enabled)
        {
          mSTATE = IRstate;
        }

        else
        {
          mSTATE = Homing;
        }
      }
      break;

  } //END of switch/case

} //END of stateMachine()

//************************************************************************************

Thanks for your continous support.
I have changed the section of IR and it works as I want.
But now stepper not working. As the IR section completes Stepper should run again.
Stepper runs, IR section again and again.

Sorry if I still don't convey my code properly.

//VERSION 3.00

#define enabled            true
#define disabled           false

#define STEP 11
#define DIR  10
#define Pin1 8          // Relay 1
#define Pin2 7          // Relay 2
#define Pin3 6          // Relay 3
#define IR   3
#define home_switch 2   // Pin 12 connected to Home Switch (MicroSwitch)

bool IRflag = disabled; //when enabled, we can check the IR sensor

int direction;          // Variable to set Rotation (CW-CCW) of the motor
int steps;              // Used to set HOME position after Homing is completed

byte processState;      // 0 is OFF, 1 is ON
byte pin4prevState;
byte pin5prevState;
byte readState;

unsigned long currentMillis;
unsigned long switchMillis;

enum STATES {Startup, Homing, IRstate, ScanSwitches};

STATES mSTATE = Startup;

//************************************************************************************
void setup()
{
  // Sets the two pins as Outputs
  pinMode(STEP, OUTPUT);
  pinMode(DIR,  OUTPUT);
  pinMode(Pin1, OUTPUT);
  pinMode(Pin2, OUTPUT);
  pinMode(Pin3, OUTPUT);

  pinMode(IR, INPUT_PULLUP);

  digitalWrite(Pin1, HIGH);
  digitalWrite(Pin2, HIGH);
  digitalWrite(Pin3, HIGH);

  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(home_switch, INPUT_PULLUP);

  digitalWrite(STEP, HIGH);           // Wake up EasyDriver
  digitalWrite(DIR, HIGH);

  delay(10);                          // Wait for EasyDriver wake up

} //END of setup()

//************************************************************************************
void loop()
{
  currentMillis = millis();

  stateMachine();

} //END of loop()

//************************************************************************************
void stateMachine()
{
  switch (mSTATE)
  {
    //*****************************
    case Startup:
      {
        // Start Homing procedure of Stepper Motor at startup
        //*****************************
        while (digitalRead(home_switch))    // Do this until the switch is activated
        {
          digitalWrite(DIR, HIGH);          // (HIGH = anti-clockwise / LOW = clockwise)
          digitalWrite(STEP, HIGH);
          delay(3);                         // Delay to slow down speed of Stepper
          digitalWrite(STEP, LOW);
          delay(3);
        }

        //*****************************
        while (!digitalRead(home_switch))   // Do this until the switch is not activated
        {
          digitalWrite(DIR, HIGH);
          digitalWrite(STEP, HIGH);
          delay(3);                         // More delay to slow even more while moving away from switch
          digitalWrite(STEP, LOW);
          delay(3);
        }

        steps = 0;                          // Reset position variable to zero

        mSTATE = ScanSwitches;
      }
      break;

    //*****************************
    case Homing:
      {
        if ( processState == 1 )
        {
          //*****************************
          while (digitalRead(home_switch))    // Do this until the switch is activated
          {
            digitalWrite(DIR, HIGH);          // (HIGH = anti-clockwise / LOW = clockwise)
            digitalWrite(STEP, HIGH);
            delay(1);                         // Delay to slow down speed of Stepper
            digitalWrite(STEP, LOW);
            delay(1);
          }

          //*****************************
          while (!digitalRead(home_switch))   // Do this until the switch is not activated
          {
            digitalWrite(DIR, HIGH);
            digitalWrite(STEP, HIGH);
            delay(1);                         // More delay to slow even more while moving away from switch
            digitalWrite(STEP, LOW);
            delay(1);

          }

          //the motor has rotated to the proximity sensor and stopped

          //we can now check the IR sensor
          IRflag = enabled;

          //check the IR sensor
          mSTATE = IRstate;

        } //END of   if ( processState == 1 )

        else
        {
          //processState == 1      was false

          //check the switches again
          mSTATE = ScanSwitches;
        }
      }
      break;

    //*****************************
    case IRstate:
      {
        //when you get here, IRflag will be enabled

        //*****************************
        if (digitalRead(IR) == 1)
        {

        }
        else
        {
          digitalWrite(Pin1, HIGH);         // locker & cutter
          delay(300);
          digitalWrite(Pin1, LOW);
          delay(300);
          digitalWrite(Pin2, HIGH);         // punch
          delay(600);
          digitalWrite(Pin3, HIGH);         // release
          delay(300);
          digitalWrite(Pin3, LOW);
          delay(100);
          digitalWrite(Pin2, LOW);
          delay(300);
        }

        //check the switches again
        mSTATE = ScanSwitches;
      }
      break;

    //*****************************
    case ScanSwitches:
      {
        if (currentMillis - switchMillis >= 50)
        {
          //restart the TIMER
          switchMillis = currentMillis;

          //*****************************     >>>>---->   s w i t c h   4
          readState = digitalRead(4);

          if (pin4prevState != readState)
          {
            pin4prevState = readState;

            if (readState == LOW)     // button down with INPUT_PULLUP drains the pin to ground.
            {
              processState = 1;
            }
          } //END of this switch

          //*****************************     >>>>---->   s w i t c h   5
          readState = digitalRead(5); // note that the stop button is read last. press both makes stop

          if (pin5prevState != readState)
          {
            pin5prevState = readState;

            if (readState == LOW)     // button down with INPUT_PULLUP drains the pin to ground.
            {
              processState = 0;

              //cancel IR sensor checking
              IRflag = disabled;
            }
          } //END of this switch

        } //END of  if (currentMillis - switchMillis >= 50)

        //decide where to go now
        if (IRflag == enabled)
        {
          mSTATE = IRstate;
        }

        else
        {
          mSTATE = Homing;
        }
      }
      break;

  } //END of switch/case

} //END of stateMachine()

//************************************************************************************
  1. Power on Motor homing takes place. Proximity sensor attached to the motor shaft for position control.

  2. When push button 4 pressed cycle start.

a. Motor rotates till the proximity sensor activates and stop.
b. If IR sensor not activated nothing happens.
c. if IR activated then all the relays go high and low.

This completed cycle.
Loop starts again without pressing any button.
Motor start, check IR, relay on/off.
All this I want again and again.

Is this drawing exactly the way things are to work ?

If not, erase and add arrows as needed.