Loading...
Pages: [1]   Go Down
Author Topic: How to run a stepper motor without delay?  (Read 319 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Adam
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1. I want to add this code into my existing code but with out delay()

2. Or can I use multiple loops?
like;
void loop()
void loop1()
void loop2()

Also, can I trigger each loop to start and stop independently with diffident delays in each but also let others run?

Code:
int delaylegnth = 1000;

void setup() {
 
  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
 
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B


 
 
}

void loop(){
 
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
 
  delay(delaylegnth);
 
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
 
  delay(delaylegnth);
 
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
 
  delay(delaylegnth);
   
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
 
  delay(delaylegnth);

}

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 314
Posts: 35518
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
1. I want to add this code into my existing code but with out delay()
You need to wait until the stepper has moved before you tell it to step again. How do you propose to do that?

Quote
2. Or can I use multiple loops?
like;
void loop()
void loop1()
void loop2()
Sure, but you will need to call the others from inside loop().

Quote
Also, can I trigger each loop to start and stop independently with diffident delays in each but also let others run?
No. You need to read, understand, and embrace the blink without delay example.
Logged

0
Offline Offline
Tesla Member
***
Karma: 71
Posts: 6624
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to use the technique exemplified in the BlinkWithoutDelay example sketch - ie avoid using delay completely.
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Adam
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Brilliant! Thanks chaps  smiley-cool
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Adam
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Using an Mega2560 with Arduino Motor ShieldV3

I have a 8 wire stepper motor and all working fine with this code,
http://docs-europe.electrocomponents.com/webdocs/001c/0900766b8001c018.pdf

Code:
int delaylegnth = 60;

void setup() {
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
}

void loop(){
 
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  delay(delaylegnth); 
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  delay(delaylegnth);
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  delay(delaylegnth); 
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  delay(delaylegnth);
}

Trying the millies() and not having much luck.

Code:
int delaylegnth = 1000;
long previousMillis = 0; 
long interval = 1000;

void setup() {
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
}
void loop(){
   unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis; 
    }
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A 
  millis(); 
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B 
  millis(); 
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  millis();   
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  millis();
}

What have I not done?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 314
Posts: 35518
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis; 
    }
This is where you also need to take another step.

Code:
  millis(); 
Call a function to get the time. Discard the time that you just got. Doesn't seem very useful to me.

Somewhere above, I said you needed to read, understand, and embrace the blink without delay example. You seem to have managed only one of those three tasks.

You need a step() function that causes the stepper motor to take a step. That function needs to know which step it is at, so it can bang the right set of pins to make the next step happen. The rest of the code should NOT know that. Hint: a static variable in the function.

You need to call that method when the required amount of time, since the last step, has elapsed. That function needs to record when the last step happened.
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Adam
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could you please give me an example Paul? I need to see the full code to work it out so I can understand it and learn more you.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 314
Posts: 35518
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Could you please give me an example Paul?
Code:
    if(currentMillis - previousMillis > interval)
    { // Moved down here where it belongs
       step();
       previousMillis = currentMillis;  // indented properly
    }

Code:
void step()
{
   static int lastStep = 0;
   switch(lastStep)
   {
      case 0:
         // First step code
         break;
      case 1:
         // Second step code
         break;
      case 2:
         // Third step code
         break;
      case 3:
         // Fourth step code
         break;
   }
   lastStep++;
   if(lastStep > 3)
     lastStep = 0;
}

You already have the first, second, third, and fourth step code. First step:
Code:
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A

You can find and move other three step stuff.
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Adam
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Brilliant, thank you Paul. I'll return the full code in a sec for you moderation  smiley-wink
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Adam
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And here it is! It works perfectly, thanks.

Code:
int delaylegnth = 1000;
long previousMillis = 0; 
long interval = 2;

void setup() {
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
}
void step()
{
  static int lastStep = 0;
  switch(lastStep)
  {
  case 0:
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(12, HIGH);
    analogWrite(3, 255);
    break;
  case 1:
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(13, LOW);
    analogWrite(11, 255);
    break;
  case 2:
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(12, LOW);
    analogWrite(3, 255);
    break;
  case 3:
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(13, HIGH);
    analogWrite(11, 255);
    break;
  }
  lastStep++;
  if(lastStep > 3)
    lastStep = 0;
}
void loop(){
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval)
  { // Moved down here where it belongs: Got ya.
    step();
    previousMillis = currentMillis;  // indented properly: Thanks Paul
  }
}
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Adam
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Question?
How dose myDelay work?

Would it be the same as delay();

so would,

delay(1000); be the same as, myDelay(1000);
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Adam
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have now added an LED just see how the code would look.

How would I blink the LED in diffident intervals to the speed of the stepper motor, I have try'd but I'm stumped...

here is the same code but what I have try'd.

Code:
const int ledPin =  40;
int ledState = LOW; 
int delaylegnth = 1000;
long previousMillis = 0; 
long interval1 = 30;
long interval2 = 30;
void setup() {
  pinMode(ledPin, OUTPUT);   
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
}
void step()
{
  static int lastStep = 0;
  switch(lastStep)
  {
  case 0:
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(12, HIGH);
    analogWrite(3, 255);
    break;
  case 1:
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(13, LOW);
    analogWrite(11, 255);
    break;
  case 2:
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(12, LOW);
    analogWrite(3, 255);
    break;
  case 3:
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(13, HIGH);
    analogWrite(11, 255);
    break;
  }
  lastStep++;
  if(lastStep > 3)
    lastStep = 0;
}
void loop(){
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval1)
  if(currentMillis - previousMillis > interval2)
  { // Moved down here where it belongs: Got ya.
    step();
    previousMillis = currentMillis;  // indented properly: Thanks Paul
        if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Thanks,
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 314
Posts: 35518
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if(currentMillis - previousMillis > interval1)
  if(currentMillis - previousMillis > interval2)
If you ALWAYS used curly braces after an if statement, you'd see the problem.
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Adam
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As in,

Code:
  if(currentMillis - previousMillis > interval1);
  if(currentMillis - previousMillis > interval2);
?? smiley
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 314
Posts: 35518
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

These are curly braces: { and }
This is not: ;
Logged

Pages: [1]   Go Up
Print
 
Jump to: