Pages: [1]   Go Down
Author Topic: 2 stepper motors running at the same time ?library  (Read 446 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am using 2 easydrivers( using 2 pins, one for direction and the other for the number of steps), and i am trying to build a library which allow you to make the motor run at the same time with different speed, but i 
couldn't manage how to ignore the delay() between 2 steps in the main loop()  ?? for now only one stepper is moving and then the other one start. it's my first library.
a little help would be more than welcome.


here is the library

 
Code:

#ifndef Mot_h
#define Mot_h


#include "WProgram.h"


class Mot
{
  
public:
    Mot(int pindir, int pinstep);

   void move(int step,int del, boolean dir);//del=delay();

      

  private:
  


  int dirpin;
    int steppin;
  int step;
int del;
 boolean dir;
};


#endif

//////////////////////////////////////////////////////////////////////////////
#include "WProgram.h"
#include "Mot.h"

Mot::Mot(int pindir, int pinstep)
{
  pinMode(pindir, OUTPUT);
  pinMode(pinstep, OUTPUT);
  
dirpin = pindir;
steppin=pinstep;
}


void Mot::move(int step,int del, boolean dir)
{
int i;

if(dir==false){
digitalWrite(dirpin,HIGH);
for(i=0;i<step;i++){
  digitalWrite(steppin, HIGH);
delayMicroseconds(del);  
  digitalWrite(steppin, LOW);
delayMicroseconds(del);  
}
}
if(dir==true){
digitalWrite(dirpin,LOW);

for(i=0;i<step;i++){
  digitalWrite(steppin, HIGH);
delayMicroseconds(del);  
  digitalWrite(steppin, LOW);
delayMicroseconds(del);  
}
}

}



here's the arduino code

Code:
#include <Mot.h>
Mot motx=Mot(2,3);
Mot moty=Mot(5,6);
int deltaxneg=0;
int deltayneg;
int mess [15];
int deltax=0;
int deltay=0;
int delx=0;
int dely=0;
int dess=0;
boolean dirx;
boolean diry;
int pindir=2;
int pinstep=3;

void setup() {
 Serial.begin(115200); // Initiate Serial Communication
pinMode(pindir,OUTPUT);
pinMode(pinstep,OUTPUT);
establishContact();

}
void loop() {
int i;
int j;
int r;
  if ( Serial.available() ) {

   for (i=0;i<15;i++){
   mess[i]=0;
  
 mess [i]=Serial.read()-48;
delayMicroseconds(100);

   }
  
   deltax=mess[0]*100+mess[1]*10+ mess[2] ;  
   deltay=mess[3]*100+mess[4]*10+ mess[5] ;  
   delx=mess[6]*1000+mess[7]*100+ mess[8]*10+mess[9]  ;  
   dely=mess[10]*1000+mess[11]*100+ mess[12]*10+mess[13] ;
   dess=mess[14];
  
    if((deltay>0)&&(deltay<500)){
      diry=true;
      moty.move(deltay*5,dely,diry);
   }
   if((deltay>500)&&(deltay<1000)){
     diry=false;
     deltayneg=deltay-500;
     moty.move(deltayneg*5,dely,diry);
     }

    
    
      
      if((deltax>0)&&(deltax<500)){
  dirx=true;
     motx.move(deltax*5,delx,dirx);

   }
   if((deltax>500)&&(deltax<1000)){
     dirx=false;
    
     deltaxneg=deltax-500;
   motx.move(deltaxneg*5,delx,dirx);
      
     }
Serial.flush();
 Serial.print('A', BYTE);

 }}
void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('A', BYTE);   // send a capital A
    delay(300);
  }
}
  

« Last Edit: August 01, 2009, 12:26:05 pm by adrien » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I didn't have a chance to look at your code too closely, but you could probably use the millis() function instead of delays.
Logged

Pages: [1]   Go Up
Jump to: