Go Down

Topic: Easydriver (stepper) and accelerometer (MMA7260Q) (Read 13 times) previous topic - next topic

mmcp42

#10
Jan 22, 2011, 07:50 pm Last Edit: Jan 22, 2011, 07:51 pm by mmcp42 Reason: 1
suggest you change the stepper code to include a small delay

Code: [Select]

for(int i=0;i<steps;i++){            
  //loop through the number of steps that the stepper motor must complete
  digitalWrite(stepperPin, LOW);
  [glow]delayMicroseconds(500);[/glow]
  digitalWrite(stepperPin, HIGH);
  delayMicroseconds(1000);
}


and rather than sleep (to disable the stepper)
why not use the Enable input instead?
there are only 10 types of people
them that understands binary
and them that doesn't

doublec4

I haven't tried adding that delay in there yet with it being wired properly. When it was wired improperly, it didn't like the delay, however that doesn't mean much considering the way it was wired. I can give that a try.

As for the "enable," I see the pin for it, however, what does it do? If I don't send voltage to it, the board already accepts input. If I send it a voltage, is it just for redundancy?

Thanks!

doublec4

I finally got it to do what I had originally intended. The delays could use some work to speed up the process, and I could filter out some noise a little better, but here is the code I have:

//Arduino Uno, Easydriver, Stepper Motor, Accelerometer Control
//doublec4 2011
//Easydriver pins

int dirPin = 3;
int stepperPin = 2;
int SLEEP = 8;
int MS1 = 7;
int MS2 = 9;

//Accelerometer pins (analog)

const int pinx = 1 ;                  
const int piny = 2  ;              
const int pinz = 3  ;              
const int SLP = 10;

//variables to store accelerometer output and keep track of stepper position
int  valx = 0;                      
int  valy = 0;
int  valz = 0;

int calib = 0;
int position = 0;

int count = 0;




//setup

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

pinMode(SLP, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(stepperPin, OUTPUT);
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(SLEEP, OUTPUT);
}

void loop(){
 
 digitalWrite(SLP, HIGH);              //Make sure the accelerometer is "awake"
 digitalWrite(SLEEP, HIGH);
 digitalWrite(MS1, LOW);
 digitalWrite(MS2, LOW);
 valy = analogRead(piny);
 
 if (count > 2) {

  if (calib == 0) {                //For the first time through the loop, set the "calibration" value to y input from the accelerometer
  calib = 300;
  position = calib;
   }

  //while(valy > (position + 5) || valy < (position - 5)) {
     //digitalWrite(dirPin,HIGH);

     //digitalWrite(stepperPin, LOW);
     //digitalWrite(stepperPin, HIGH);
     //delayMicroseconds(1500);
  //}
  Serial.print("Calibration: ");
  Serial.print(calib);
  Serial.print("   Position: ");
  Serial.print(position);
  Serial.print("   Valy: ");
  Serial.println(valy);
 
 
 if ((valy > (calib + 10)) || (valy < (calib - 10))) {
   
    if ((valy - position) > 0) { //If the G reading (positive direction) is greater than the current position, move up towards that position
       
       digitalWrite(dirPin,HIGH);

       digitalWrite(stepperPin, LOW);
       
       digitalWrite(stepperPin, HIGH);
       delayMicroseconds(1200);
       position++;
    }
   
    if (((calib - valy + calib) - position) > 0) { //If the G reading (negative direction) is greater than the current position, move up towards that position
     
       digitalWrite(dirPin,HIGH);

       digitalWrite(stepperPin, LOW);
       
       digitalWrite(stepperPin, HIGH);
       delayMicroseconds(1200);
       position++;
    }
     
    if (((valy - position) < 0) && (position > calib) && (valy > calib)) { //If the G reading (positive direction) is less than the current position, move down towards that position
     
       digitalWrite(dirPin,LOW);

       digitalWrite(stepperPin, LOW);
     
       digitalWrite(stepperPin, HIGH);
       delayMicroseconds(1200);
       position--;
    }
   
    if ((((calib - valy + calib) - position) < 0) && (position > calib) && (valy < calib)) { //If the G reading (negative direction) is less than the current position, move down towards that position
     
       digitalWrite(dirPin,LOW);

       digitalWrite(stepperPin, LOW);
     
       digitalWrite(stepperPin, HIGH);
       delayMicroseconds(1200);
       position--;
    }
 }
 if ((valy < (calib + 10)) && (valy > (calib - 10))) {
    if (position > calib){
       
      digitalWrite(dirPin,LOW);

      digitalWrite(stepperPin, LOW);
   
      digitalWrite(stepperPin, HIGH);
      delayMicroseconds(1200);
      position--;
    }
   
 }

 }

 count++;

}




Basically, the stepper motor travels in one direction to a position related to a G force. The higher the G, the greater the position away. Right now only using the y axis. When no G is detected, the stepper returns to the original position.

Comments and critiques welcome. Not sure if I could have done this in a more elegant and robust way? I'd love to know! Thanks!

mmcp42

the difference between sleep and enable:

sleep sends the whole board to sleep - it takes a finite time to wake up
enable (or rather NOT enable) is a signal that switches off the output stage

quick look at the data sheet will tell all

as it is a NOT input, by default it is enabled, you can disable by setting it HIGH
there are only 10 types of people
them that understands binary
and them that doesn't

doublec4

Sounds good. So technically, if I have nothing connected to the sleep pin, or the enable pin, it will always be on, and ready for input. Eliminating that amount of time to enable the board?

Go Up