Problem with stepper motor code

I’m trying to build a blind controller with a photon, A4988 driver and nema17 motor.

The circuit is set up correctly and when use this code, it works as expected:

const int stepPin = D7; 
const int dirPin = D6; 
void setup() {
pinMode(stepPin,OUTPUT); 
pinMode(dirPin,OUTPUT);
}
void loop() {
digitalWrite(dirPin,HIGH); 
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH); 
delayMicroseconds(2000); 
digitalWrite(stepPin,LOW); 
delayMicroseconds(2000); 
}
delay(1000);
digitalWrite(dirPin,LOW); 
for(int x = 0; x < 400; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin,LOW);
delayMicroseconds(2000);
}
delay(1000);
}

But when I try to use the following code, the motor just turns 1-2 steps no matter what delay and step count I use:

const int stepPin = D7; 
const int dirPin = D6;

void setup() {
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
Particle.function("stepper",steptoggle);
}

void loop() 
{

}

int steptoggle(String command) {

if(command=="left") {
    digitalWrite(dirPin,HIGH); 
    for(int x = 0; x < 200; x++)
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(20000);
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(20000);
    return 0;
}
else if(command=="right"); {
    digitalWrite(dirPin,LOW);
    for(int x = 0; x < 400; x++)
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(20000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(20000);
    return 1;
}
}

Any help would be greatly appreciated.

Thanks

You are missing the curly braces for your for() loops.
You are only repeatadly setting the pin HIGH but never set it LOW again during the for().

That’s where proper code indentation helps pinpoint such issues :wink:

2 Likes

Thanks for the response.

I’ve edited it but it’s still doing the same:

const int stepPin = D7; 
const int dirPin = D6;

void setup() {
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
Particle.function("stepper",steptoggle);
}

void loop() 
{

}

const int steptoggle(String command) {

if(command=="left") {
    digitalWrite(dirPin,HIGH); 
    for(int x = 0; x < 200; x++)
    {digitalWrite(stepPin,HIGH); 
    delayMicroseconds(2000);
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(2000);
    return 0;}
}
else if(command=="right"); {
    digitalWrite(dirPin,LOW);
    for(int x = 0; x < 400; x++)
    {digitalWrite(stepPin,HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(2000);
    return 1;}
}
}

So, what happens to the flow of the function once the return statements are reached?

They need to be outside (and after) the for-loop.

:wink:

3 Likes

Still, proper indentation helps yourself spot these things :wink:

How would you see it this way

const int steptoggle(String command) {
  if(command=="left") {
    digitalWrite(dirPin,HIGH); 
    for(int x = 0; x < 200; x++) {
      digitalWrite(stepPin,HIGH); 
      delayMicroseconds(2000);
      digitalWrite(stepPin,LOW); 
      delayMicroseconds(2000);
      return 0;
    }
  }
...
}

See the mistake now?

Clean code formatting is not only a whim but has some real purpose.

3 Likes

Thanks guys, all working now