Go Down

Topic: The simplest stepper code not working (Read 791 times) previous topic - next topic

Doomrazor

Hello everybody. I'm very new at programming and need some help with a basic thing that I couldn't find information about here on the forum. (probably it's too simple).

I have a stepper motor connected to an easydriver. I'm trying to make it do small rotations (45°, 200steps) with delay, and after a completed  rotation (360°, 1600steps) turn and go back.

Here's the code. Could you take a look and leave a comment? I would be so happy.

int stepperDir = 8;   // Direction pin on the Easydriver
int stepperStep = 9;   // Step pin on the Easydriver
int stepperCount = 0;   // Number holder for 360° rotation.
int stepperWaitCount = 0;   // Number holder for 45° rotation.


void setup() {
 
  pinMode(stepperDir, OUTPUT); 
  pinMode(stepperStep, OUTPUT);
  digitalWrite(stepperDir, LOW);
  digitalWrite(stepperStep, LOW);
}



void loop(){
 
  // Take steps with 70 milliseconds delay.
  digitalWrite(stepperStep, HIGH);
  delayMicroseconds(70);
  digitalWrite(stepperStep, LOW);
  delayMicroseconds(70);

  //Count and store steps.
  stepperCount = stepperCount + 1;
  stepperWaitCount = stepperWaitCount + 1;
 

  // If stepper has taken 200 steps, paus for half a second, than zero out the counter and continue stepping
  if (stepperWaitCount == 200)
  {
   delay(500);
  }
  stepperWaitCount = 0;
 
 
  // If stepper has taken 1600 steps, turn in opposite direction, zero out the stepper counter and paus for a second, than continue.
  if (stepperCount == 1600)
  {
    if (digitalRead(stepperDir) == LOW)
    {
      digitalWrite(stepperDir, HIGH);
    }
    else
    {
      digitalWrite(stepperDir, LOW);
    }
    stepperCount = 0;
    delay(1000);
  }
}

If anyone can see the mistake I've done. Please leave a comment.

Thanks!

/Kalle

LeonardoNewbie

Hi,

you say in the comment that's milliseconds, but the code uses microseconds:
  // Take steps with 70 milliseconds delay.
  digitalWrite(stepperStep, HIGH);
  delayMicroseconds(70);

The major point might be this, you always reset stepperWaitCount:
  if (stepperWaitCount == 200)
  {
   delay(500);
  }
  stepperWaitCount = 0;

Better maybe this:

  if (stepperWaitCount == 200)
  {
   delay(500);
  stepperWaitCount = 0;
  }

bye

wildbill

It's not quite the simplest stepper code - have you managed to make the stepper turn at all yet? If not, just get it turning before you try more complex control patterns. Unless of course using the suggestions above gets you going.

Doomrazor

LeonardoNewbie: You're totally right about the micro/milliseconds. It's supposed to be microSeconds though. Thank you. And thank you for the code change. I'll try it tomorrow when I have my stuff available. I'll get back if it works or not.

wildbill: Yes, I've tried other things and got it working. You're probably right, it's not the simplest code, but kind of basic I guess. Thanks anyway.


kf2qd

Code would be simpler to understand if you broke your move code into a seperate function -

Feed it the number of steps and the direction. Every time it steps a or subtract 1 from a position variable. It would actually simplify the logic of you program.

Go Up