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