Stepping Motor ClockWise and CounterClockWise direction

Hi,
I am a newbie or a beginner in Arduino programming and for the code below I am using an Arduino UNO and 28BYJ-48 motor and a ULN2003 driver board for a project that I am interested in. The code is given below.

const int CW =1;
const int CCW =2;
const int STOP =3;

int Pin1 = 8;//IN1 is connected to 8 
int Pin2 = 9;//IN2 is connected to 9  
int Pin3 = 10;//IN3 is connected to 10 
int Pin4 = 11;//IN4 is connected to 11
int switchSTOP =2;//define input pin for STOP push button
int stopType=2;//1=normal stop, 2=hold stop (consumes power)

int speedFactor =1;//1=fastest, 2=slower or 3 more slower

long angles[] = {90,180,270};//angles of each push button
int pushButtons[] ={3, 4, 5};//digial pin for each push button
int directions[] ={CCW, CCW, CCW};//direction of eacch push button
int speedFactors[] = {1, 1, 1};//speed for each push button


int correction_CW = 150;//watch video for details
int correction_CCW = 150;//watch video for details


int poleStep = 0; 
long stepVale =0;
const int SPR=64*64;
long goToAngle=0;
int activeButton=0;
int pole1[] ={0,0,0,0, 0,1,1,1, 0};//pole1, 8 step values
int pole2[] ={0,0,0,1, 1,1,0,0, 0};//pole2, 8 step values
int pole3[] ={0,1,1,1, 0,0,0,0, 0};//pole3, 8 step values
int pole4[] ={1,1,0,0, 0,0,0,1, 0};//pole4, 8 step values





int count=0;
int  dirStatus = STOP;// stores direction status 3= stop (do not change)

void setup() 
{ 
  //Robojax.com Stepper Push button Any Angle STPB-5
  Serial.begin(9600);
  Serial.begin("Robojax Video for Stepper Motor STPB-2");  
 pinMode(Pin1, OUTPUT);//define pin for ULN2003 in1 
 pinMode(Pin2, OUTPUT);//define pin for ULN2003 in2   
 pinMode(Pin3, OUTPUT);//define pin for ULN2003 in3   
 pinMode(Pin4, OUTPUT);//define pin for ULN2003 in4   

 pinMode(switchSTOP,INPUT_PULLUP); 
 attachInterrupt(digitalPinToInterrupt(switchSTOP), stopMotor, FALLING );
 // see this https://www.arduino.cc/reference/en/language/variables/utilities/sizeof/
 for (byte i = 0; i < (sizeof(pushButtons) / sizeof(pushButtons[0])); i++) {
   pinMode(i,INPUT_PULLUP);
  }


 
} //setup
 void loop() 
{ 
    stepVale = (SPR * goToAngle)/360 ;
  //Robojax.com Stepper Push button Any Angle STPB-5

  //see this https://www.arduino.cc/reference/en/language/variables/utilities/sizeof/
 for (byte i = 0; i < (sizeof(angles) / sizeof(angles[0])); i++) {
    if(digitalRead(pushButtons[i]) == LOW) 
    {
      goToAngle =angles[i];
      dirStatus =directions[i];
      count =0;
      activeButton =i;
    }// if ends
 }//for loop ends
  
  
 if(dirStatus ==CCW){ 
  
   poleStep++; 
   count++;   
   if(count+correction_CCW <= stepVale)
   {
    driveStepper(poleStep);      
   }else{
      stopMotor();  
   }
  //full explannation at Arduino Course on Udemy.com see link above
 }else if(dirStatus ==CW){ 
   poleStep--; 
   count++;   
   if(count+correction_CW <=stepVale)
   {
    driveStepper(poleStep);      
   }else{
      stopMotor();  
   }   
 }else{
  stopMotor();   
 }
 if(poleStep>7){ 
   poleStep=0;
 } 
 if(poleStep<0){ 
   poleStep=7; 
 } 
  delay(speedFactors[activeButton]);
  
  //Robojax.com Stepper Push button Any Angle STPB-5

}// loop


/*
 * @brief moves motor to specific angle 
 * @param "angle" is integer representing the angle
 * @return does not return anything
 * 
 * www.Robojax.com code Ap1il 19 2020 at 01:22 in Ajax, Ontario, Canada
 */
