#include <AccelStepper.h>
#define HALFSTEP 8
boolean start=true;
volatile boolean e_stop=false; //initialization for emergency button
int IRcw=8; //IR sensor clockwise
int IRccw=9; //IR sensor counter clockwise
int switchUP=20;
int switchDOWN=21;
// Motor pin definitions
#define motorPin1 3 // IN1 on the ULN2003 driver 1
#define motorPin2 4 // IN2 on the ULN2003 driver 1
#define motorPin3 5 // IN3 on the ULN2003 driver 1
#define motorPin4 6 // IN4 on the ULN2003 driver 1
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper(HALFSTEP, motorPin1, motorPin2, motorPin3, motorPin4);
void setup()
{
pinMode(IRcw,INPUT);
pinMode(IRccw,INPUT);
pinMode(switchUP,INPUT);
pinMode(switchDOWN,INPUT);
Serial.begin(9600);
attachInterrupt(0,e_stop_ISR,RISING);
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(400.0);
stepper.setSpeed(500);
}
//--(end setup )---
void loop()
{
int valueCW=digitalRead(IRcw); //read value from IR sensor HIGH or LOW
int valueCCW=digitalRead(IRccw); //read value from IR sensor HIGH or LOW
int valueSwitchUP=digitalRead(switchUP);
int valueSwitchDOWN=digitalRead(switchDOWN);
if(start==true)
{
if(e_stop==false)
{
if (valueCW==LOW && stepper.distanceToGo() >= 0 && ) //stepper.distanceToGo() for as long stepper did not reaches target position it cannot rotate to other direction
{
stepper.move(8000);
}
else if(valueCCW==LOW && stepper.distanceToGo() <= 0)
{
stepper.move(-8000);
}
else if(valueSwitchUP==HIGH)
{
//HERE I WANT TO MAKE IT ROTATE CLOCKWISE INFINITELY AS UP SWITCH IS ON
}
else if(valueSwitchDOWN==HIGH)
{
//HERE I WANT TO MAKE IT ROTATE COUNTER CLOCKWISE INFINITELY AS DOWN SWITCH IS ON
}
stepper.run();
}
else
{
start=false;
}
}
}
//--(end loop)--
void e_stop_ISR(void)
{
detachInterrupt(0);
e_stop=!e_stop;
}