Go Down

Topic: Stepper Motor Code (Read 435 times) previous topic - next topic

devika9

#15
Sep 01, 2017, 12:06 am Last Edit: Sep 01, 2017, 12:16 am by devika9
Hello Robin,

Yes, I am 800% sure. All the codes I have posted so far I have tried them first on my motor with arduino.

The code in reply #11, works the exact way it is coded. The motor runs 100 steps in direction #1 and then goes 100 step in direction #2. Then goes 200 steps in direction  #1 next it takes 100 steps in direction #2. Lastly, it takes 300 steps in direction #1. Then it starts again to repeat the cycle with 100 steps in direction #1

Now, I am posting here two codes, #1 doesn't work and #2 does works.

#1

Code: [Select]
const int stepPin = 5;
const int dirPin = 4;
 
void setup() {
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
 
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction to 2nd direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 300; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);
  }
 
void loop() { 
}   




#2

Code: [Select]
const int stepPin = 5;
const int dirPin = 4;
 
void setup() {
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
} //change

void loop() { //change
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction to 2nd direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 300; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);
  }
 
// void loop() {  /// change, basically bringing the main code inside loop
//}   


#2 Works exactly as I mentioned above. I am running at a voltage supply of 19 volts.
When I am running the code #2 I see current fluctuation between 1.07 A to .14 A. Whereas, there is no current fluctuation while running code #1( which does not work, the motor doesn't moves at all).

I have copied both the codes  from Arduino software window  & pasted here after uploading as well as running them on my Arduino board.



devika9

Hello Robin,

I got this code working the exact way I want by using "while" command.

Code: [Select]
const int stepPin = 5;
const int dirPin = 4;
 
void setup() {
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  digitalWrite(dirPin,LOW); //change
  digitalWrite(stepPin,LOW); // change
 
} //change

void loop() { //change
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay
 
  digitalWrite(dirPin,LOW); //Changes the rotations direction to 2nd direction
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
  for(int x = 0; x < 301; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
    if (x==300){//change
      while(true)/*Do nothing*/;//change
    }
    }
  }

 
// void loop() {  /// change, basically bringing the main code inside loop
//}   



After running once, either I have to reload the code or press reset button to run the code again which is completely fine for me.

However, I am not sure if I am doing something wrong to my motor or board by using the above code.

 I will still try to figure out why my code #1 in previous reply didn't work. I read everywhere that the code should work if it's in setup () instead loop().

Robin2

I tried your code with LEDs in place of your motors and, as usual, @MarkT was correct back in Reply #7.

I reckon the real problem is that your original program is not working properly and only appears to work because the code is in loop(). When it is taken out of loop() its weakness is exposed.

Rather than delayMicroseconds(500) try delay(100) for a step rate of about 5 per second. If that works then you can try faster speeds.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up