Making stepper motor go forward and backwards.

Hi everyone.

Im new to this site and also new when it comes to stepper motors and coding.

A friend of mine wants to use a stepping motor that can push the button of a ps4 controller, reverse, 5 sec pause and repeat.

I helped him build the stepper motor. He has a Arduino UNO, ULN2003 driving board and a 12v motor.

I have found out how to delay (delay(2000);), make it go 360 degrees (int Steps = 4096) and the speed of the motor.

What we cant find out is the reverse, delay and repeat,

Here is the code.

I appreciate all help! Thank you :slight_smile:

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11
int Steps = 4096; //4096 or 768
int cstep = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(IN1, OUTPUT); 
  pinMode(IN2, OUTPUT); 
  pinMode(IN3, OUTPUT); 
  pinMode(IN4, OUTPUT); 
}

void loop()
{
  for(int x=0;x<Steps;x++)
  {
  step1();
  //delay(1);
  delayMicroseconds(4500);
  }
  Serial.println("Boom!!");
  delay(2000);
  
}

void step1()
{
  //stepp
  switch(cstep)
  {
   case 0:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break; 
   case 1:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, HIGH);
   break; 
   case 2:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break; 
   case 3:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break; 
   case 4:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
   case 5:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
     case 6:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
   case 7:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break; 
   default:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
  }
   
   cstep=cstep+1;
   if(cstep==8)
     {cstep=0;}
}

This makes it go forward

  cstep=cstep+1;
   if(cstep==8) {
       cstep=0;
   }

This will make it go backwards

  cstep = cstep - 1;
   if(cstep  <  0) {
       cstep = 7;
   }

...R

Robin2:
This makes it go forward

  cstep=cstep+1;

if(cstep==8) {
      cstep=0;
  }




This will make it go backwards


cstep = cstep - 1;
  if(cstep  <  0) {
      cstep = 7;
  }




...R

Thank you. But i have to be honest. I just dont know how to, i try, but still get an error.
Maybe you cant put it in the code i posted? Sorry for being such a newbie.

alexstavik:
Thank you. But i have to be honest. I just dont know how to, i try, but still get an error.
Maybe you cant put it in the code i posted? Sorry for being such a newbie.

Please post the program that represents your best attempt. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

At it's simplest you could replace your piece of code with mine and see what happens.

...R

Robin2:
Please post the program that represents your best attempt. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

At it's simplest you could replace your piece of code with mine and see what happens.

...R

Full rotation is 4096. I have put it to 1/4 of that (1024).
Set each movement to 5 seconds (moves clockwise for 5 sec, then stops).
5 second delay. Then repeat what i written above.

I cant get it to stop, go counter clockwise -1024, 5 sec pause and then repeat. Because i am a newbie. I can only get it to go 360 degrees, clockwise, with delays.

Tried a lot, read a lot. But i just dont know how. So this was my last try.

Best attempt. Using Arduino 1.8.9

Thank you for trying to help me.

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11
int Steps = 1024; //4096 or 768
int cstep = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(IN1, OUTPUT); 
  pinMode(IN2, OUTPUT); 
  pinMode(IN3, OUTPUT); 
  pinMode(IN4, OUTPUT); 
}

void loop()
{
  for(int x=0;x<Steps;x++)
  {
  step1();
  //delay(1);
  delayMicroseconds(5000);
  }
  Serial.println("Boom!!");
  delay(5000);
  
}

void step1()
{
  //stepp
  switch(cstep)
  {
   case 0:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break; 
   case 1:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, HIGH);
   break; 
   case 2:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break; 
   case 3:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break; 
   case 4:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
   case 5:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
     case 6:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
   case 7:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break; 
   default:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
  }
   
   cstep=cstep+1;
   if(cstep==8)
     {cstep=0;}
}[code]

Do you understand how to use "cut and paste"? If so, replace this in your code:

   cstep=cstep+1;
   if(cstep==8)
     {cstep=0;}

with this:

  cstep = cstep - 1;
   if(cstep  <  0) {
       cstep = 7;
   }

It cannot be made any simpler.