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 {
//
}
}

