Hi!
I have a Nema17 stepper motor connected to a Arduino motorshield rev 3. I'm trying to make a automated blind and i want to control the number of steps by writing it in the serial monitor.
I have written a code in the Setup part that sets a "Home position". The thought is that the stepper is supposed to spinn untill the limitswitch is pushed/switched and that sets the current position to 0.
The problem for me comes after that... Because .run() works in the Setup part. But when i try to use it again in the Void Loop() part nothing happens...
The code:
#include <AccelStepper.h>
AccelStepper stepper(2,12,13);
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 8;
const int brakeB = 9;
const int limitSwitch = 6;
int limitVal;
long int travelX = 0;
void setup()
{
Serial.begin(9600);
Serial.println("Power on");
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
pinMode(limitSwitch, INPUT_PULLUP);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
stepper.setMaxSpeed(200);
stepper.setSpeed(200);
stepper.setAcceleration(200);
Serial.println("Stepper is homing...");
do{
limitVal = digitalRead(limitSwitch);
stepper.moveTo(-2000);
stepper.run();
}while(limitVal == LOW);
do{
limitVal = digitalRead(limitSwitch);
stepper.moveTo(500);
stepper.run();
}while(limitVal == HIGH);
Serial.println("Stepper home");
Serial.println("");
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(200);
stepper.setAcceleration(200);
}
void loop() {
while (Serial.available()){
Serial.println("Enter the number of steps: ");
travelX = Serial.parseInt();
delay(10);
Serial.println(travelX);
stepper.moveTo(travelX);
stepper.run();
}
}
Thanks in advance for any help!!