void driveStepper(int c)
{
    //Robojax.com Stepper Push button Any Angle STPB-5
     digitalWrite(Pin1, pole1[c]);  
     digitalWrite(Pin2, pole2[c]); 
     digitalWrite(Pin3, pole3[c]); 
     digitalWrite(Pin4, pole4[c]);

}//driveStepper ends here

/*
 * @brief stops the motor immediately
 * @param none
 * @return does not return anything
 * 
 * www.Robojax.com code June 09, 2020 at 11:09 in Ajax, Ontario, Canada
 */
void stopMotor()
{
  //see this https://www.arduino.cc/reference/en/language/variables/utilities/sizeof/
 for (byte i = 0; i < (sizeof(angles) / sizeof(angles[0])); i++) {
    digitalWrite(pushButtons[i], HIGH); 

 }//for loop ends
 dirStatus = STOP;
 if( stopType ==2)
 {
  driveStepper(8);
 }
}//stopMotor()

The code above helps the stepper motor to go to any specified angle in two directions. One is in Clockwise Direction and the other is CounterClockWise direction, you can find the abbreviation in the code as CW and CCW respectively.
The problem is that I want my code to go to the specified angle in the CW direction and come back to the initial position in the CCW direction with a help of a push-buttons.

For example, I want the stepper motor to go to 90 degrees in CW direction and then CCW back which means back to 0 degrees(initial position).

The problem here is that each push-button will only execute in CW or CCW but I want one push button to execute both CW and CCW direction and it should come back to its initial position after the motor has rotated to the specified angle.

For reference, you can use this link.(click here)

I personally think that changing of CW and CCW direction is possible in this segment of the above code.

if(dirStatus ==CCW){ 
  
   poleStep++; 
   count++;   
   if(count+correction_CCW <= stepVale)
   {
    driveStepper(poleStep);      
   }else{
      stopMotor();  
   }
  //full explannation at Arduino Course on Udemy.com see link above
 }else if(dirStatus ==CW){ 
   poleStep--; 
   count++;   
   if(count+correction_CW <=stepVale)
   {
    driveStepper(poleStep);      
   }else{
      stopMotor();  
   }   
 }else{
  stopMotor();   
 }

I will also attach new.ino file if there are any issues in copy-pasting from the post itself.

Any help is much appreciated.

new.ino (3.93 KB)

lyric1234:
The problem is that I want my code to go to the specified angle in the CW direction and come back to the initial position in the CCW direction with a help of a push-buttons.

Do you mean that when you press the button the first time the motor should move 90° CW followed immediately by 90° CCW (back to the original position). And when you press the same button a second time it should rotate CCW followed by CW?

Your program seems to be written for 3 buttons - what does each of them do now?

...R

I see that if you detect a button press you get dirStatus from your directions array. Once you have got it, change the value in the directions array to the opposite so it will be used the next time you press that button.

Robin2:
Do you mean that when you press the button the first time the motor should move 90° CW followed immediately by 90° CCW (back to the original position). And when you press the same button a second time it should rotate CCW followed by CW?

Your program seems to be written for 3 buttons - what does each of them do now?

...R

Hi Robin 2
What i meant was that when I click on the 1st button once it should go from 0 to 90 degree and then wait for 2 seconds and come back to 0 degree(original position).
Pressing the same button twice is not really important to me.
The reason why I need three push button is because I need the motor to turn in three angles 90, 180 , 270 degrees respectively.
So if the motor can go from 0 to 90 degree and then wait for 2 seconds and then back to it's original position then it should respectively work for 180 degree and 270 degree.

wildbill:
I see that if you detect a button press you get dirStatus from your directions array. Once you have got it, change the value in the directions array to the opposite so it will be used the next time you press that button.

Yes this is correct and I have already done it wildbil.
But what I need is just one push button to go from 0 to 90 degree a(CW)then wait for 2 seconds and then go back to it's initial position(in CCW).

lyric1234:
What i meant was that when I click on the 1st button once it should go from 0 to 90 degree and then wait for 2 seconds and come back to 0 degree(original position).

Thanks. That makes things much clearer. But it reminds me of the Irishman who was asked how to get to Dublin and said "well I wouldn't start from here".

It really would be easier to start with a completely new program and use the Stepper library to control the motor.

Then if you know how many steps are required to go 90° the code could be a simple as

myStepper.step(numberOfSteps);
delay(2000);
myStepper.step(- numberOfSteps);

I'm assuming that you don't need anything else to happen while the motor is moving - if you do then the AccelStepper library would be more appropriate.

...R

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.