Stepper motor programming

Hi, i’m trying to programming an stepper motor but i don’t know what’s wrong.

/* Stepper Motor v2
 *22 Feb 2014
 *In progress
*/


int mPin_1[] = {22, 23, 24, 25};             // pin connection
int pin, count, npin;
int n = 20;                                  //timer no

void setup(){
  
  for(pin = 0; pin < 4; pin++)
    pinMode(mPin_1[pin], OUTPUT);            // pins set as output
  for(pin = 0; pin < 4; pin++)
    digitalWrite(mPin_1[pin], LOW);          // pins set as low
}

void mInainte(){                             // forward spin
  for(count = 0; count < 8; count++){        // counter for 8 steps
    for(npin = 0; npin < 4; npin++){         // counter for pins
      if((count = 0) && (npin = 0)){         // 1st step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }
      else if((count = 1) && (npin = 2)){    // 2nd step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }
      else if((count = 2) && (npin = 0)){    // 3rd step
        digitalWrite(mPin_1[npin], LOW);
        break;
      } 
      else if((count = 3) && (npin = 1)){    // 4th step
        digitalWrite(mPin_1[npin],HIGH);
        break;
      }
      else if((count = 4) && (npin = 2)){    // 5th step
        digitalWrite(mPin_1[npin],LOW);
        break;
      }
      else if((count = 5) && (npin = 3)){    // 6st step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }
      else if((count = 6) && (npin = 1)){    // 7th step
        digitalWrite(mPin_1[npin], LOW);
        break;
      }
      else if((count = 7) && (npin = 0)){    // 8th step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }
    }
    delay(n);
  }
}

void loop(){
  mInainte();                                //call
}

but i don't know what's wrong.

And, you expect us to? The code does something. You didn't say what. You expect it o do something. You didn't say what. You have some kind of hardware between the Arduino and the stepper motor. You didn't say what.

int n = 20;                                  //timer no

No, it isn't. One letter global variable names are a bad idea.

count, pin, and npin should NOT be global variables.

      if((count = 0) && (npin = 0)){         // 1st step

= is not for comparison. == is.

the timer is just for test will be another type of delay. I’m using l298n drivers for the stepper mercury SM- 428YG011-25. Thanks for reply, i’m trying now to change the egualisation value to comparison. I’m beginer at programming :slight_smile: i’ll try to not write global variables.

Re: I’ve changed the code but doesen’t work, the 3rd port doesent work. Below is the table guide for HIGH and LOW

0 1 2 3

0 | 1 0 0 0
1 | 1 0 1 0
2 | 0 0 1 0
3 | 0 1 1 0
4 | 0 1 0 0
5 | 0 1 0 1
6 | 0 0 0 1
7 | 1 0 0 1

/* Stepper Motor v2
 *22 Feb 2014
 *In progress
*/


int mPin_1[] = {22, 23, 24, 25};             // pin connection

void setup(){
  int pin;                                   // output pin counter
 
  for(pin = 0; pin < 4; pin++)
    pinMode(mPin_1[pin], OUTPUT);            // pins set as output
  for(pin = 0; pin < 4; pin++)
    digitalWrite(mPin_1[pin], LOW);          // pins set as low
}

void mInainte(){                             // forward spin

  int n = 20;                                // timer no
  int npin;                                  // forward pin counter
  int count;                                 // step counter

  for(count = 0; count < 8; count++){        // counter for 8 steps
    for(npin = 0; npin < 4; npin++){         // counter for pins
      if((count == 0) && (npin == 0)){         // 1st step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }
      else if((count == 1) && (npin == 2)){    // 2nd step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }
      else if((count == 2) && (npin == 0)){    // 3rd step
        digitalWrite(mPin_1[npin], LOW);
        break;
      } 
      else if((count == 3) && (npin == 1)){    // 4th step
        digitalWrite(mPin_1[npin],HIGH);
        break;
      }
      else if((count == 4) && (npin == 2)){    // 5th step
        digitalWrite(mPin_1[npin],LOW);
        break;
      }
      else if((count == 5) && (npin == 3)){    // 6st step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }
      else if((count == 6) && (npin == 1)){    // 7th step
        digitalWrite(mPin_1[npin], LOW);
        break;
      }
      else if((count == 7) && (npin == 0)){    // 8th step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }
    }
    delay(n);
  }
}

void loop(){
  mInainte();                                //call
}
  int pin;                                   // output pin counter
 
  for(pin = 0; pin < 4; pin++)
    pinMode(mPin_1[pin], OUTPUT);            // pins set as output
  for(pin = 0; pin < 4; pin++)
    digitalWrite(mPin_1[pin], LOW);          // pins set as low

should be:

  for(int pin = 0; pin < 4; pin++)
  {
     pinMode(mPin_1[pin], OUTPUT);            // pins set as output
     digitalWrite(mPin_1[pin], LOW);          // pins set as low
  }

Below is the table guide for HIGH and LOW

Where did you come up with this?

      if((count == 0) && (npin == 0)){         // 1st step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }

What you are doing here does NOT match your table. Your table says pin 0 should be on AND pins 1, 2, and 3 should be off. You have omitted 3/4 of the code in each section.

Quote
Below is the table guide for HIGH and LOW
Where did you come up with this?

From here but simplified
http://www.piclist.com/images/www/hobby_elec/e_step1.htm

but 1st is not on and the rest off? i,ve initializated pins in setup as LOW

just 3rd port doesent move, i have leds on each port of driver.
from 3 to 5 rows on table doesen’t work.

but 1st is not on and the rest off? i,ve initializated pins in setup as LOW

That is true on the first pass through the inner loop. It is never true again. You need to explicitly set the state of all 4 pins.

I've thinked to do with bitwise but i dont know i've searched on internet some exercices but i dont know how to transorm into a boolean from hexa or numerical all 4 pins. if u can show me an example, i'll try to figure out how it work and if i can't i will ask for more info on how to apply bitwise. Thanks

I've thinked to do with bitwise but i dont know

Then, don't try. Use the brute force technique first. Get that working FIRST. Then, you can consider more advance techniques.

It works thanks. Now u can show me an example with bitwise? Please.

/* Stepper Motor v2
 *22 Feb 2014
 *In progress
*/


int mPin_1[] = {22, 23, 24, 25};             // pin connection

void setup(){
  int pin;                                   // output pin counter
 
  for(pin = 0; pin < 4; pin++){
    pinMode(mPin_1[pin], OUTPUT);            // pins set as output
  }
}
void initializare(){                        // pins initialization
  int pin;                                  // pin set to low
  
  for(pin = 0; pin < 4; pin++){
    digitalWrite(mPin_1[pin], LOW);
  }
}

void mInainte(){                             // forward spin

  int n = 20;                                // timer no
  int npin;                                  // forward pin counter
  int count;                                 // step counter

  for(count = 0; count < 8; count++){        // counter for 8 steps
    for(npin = 0; npin < 4; npin++){         // counter for pins
      if((count == 0) && (npin == 0)){         // 1st step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }
      else if((count == 1) && (npin == 2)){    // 2nd step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }
      else if((count == 2) && (npin == 0)){    // 3rd step
        digitalWrite(mPin_1[npin], LOW);
        break;
      } 
      else if((count == 3) && (npin == 1)){    // 4th step
        digitalWrite(mPin_1[npin],HIGH);
        break;
      }
      else if((count == 4) && (npin == 2)){    // 5th step
        digitalWrite(mPin_1[npin],LOW);
        break;
      }
      else if((count == 5) && (npin == 3)){    // 6st step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }
      else if((count == 6) && (npin == 1)){    // 7th step
        digitalWrite(mPin_1[npin], LOW);
        break;
      }
      else if((count == 7) && (npin == 0)){    // 8th step
        digitalWrite(mPin_1[npin], HIGH);
        break;
      }
    }
    delay(n);
  }
}

void loop(){
  initializare();                            // call initialization
  mInainte();                                // call forward move
}

Code for who need!!!

Now u can show me an example with bitwise?

Can u spell? This is NOT a text-chat room.

byte step[8] = {0b1000, 0b1010, 0b0010, 0b0110, 0b0100, 0b01010, 0b0001, 0b1001 };

Now, on each pass through the outer loop, count is the index into this array:
byte stepData = step[count];

In the inner loop, use bitRead() to read the ith bit, and set the ith pin to that value, for all 4 bits set. Much shorter code.

I have the same stepper motors. They work very nicely with the Pololu A4988 stepper motor driver boards which make the programming soooo much easier. The A4988 can also drive the motors at up to 30v for better performance, and it can be set to do microstepping.

...R