Control 2 stepper motros with Arduino Uno from C# application

To control 2 servo motor independently from C# desktop application I send pin and trackBar1 value this way:

      private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (myport.IsOpen)
            {
                myport.WriteLine("9");
                myport.WriteLine(trackBar1.Value.ToString());               
            }
        }

With Arduino load code:

     #include <Servo.h>
    Servo servo1;
    Servo servo2;
    int val;
    
    void setup() {
        Serial.begin(9600);
        servo1.attach(9);
        servo2.attach(11);
    }
    
    void loop() { 
      val = Serial.parseInt(); 
      if(val == 9){
        servo1.write(Serial.parseInt()); 
      }
      else if(val == 11){
        servo2.write(Serial.parseInt());  
      }
    }

To run stepper motor in rotation:

 int stpPin = 12; 
    int dirPin = 13; 
    int a = 0;
    void setup()
    {
      pinMode(stpPin, OUTPUT);
      pinMode(dirPin, OUTPUT);
    }
    
    void loop()
    {
      if (a < 200)
      {
        a++;
        digitalWrite(stpPin, HIGH);
        delay(10);
        digitalWrite(stpPin, LOW);
        delay(10);
      }
      else
      {
        digitalWrite(dirPin, HIGH);
        a++;
        digitalWrite(stpPin, HIGH);
        delay(10);
        digitalWrite(stpPin, LOW);
        delay(10);
        
        if (a>400)
        {
          a = 0;
          digitalWrite(dirPin, LOW);
        }
      }
    }

But how to control steppers from C# application send step and directory, if I have to #include <Stepper.h> function library and set Stepper stepper; what is a loaded code for Arduino?

Any guide, advice or example would be helpful

The stepper library is not meant for the step-dir type of stepper motor driver. The AccelStepper library is for those drivers. There are many examples with the library to get you started.

Stepper basics.
Simple stepper program.

Reading from the serial port before you know that there is data to be read doesn’t make much sense (see Serial.available())

The Serial.parseInt() function is a blocking function. The serial input basics tutorial shows how to read serial data without blocking.

Stepper or servo? You seem to have both. In either case, you will need to develop a protocol where you send a short (fast) command from C# to Arduino. Arduino then interprets this commands and issues instructions to the appropriate library.

DKWatson:
Stepper or servo? You seem to have both. In either case, you will need to develop a protocol where you send a short (fast) command from C# to Arduino. Arduino then interprets this commands and issues instructions to the appropriate library.

Hello, yes I'm using both stepper and servos control from C# application

Usually, when sending serial data from a host app, clockwise steps are indicated with a positive integer, counterclockwise with a negative integer. Your Arduino code must then interpret these values and set the dir pin accordingly. The step function simply requires the number of steps (positive value).

groundFungus:
The stepper library is not meant for the step-dir type of stepper motor driver. The AccelStepper library is for those drivers. There are many examples with the library to get you started.

Stepper basics.
Simple stepper program.

Reading from the serial port before you know that there is data to be read doesn't make much sense (see Serial.available())

The Serial.parseInt() function is a blocking function. The serial input basics tutorial shows how to read serial data without blocking.

Hello,

Thank you

Very useful information

DKWatson:
Usually, when sending serial data from a host app, clockwise steps are indicated with a positive integer, counterclockwise with a negative integer. Your Arduino code must then interpret these values and set the dir pin accordingly. The step function simply requires the number of steps (positive value).

I use two programs, one rotates with 360 degree full turn in one direction, another with change of direction on 360 degree. I’m trying to figure out, why I can’t stop movement by one or another way, depending on the load, even if I reset controller and load with Arduino IDE empty code, it performs the previous program, continues rotation or moving from side to side, and why holding the motor in hand, somehow affects motor work, steps begin to slow or even stops, I’m not touching shift itself, then if I put motor on the table, rotation stabilizes. Shaft also seems very sensitive, I can stop rotation without any pressure just with touching shaft

aive:
I use two programs,

There is no point talking about programs if you don't post the code.

...R

Robin2:
There is no point talking about programs if you don’t post the code.

…R

Hello,

Same as in Post #11

int stpPin = 12;  
int dirPin = 13;  

int a = 0; 
void setup()
{
  pinMode(stpPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop()
{
  if (a < 200) //sweep 200 step in dir 1
  {
    a++;
    digitalWrite(stpPin, HIGH);
    delay(10);
    digitalWrite(stpPin, LOW);
    delay(10);
  }
  else
  {
    digitalWrite(dirPin, HIGH);
    a++;
    digitalWrite(stpPin, HIGH);
    delay(10);
    digitalWrite(stpPin, LOW);
    delay(10);
    
    if (a>400) //sweep 200 in dir 2
    {
      a = 0;
      digitalWrite(dirPin, LOW);
    }
  }
}

and

const int stepPin = 3;
const int dirPin = 4;

void setup() {
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
  
 digitalWrite(dirPin, HIGH);
 
 for(int x = 0; x < 200; x++) {
   digitalWrite(stepPin, HIGH);
   delayMicroseconds(500);
   digitalWrite(stepPin, LOW);
   delayMicroseconds(500);
 }

 delay(1000);

 digitalWrite(dirPin, LOW);

 for(int x = 0; x < 400; x++) {
   digitalWrite(stepPin, HIGH);
   delayMicroseconds(500);
   digitalWrite(stepPin, LOW);
   delayMicroseconds(500);
 }
 delay(1000);
}

aive:
Hello,

Same as in Post #11

As you posted that in Reply #8, the mind boggles.

...R

aive:
I use two programs, one rotates with 360 degree full turn in one direction, another with change of direction on 360 degree. I'm trying to figure out, why I can't stop movement by one or another way, depending on the load, even if I reset controller and load with Arduino IDE empty code, it performs the previous program,

I confess that even after looking at the two programs I am at a loss. Apart from the step and direction pins the programs are very similar and neither of them attempts to read incoming serial data.

If you are saying that you cannot replace one program with another then it helps if you print a message from setup() that identifies the program. That way you can be sure that the new program was loaded.

if the new program was not uploaded then you are likely to have received some error message in the Arduino IDE.

...R

So, you've made no changes to the code? And it still doesn't work?