1

I am using a stepper motor for a project that I want it to stop. I tried for loops, while, and an if statement. All of these were placed inside the void loop, because it raised an error.

My code with the if loop:

//This program does about 1 rotation or 4100 steps. The program should be able to stop the stepper motor after it does one rotation.
int Pin1 = 8;
int Pin2 = 9;
int Pin3 = 10;
int Pin4 = 11;
int _step = 0;
int _on = 4100;
int _switches = 0;
boolean direct = false;
void setup() {
  pinMode (Pin1, OUTPUT);
  pinMode (Pin2, OUTPUT);
  pinMode (Pin3, OUTPUT);
  pinMode (Pin4, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (_on > 0) {
    switch(_step) 
    {
      case 0:
        digitalWrite (Pin1, LOW);
        digitalWrite (Pin2, LOW);
        digitalWrite (Pin3, LOW);
        digitalWrite (Pin4, HIGH);
      break;
      case 1:
        digitalWrite (Pin1, LOW);
        digitalWrite (Pin2, LOW);
        digitalWrite (Pin3, HIGH);
        digitalWrite (Pin4, HIGH);
      break;
      case 2:
        digitalWrite (Pin1, LOW);
        digitalWrite (Pin2, LOW);
        digitalWrite (Pin3, HIGH);
        digitalWrite (Pin4, LOW);
      break;
      case 3:
        digitalWrite (Pin1, LOW);
        digitalWrite (Pin2, HIGH);
        digitalWrite (Pin3, HIGH);
        digitalWrite (Pin4, LOW);
      break;
      case 4:
        digitalWrite (Pin1, LOW);
        digitalWrite (Pin2, HIGH);
        digitalWrite (Pin3, LOW);
        digitalWrite (Pin4, LOW);
      break;
      case 5:
        digitalWrite (Pin1, HIGH);
        digitalWrite (Pin2, HIGH);
        digitalWrite (Pin3, LOW);
        digitalWrite (Pin4, LOW);
      break;
      case 6:
        digitalWrite (Pin1, HIGH);
        digitalWrite (Pin2, LOW);
        digitalWrite (Pin3, LOW);
        digitalWrite (Pin4, LOW);
      break;
      case 7:
        digitalWrite (Pin1, HIGH);
        digitalWrite (Pin2, LOW);
        digitalWrite (Pin3, LOW);
        digitalWrite (Pin4, HIGH);
      break;
      case 8:
        digitalWrite (Pin1, LOW);
        digitalWrite (Pin2, LOW);
        digitalWrite (Pin3, LOW);
        digitalWrite (Pin4, LOW);
      break;
    }

    if(direct == false) {
      _step++;
      _switches++;
      direct = false;
    }else{
      _step--;
      _switches--;
      direct = true;
    }

    Serial.println(_switches);

    if(_step>7) {
      _step=0;
    }

    if(_step<0) {
      _step=7;
    }
    _on--;
    delay(1); // This solves it.
  }
  break; //This raises an error when used with for loop.
}

Can anyone help on why the delay makes it stop? And why the break command raises and error?

Arduino UNO. 5 wire stepper.

  • I'm not sure if print statements can keep up the speed, but you can try it (print out the relevant variables, so you can see where it is going wrong), for this you can temporarily comment out the digitalWrites. Also a tip, to give your variables a good name (like _on, I would expect it is a boolean). – Michel Keijzers Feb 18 at 16:17
  • 2
    @michel keijzers. I have fixed it, the delay at the end fixes it for some reason. I don’t know why though. – 12944qwerty Feb 18 at 16:19
  • Ok good to hear :-) – Michel Keijzers Feb 18 at 16:21
1

... why the break command raises and error?

Because you are using it wrong! break will end a loop, and is used in the switch...case statements. Read the documentation for break if you need to know more

I have fixed it, the delay at the end fixes it for some reason. I don’t know why though.

You haven't really fixed it if you don't know why. It seems that you are trying to directly control a 5 wire stepper, i assume your pins must drive a transistor or something as the arduino pins themselves will not work.

The reason it works is because the stepper takes time to energize and move, if you don't wait long enough it won't work, your serial.print must be long enough, as the delay(1) should produce a 1ms delay which is too short.

You are driving your stepper wrong, you have the code setup for a half-step control, but you have 9 states, state 8 in not needed.

you are duplicating effort you don't need with both _steps and _switches, they always have the same value.

you can do better on your code as well:

int dir = 1
int c = 0

// in loop
c += dir

if (c > max)
{
  dir *= -1
  c = max
}

if (c < min)
{
  dir *= -1
  c = min
}

With this you don't have to check the direction to modify your variables.

  • Just for clarity, the stepper doesn't stop because it needs a delay to calculate what to do next? Also, the break should break the main loop void loop(). And, lastly, this is my first time using Arduino and C. I don't know how to work with the code as well as many of you guys. – 12944qwerty Feb 18 at 23:26
  • @12944qwerty void loop() is a function, you return from functions, you break from loops; naming a function loop doesn't make it a loop; its called loop() because the main Arduino library calls loop() in a loop! – esoterik Feb 19 at 1:26
  • @12944qwerty the stepper motor needs a delay to PHYSICALLY move; it takes time for the coils to energize and build up a magnetic field, then the motor will move to the next step. – esoterik Feb 19 at 1:28
0

In simple terms: break() literally breaks out of the immediate loop it is in. Since you were in no loop [void loop() is not your usual loop functions, it's just Arduino-for keep repeating whatever is inside infinitely. You could use a for/while loop in setup() for how many ever times you want it to run and then use break().] delay(time in ms), as the name says just pauses the Arduino like a brainfart for how much ever time you put as the parameter. You could check out this link in the future for information of inbuilt functions and other language-related doubts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.