THLAB Stepper Motor Code

void setup() {

  //Pins with output are related to the motors and the input pins are related to the button switches
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  digitalWrite(2, HIGH);
}
 



void loop() {

//this is just the same while loop repeated 6 times for each axis in the pos. and neg. direction
// the digital read pin is based off of the button closing the loop or not
  //x direction
   while (digitalRead(6) == LOW)
  {digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    delayMicroseconds(250);
    digitalWrite(3, LOW);
    delayMicroseconds(250);}

  while (digitalRead(7) == LOW)
  {digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    delayMicroseconds(250);
    digitalWrite(3, LOW);
    delayMicroseconds(250);}

//y direction
    while (digitalRead(8) == LOW)
  {digitalWrite(2, LOW);
    digitalWrite(4, HIGH);
    delayMicroseconds(250);
    digitalWrite(4, LOW);
    delayMicroseconds(250);}

  while (digitalRead(9) == LOW)
  {digitalWrite(2, HIGH);
    digitalWrite(4, HIGH);
    delayMicroseconds(250);
    digitalWrite(4, LOW);
    delayMicroseconds(250);}


//z direction
    while (digitalRead(10) == LOW)
  {digitalWrite(2, LOW);
    digitalWrite(5, HIGH);
    delayMicroseconds(250);
    digitalWrite(5, LOW);
    delayMicroseconds(250);}

  while (digitalRead(11) == LOW)
  {digitalWrite(2, HIGH);
    digitalWrite(5, HIGH);
    delayMicroseconds(250);
    digitalWrite(5, LOW);
    delayMicroseconds(250);}
  
  }  

Question is?

while( ) is/can_be blocking, avoid using it like a plague.

Scan you switches every 50ms and look for switch change in state not switch level.

The delays are surely not the best. The delay setting the length of the pulse can be okey but the delay between two pulses, try 25 mS as a first test. Likely it can be tuned down to some 10 mS.

The delays there are micro seconds. :thinking:

delayMicroseconds(250);}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.