0

The following is the code that I am trying but the problem is steppers rotate one after each other. However, I want to rotate steppers simultaneously.
This code for two stepper motors.

int pulse=3;

int direc=4;

int pulse1=5;

int direc1=6;

int i,j,k;

float angle[5][3]={{360,360,3},{7,8,9},{11,12,13},{14,15,16},{17,18,19}};

float steps;

void motor1(){

  for(k = 0; k<=4; k++){

    Serial.println(angle[k][0]);

    steps = (1600*angle[k][0])/360;

    Serial.println(steps);

    for(j = 0; j<=steps;j++){

      digitalWrite(3,HIGH);

      delayMicroseconds(100);

      digitalWrite(3,LOW);

      delayMicroseconds(100);
        }

}

}

void motor2(){

  for(k = 0; k<=4; k++){

    Serial.println(angle[k][1]);

    steps = (1600*angle[k][1])/360;

    Serial.println(steps);

    for(j = 0; j<=steps;j++){

      digitalWrite(5,HIGH);

      delayMicroseconds(100);

      digitalWrite(5,LOW);

      delayMicroseconds(100);

        }

}

}

void setup() {
  Serial.begin(9600);

  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);    
}

void loop(){
    motor1();

    motor2();

   //while(1);   
}
3

(Half a year ago, but what the hey...)

Your motor1() and motor2() are currently written as:

do until done:
  take a step
  wait
end

, so yeah, one finishes before the next one even starts.

Each motor function needs to do:

if motor's current-position is not the final position,
   if it is time to take a step,
      take a step
      calc when to take another step
      calc motor's new current-position
   end
end

Now call your motor functions as frequently as possible. Notice that either if test can cause the function to return without doing anything - that is intentional.

This is non-blocking programming - coding in such a way that nothing keeps control while waiting for something to happen (clock ticks, in this case), it just does something, or does nothing, right now, if there is or is not something to do right now, but it does release the processor (the function returns immediately) whether it did something or not.

I call these functions "maybe-do" functions:

  • maybe step a motor (if, and only if, a certain interval has passed, step the motor);

  • maybe light an LED (if, and only if, the button is being pressed and it wasn't pressed the last time we looked, turn on the LED;

  • maybe read the terminal keyboard (if there is at least one character in the Serial input buffer, collect a character).

Then your loop() function should do nothing but call each maybe-do function, in sequence, as fast as possible. And it follows from the above that your maybe-do functions need to execute as quickly as possible and do no more than is necessary to (1) decide whether to act, and if so, (2) to carry out its action, and (3) in either case, return immediately.


And - Bang-Boom! - right there is a half-semester course in block-free programming without interrupts!

| improve this answer | |
  • This is a valuable reference answer, that could be useful for sooo many questions. – Edgar Bonet Jun 18 '19 at 8:47
0

I can't understand clearly what you want, but maybe you want to drive your two servos simultaneously. To do this, after you determine the angle of servos, you can drive them without delay between their commands.

Like you use above, you can't drive them at the same time, there are loops and they have to be completed before the next instruction is run so your servos wait each other.

Here is a good example code for you:

#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo

float angle[5][3]={{360,360,3},{7,8,9},{11,12,13},{14,15,16},{17,18,19}};

void setup() {
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);  // attaches the servo on pin 10 to the servo object
}

void loop() {
  for(k = 0; k<=4; k++){
    pos = (1600*angle[k][0])/360; //Calculate the angle which you want

    //The most impportant part, there shouldn't be a delay between them
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'    
    myservo2.write(pos);              // tell servo to go to position in variable 'pos'

    delay(1000);                       // waits 1000ms for the servo to reach the position
  }
}
| improve this answer | |
  • Thank you sir for your answer but i want to rotate motor 1 for this loop, pos = (1600*angle[k][0])/360; and motor 2 for this loop pos = (1600*angle[k][1])/360; – Akshay Dalvi Jan 19 '19 at 9:21
  • Then, add pos2= (1600*angle[k][1])/360 below pos = (1600*angle[k][0])/360. And, change the myservo2 command to myservo2.write(pos2). So your first motor rotates x degree while your second motor rotates y degree without any delay. – Faruk ÜNAL Jan 19 '19 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.