Recently I started work on a project that requires my laptop to send serial communicate with my arduino. I got the communication working, where if a press a key on my keyboard it will send a corresponding message to my arduino, but whenever I send a message the arduino will pause causing the attached stepper motor to stop for a second and then start. If I hold down the key to continuously send messages the stepper motor will only do 1 step at a time with pauses in between instead of continuously rotating. Does anyone know what is causing this, I've attached the code for my arduino below. Right now for testing purposes the motor will continuously rotate if not given a command.
#define relay 13
#define dir 8 //Stepper motor direction pin
#define step 9 //Stepper motor step pin
String command = ""; //Current action to be run
String direction = "null"; //Direction of stepper motor
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.write(1);
pinMode(step, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(relay, OUTPUT);
}
void loop() {
if(Serial.available()) //Checks to see if a message was sent to the arduino, if so, sets the command var to it
{
command = Serial.readString();
Serial.write(1);
}
else command = "";
if (command.equals("FIRE")) digitalWrite(relay, HIGH); //Runs corresponding actions for commands
if (command.equals("RIGHT")) stepRight();
if (command.equals("LEFT")) stepLeft();
if(direction.equals("RIGHT") || direction.equals("LEFT"))
{
stepMotor();
}
}
void stepRight()
{
direction = "RIGHT";
digitalWrite(dir, LOW);
}
void stepLeft()
{
direction = "LEFT";
digitalWrite(dir, HIGH);
}
void stepMotor()
{
digitalWrite(step, HIGH);
delayMicroseconds(500);
digitalWrite(step, LOW);
delayMicroseconds(500);
}