I would like to control my stepper via serial monitor and unfortunatley the code below did not work. Serial monitor works (Serial.println()), but it does not execute the stepperOne() part. Any ideas why would be awesome. Thank you.
#include <AccelStepper.h>
const int stepPin = 5;
const int dirPin = 4;
const int enPin = 12;
String msg;
unsigned int DEST = 800;
unsigned int SPD = 1600;
unsigned int ACCEL = 1600;
const int TEST = 3;
void setup() {
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,HIGH);
Serial.begin(9600); // Start Serial
}
void loop() {
if (Serial.available()>0) {
msg = "";
while (Serial.available()) {
delay(10);
char tmp = Serial.read();
// msg += tmp;
if( tmp!='\r' && tmp!='\n' ) msg+=tmp; // Does not work either
}
msg.replace(" ","");
if (msg=="G") {
stepperOne();
Serial.println("Stepper turned.");
}
Serial.flush();
}
}
void stepperOne() {
AccelStepper stepper(1,stepPin,dirPin);
digitalWrite(enPin,LOW);
stepper.setMaxSpeed(SPD);
stepper.setAcceleration(ACCEL);
stepper.move(DEST);
if (DEST > 0) {
while ( stepper.distanceToGo() > 0 ) {
stepper.run();
}
digitalWrite(enPin,HIGH);
}
}
if( tmp!='\r' && tmp!='\n' ) msg+=tmp;