Data Input from Serial Monitor to Control Stepper Motor with Arduino Uno

I want to develop C# win app application to control stepper motor. To do that first i want to control the motors by entering data from serial monitor at Arduino IDE 2.0.0. I am using CNC Shield and related motor drivers. All equipment is working, i tested all by entering data directly to code. I am able to control the motor. However, i can not control the motor via serial monitor. There is no rotation at all.

I wanted to control by giving two data(direction and rotation) but i am not able to rotate the motor. According to mo code, my input is in the form of "C,3600".I am adding the code below. I think i have problem in if or for loop.

I really appreciate to all helps.

#include <Stepper.h>

const int StepX = 2;
const int DirX = 5;
//const int StepY = 3;
//const int DirY = 6;
//const int StepZ = 4;
//const int DirZ = 7;

int index =0;
String data[2];

void setup() {
  pinMode(StepX,OUTPUT);
  pinMode(DirX,OUTPUT);
  //pinMode(StepY,OUTPUT);
  //pinMode(DirY,OUTPUT);
  //pinMode(StepZ,OUTPUT);
  //pinMode(DirZ,OUTPUT);

}

void loop() {

 //digitalWrite(DirY, HIGH);
 //digitalWrite(DirZ, HIGH);

  if(Serial.available()){
    data [index] = Serial.readStringUntil(',');
    Serial.print("data read:");
    Serial.println(data[index]);
    index++;
    if(index==2){
      index = 0;
      for(int x = 0; x<data[1].toInt(); x++) { // loop for data[1] steps
         if(data[0] == "C") digitalWrite(DirX,HIGH); // set direction, HIGH for clockwise
                                                     //, LOW for anticlockwise
         else if(data[0] == "W") digitalWrite(DirX,LOW); 
        
        /*digitalWrite(DirX,HIGH);
        delayMicroseconds(500);        
        digitalWrite(DirX,LOW); 
        delayMicroseconds(500);*/
      }      

    }
     while(1);
  }

You are missing a Serial.begin in setup.

It appears that you are using 2 serial sends to send the speed and direction, reading that into 2 Strings and converting to numbers to control the stepper. It, I think, would be better to send only one packet, read it into a c-string (null terminated character array), parse the string for the data and convert to numbers to control the stepper. The serial input basics tutorial will show how to make the packet, receive and parse it.

Using the String class without knowing what you are doing can cause memory problems. See the Evils of Strings page.

By using 'peek' you can tell if the next thing in the input is a number.

#include <Stepper.h>

const int StepX = 2;
const int DirX = 5;

void setup()
{
  Serial.begin(115200);
  pinMode(StepX, OUTPUT);
  pinMode(DirX, OUTPUT);
}

void loop()
{
  if (Serial.available())
  {
    char p = Serial.peek(); // Look at the first character in the buffer
    if (p == 'C')
    {
      digitalWrite(DirX, HIGH);
      Serial.read(); // Remove it from the buffer
    }
    else if (p == 'W')
    {
      digitalWrite(DirX, LOW);
      Serial.read(); // Remove it from the buffer
    }
    else if (p >= '0' && p <= '9')
    {
      int steps = Serial.parseInt();

      for (int x = 0; x < steps; x++)
      {
        digitalWrite(StepX, HIGH);
        delayMicroseconds(500);
        digitalWrite(StepX, LOW);
        delayMicroseconds(500);
      }
    }
    else // uninteresting character
      Serial.read(); // Remove it from the buffer
  } // End: if (Serial.available())
}  // End: loop()

Thanks a lot. Completely working.
:smiley:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.