Single stepping of stepper motor_coding problem

Hi,

I am trying to run a stepper motor with its compatible driver. Simply sending a series of pulses, moves the motor.
A pulse train output causes continuous rotation and single pulse causes the stepper to move one step (Jogging).

With the attached code I am able to make continuous rotation but not jogging.

Speed setting is done by analog input on pin A0.

What changes are required to make to able to jog also?

Thanks.
Arihant

Oct19_1100hrs.ino (1.37 KB)

Please include short programs in your Post so we don't have to download them. I will do it for you this time

bool mode = LOW;       // variable for jog or continuous motion
bool dir = LOW;        // variable for motion direction
double speed = 0;
int step = 0;

void setup() {

  pinMode(6, INPUT);    // jog or continuous selection. HIGH is continuous
  pinMode(7, INPUT);    // clockwise or ANTICLOCKWISE rotation
  pinMode(8, OUTPUT);   // clockwise pulse output
  pinMode(9, OUTPUT);   // anticlockwise pulse output
  
  mode = digitalRead(6);      // read mode input
  dir = digitalRead(7);       // read direction input
  speed = 0.1*analogRead(A0); // POT is connected to this pin to adjust speed

}

void loop() {

    
  if(mode == LOW && dir == LOW)  //jog clockwise
  {
    
    while (step<1)
    {
    digitalWrite(8, HIGH);
    delay(speed);
    digitalWrite(8, LOW);
    delay(speed);
    step=step+1;
    }
  }
  else if(mode == LOW && dir == HIGH)  //jog anticlockwise
  {
    while (step<1)
    {
    digitalWrite(9, HIGH);
    delay(speed);
    digitalWrite(9, LOW);
    delay(speed);
    step=step+1;
    }
  }
  else if (mode == HIGH && dir == LOW)    // continuous CW
  {
    digitalWrite(8, HIGH);
    delay(speed);
    digitalWrite(8, LOW);
    delay(speed);
  }
  else if (mode == HIGH && dir == HIGH)   // cont CCW
  {
    digitalWrite(9, HIGH);
    delay(speed);
    digitalWrite(9, LOW);
    delay(speed);
  }
  else
  {
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
  }
    

}

...R

In that program the mode and the speed are only read once in setup(). And you may not be quick enough to press the button when the program starts.

It would be more usual to read the buttons and the potentiometer regularly in loop(). And that is essential if you want to jog more than one single step. Have a look at Planning and Implementing a Program

You should also note how my demo code uses millis() to manage timing without blocking. The Arduino can do nothing during a delay() which makes for an unresponsive program.

...R

I have made changes as suggested but it still won't work.
I am a bit inexperienced in programming. May be a little clue I am missing.

bool mode = LOW;       // variable for jog or continuous motion
bool dir = LOW;        // variable for motion direction
int speed = 0;
int step = 0;

void setup() {

 pinMode(6, INPUT);    // jog or continuous selection. HIGH is continuous
 pinMode(7, INPUT);    // clockwise or ANTICLOCKWISE rotation
 pinMode(8, OUTPUT);   // clockwise pulse output
 pinMode(9, OUTPUT);   // anticlockwise pulse output
 }

void loop() {

 mode = digitalRead(6);      // read mode input
 dir = digitalRead(7);       // read direction input
 speed = 0.1*analogRead(A0); // POT is connected to this pin to adjust speed 
 if(mode == LOW && dir == LOW)  //jog clockwise
 {
   
   do
   {
   digitalWrite(8, HIGH);
   delay(speed);
   digitalWrite(8, LOW);
   delay(speed);
   step=step+1;
   }while(step <1);
 }
 else if(mode == LOW && dir == HIGH)  //jog anticlockwise
 {
   do
   {
   digitalWrite(9, HIGH);
   delay(speed);
   digitalWrite(9, LOW);
   delay(speed);
   step=step+1;
   }while(step <1);
 }
 else if (mode == HIGH && dir == LOW)    // continuous CW
 {
   digitalWrite(8, HIGH);
   delay(speed);
   digitalWrite(8, LOW);
   delay(speed);
 }
 else if (mode == HIGH && dir == HIGH)   // cont CCW
 {
   digitalWrite(9, HIGH);
   delay(speed);
   digitalWrite(9, LOW);
   delay(speed);
 }
 else
 {
   digitalWrite(8, LOW);
   digitalWrite(9, LOW);
 }
   

}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

It should look like the code in Reply #1

...R

arihant122:
I have made changes as suggested but it still won't work.

You need to tell us what is actually does. "Won't work" is not helpful for debugging.

Why do you need a DO/WHILE for a single step? Just use IF.

Using floating point for the speed variable is a waste of time. The delay() function takes an integer as its paramter. Only use floating point maths on a microprocessor if it is unavoidable - it is very slow.

Put in some Serial.print() statements so you can see what the code is doing - what is the value of mode, or speed, and what part of the program is being activated.

For anything beyond a trivial program it is better to give names to pins to make the code clearer. For example

byte modePin = 6;

mode = digitalRead(modePin);

...R

Hmm...

Looks like I have a lot of learning to do.
:relaxed:
I will implement your suggestions and try to become a better programmer and forum contributor.

Thanks.