Controlling stepper motor

Hi !

I'm total beginner with arduino but i must say i am impressed with simplicity and functionality.

Question:
I'm running one 4 wire stepper motor over EasyDriver board and its working as expected.
The sample that am using is the sample from Schmalzhaus.com web page.
I want to extend this sample with some additional steps and timing.

This is what i want to do:
Position X --> hold for 5 seconds
switch to position Y
Position Y --> hold for 1 second
return to position X
Position X --> hold for 5 seconds
endless loop

Start will be triggered by button ( not in code ).

How to make this ?. Any help is welcome !
Thank you in advance.

int Distance = 0;  // Record the number of steps we've taken


void setup() {                

  pinMode(8, OUTPUT);     

  pinMode(9, OUTPUT);

  digitalWrite(8, LOW);

  digitalWrite(9, LOW);

}


void loop() {

  digitalWrite(9, HIGH);

  delayMicroseconds(100);          

  digitalWrite(9, LOW); 

  delayMicroseconds(100);

  Distance = Distance + 1;   // record this step

  

  // Check to see if we are at the end of our move

  if (Distance == 3600)

  {

    // We are! Reverse direction (invert DIR signal)

    if (digitalRead(8) == LOW)

    {

      digitalWrite(8, HIGH);

    }

    else

    {

      digitalWrite(8, LOW);

    }

    // Reset our distance back to zero since we're

    // starting a new move

    Distance = 0;

    // Now pause for half a second

    delay(500);

  }

}

How long do you want to be at position X? The way you describe it, it will be there for 10 seconds (5 seconds after it has moved + 5 seconds before it moves)

There's lots of very fine nuances that can significantly affect how you want to code this.. I'm going to go with you wanting it to be AT Y for 1 second (while that is completely different from not X)

I have a strong aversion to using Delay(), simply because if you ever want to add something else, it will get delayed as well..... but it is the simplest way.

I would also give the pins names, which will help keep track of them..

Here's what I bashed together.. it compiles, that's all the guarantee I can give..

There's tons of ways to write this, and this isn't the ultimate, but I think it'll work for you

int IncrementPin = 9;
int DirectionPin = 8;
int ButtonPin = 10; //my wild guess
int CurrentPosition; //Where the stepper motor is now

int Xposition = 0; //may need to swap these if it's backward
int Yposition = 3600;

void setup() {
  pinMode(IncrementPin, OUTPUT);
  pinMode(DirectionPin, OUTPUT);
  pinMode(ButtonPin, INPUT_PULLUP); //I'm going out on a limb here.

  digitalWrite(IncrementPin, LOW);
  digitalWrite(DirectionPin, LOW);

}

void loop() {
  static boolean RunMode = false;
  if (!RunMode) { //RunMode is false until the button is pressed, then doesn't get checked anymore
    //latching RunMode on
    RunMode = digitalRead(!ButtonPin);
  }
  else {
    delay(5000);
    MoveMotor(Yposition);
    delay(1000);
    MoveMotor(Xposition);
    delay(5000);

  }
}
void MoveMotor(int NewPosition) {
  boolean Forward = (NewPosition > CurrentPosition); //sets the direction variable
  int AddValue;  //How much we add to the current position... it will only be 1 or -1
  if (Forward) { // perhaps this is backward and should be NOT forward (!Forward)
    digitalWrite(DirectionPin, HIGH);
    AddValue = 1;
  }
  else {
    digitalWrite(DirectionPin, LOW);
    AddValue = -1;
  }
  //OK, now we have the direction out of the way, the rest of your code should work
  while (CurrentPosition != NewPosition) {
    digitalWrite(9, HIGH);
    delayMicroseconds(100);
    digitalWrite(9, LOW);
    delayMicroseconds(100);

    CurrentPosition += AddValue;  // record this step
    // Using the += operator does the same as what you had
  }
}

@Rx7man's idea of creating a function to control the motor and separate the motor activity from the timing logic is good.

However if you want to do anything more than a simple demo the use of the delay() or delayMicroseconds() functions and a WHILE loop will get in your way as the Arduino can't do anything else while they are in progress. Have a look at how millis() or micros() can be used to manage the timing in the second example in this simple stepper code.

You may find something useful in stepper motor basics

...R

Thank you both for reply's.

I will take a look at suggested links because am planning to add some other timing functions and they are all controlled by button press. Before that i will try the code from Rx7man.

How long do you want to be at position X? The way you describe it, it will be there for 10 seconds (5 seconds after it has moved + 5 seconds before it moves)

Good question :-). Stepper should be exactly 5 seconds in X position, not 10.

It's basically fort / back spinning but with pausing on X and Y position.
Start / set to main position ---> hold for 5 seconds
Quick back to position Y --> hold for 1 second
Spin back to position X --> hold for 5 second.

Arnix
p.s. i will calculate power consumption and if this will be too much i have one other idea, but i dont know if this makes sense.. To make constant spinning in full circle but instead of delay, maybe some micro stepping can do the job ? E.g. Start position ---> micro stepping for 5 seconds, fast spin to position Y (in same direction ), micro stepping for 1 second, fast spin to position X and so on... Just an idea.