C# 2 Servo Motor Control

Hi everyone,
I am a beginner with Arduino and code.
I am Turkish, sorry for my english.

I’m making servo control (for robot arm) with c# for now. However, there are code that runs the Arduino and C # code. How can I set up 2 Servo Motors?

Arduino Code:

#include <Servo.h>

Servo servoMotor;
int servoPin=13;
int value=0;
int angle;
int x;

void setup(){
  Serial.begin(9600);
  servoMotor.attach(servoPin);
}

void loop(){
  if(Serial.available() >0) 
  {
    value = Serial.read();
    if(value>0){
      x=value;
    }
  }
  else if (x>0 && x<181){
    angle=x+20;
    servoMotor.write(angle); 
  }  }

C# Kodu:

private void trackBar1_body_Scroll(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == true)
            {
                int PWM_deger = trackBar1_body.Value;
                label1.Text = trackBar1_body.Value.ToString();
                b = BitConverter.GetBytes(PWM_deger);
                serialPort1.Write(b, 0, 4);
            }
            else MessageBox.Show("Connect to Device first !!");
            
        }

I’m waiting for your help.

You just need to create 2 object for 2 motor.
example:servo servo1;
servo servo2;

AshwiniGunjal:
You just need to create 2 object for 2 motor.
example:servo servo1;
servo servo2;

Thank you for your information. However, I want to manage 4 servomotors with C #. How can i make it.

#include<servo.h>
servo servo1;
servo servo2;

void setup(){

serial.begin(9600);
servo1.attach(pin no);
servo2.attach(pin no);

}

void loop(){

servo1.write(angle);
servo2.write(angle);

}

I don’t know in c# but I know in arduino language.

AshwiniGunjal:

#include<servo.h>

servo servo1;
servo servo2;

void setup(){

serial.begin(9600);
servo1.attach(pin no);
servo2.attach(pin no);

}

void loop(){

servo1.write(angle);
servo2.write(angle);

}




I don't know in c# but I know in arduino language.

Thank you for your answer. But how can I check this pin number from C# ?

But how can I check this pin number from C# ?

The C# app should know NOTHING about the hardware on the Arduino side.

You simply send 4 values that represent the positions for the servos, in some order, and the Arduino applies the values to the servos in the proper order.

PaulS:
The C# app should know NOTHING about the hardware on the Arduino side.

You simply send 4 values that represent the positions for the servos, in some order, and the Arduino applies the values to the servos in the proper order.

Thank you very much for your information.
I have no idea how to do it. Could you help ?

I have no idea how to do it. Could you help ?

Sure, I know how to do "it". I've been married 34 years.

I'm sure that you meant something else, though. At least, I hope you did.

So, what is "it" that you need help with? Once your C# app has opened the serial port, port.Write(someString) is not exactly rocket science.

With a properly formulated string, and Robin2's tutorial on reading serial data, getting the Arduino to read the string, and parse it into 4 position values is trivial.

Using Servo::write() to send the appropriate position value to the appropriate servo is simple.

PaulS:
Sure, I know how to do "it". I've been married 34 years.

I'm sure that you meant something else, though. At least, I hope you did.

So, what is "it" that you need help with? Once your C# app has opened the serial port, port.Write(someString) is not exactly rocket science.

With a properly formulated string, and Robin2's tutorial on reading serial data, getting the Arduino to read the string, and parse it into 4 position values is trivial.
http://forum.arduino.cc/index.php?topic=396450.0

Using Servo::write() to send the appropriate position value to the appropriate servo is simple.

Thank you very much.
However, I did not get a complete result because of my work.
Can you help me please?

Can you help me please?

Sure. Send me your computer.

Or POST YOUR CODE!

PaulS:
Sure. Send me your computer.

Or POST YOUR CODE!

Really, but very thank you
I communicated to you via a private message. I expect you to answer.

I communicated to you via a private message. I expect you to answer.

You have unrealistic expectations.

PaulS:
You have unrealistic expectations.

I do not understand anything you said sorry.

PaulS:
You have unrealistic expectations.

Would you please help me

Would you please help me

No. I've tried. I've asked you to post your code. You've refused.

PaulS:
No. I've tried. I've asked you to post your code. You've refused.

I sent the code from the private message.
I can send it from here

http://s3.dosya.tc/server11/5c5kkd/Arduino.rar.html

I sent the code from the private message.

And expected me to immediately fix it. THAT was unreasonable, as I mentioned.

Post your code HERE. Do not send me any more PMs with demands that I solve your problems immediately.

PaulS:
And expected me to immediately fix it. THAT was unreasonable, as I mentioned.

Post your code HERE. Do not send me any more PMs with demands that I solve your problems immediately.

I understand you. This is the code.

ARDUINO CODE:

#include <Servo.h>

Servo servoMotor;
int servoPin=13;
int value=0;
int angle;
int x;

void setup(){
Serial.begin(9600);
servoMotor.attach(servoPin);
}

void loop(){
if(Serial.available() >0)
{
value = Serial.read();
if(value>0){
x=value;
}
}
else if (x>0 && x<181){
angle=x+20;
servoMotor.write(angle);
} }

C# CODE:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < System.IO.Ports.SerialPort.GetPortNames().Length; i++)
{
comboBox1.Items.Add(System.IO.Ports.SerialPort.GetPortNames());

  • }*

  • trackBar1_middleservo.Visible = false;*

  • trackBar2_leftServo.Visible = false;*

  • trackBar3_rightServo.Visible = false;*

  • trackBar4_HandServo.Visible = false;*

  • }*

  • private void button1_Click(object sender, EventArgs e)*

  • { *

  • try*

  • {*

  • serialPort1.PortName = comboBox1.Text;*

  • if (!serialPort1.IsOpen) serialPort1.Open();*

  • }*

  • catch*

  • {*

  • MesrighteBox.Show(“PPort Connected…!”);*

  • trackBar1_middleservo.Visible = true;*

  • trackBar2_leftServo.Visible = true;*

  • trackBar3_rightServo.Visible = true;*

  • trackBar4_HandServo.Visible = true;*

  • }*

  • }*

  • private void trackBar1_middleservo_Scroll(object sender, EventArgs e)*

  • {*

  • if (serialPort1.IsOpen == true)*

  • {*

  • int PWM_value = trackBar1_middleservo.Value;*

  • label1.Text = trackBar1_middleservo.Value.ToString();*

  • byte b = BitConverter.GetBytes(PWM_value);*

  • serialPort1.Write(b, 0, 4);*

  • }*

  • else MesrighteBox.Show(“Connect to Device first !!”);*

  • }*

  • private void trackBar2_leftServo_Scroll(object sender, EventArgs e)*

  • {*

  • }*

  • private void trackBar3_rightServo_Scroll(object sender, EventArgs e)*

  • {*

  • }*

  • private void trackBar4_HandServo_Scroll(object sender, EventArgs e)*

  • {*

  • }*

  • }*
    }

You still have not posted your code correctly. The <> icon makes it stupidly simple to do so.

Your Arduino code has an if/else if set of statements where the two conditions bear no relationship to each other. If there is serial data to read, do something. Else, if the value in x is between 1 and 180, do something else.

I see no relationship between the if and the else if. So, it is difficult to believe that the else if statement is appropriate.

I am certain that your C# code is not half in italics. But, since what we see is, we have to guess what it was supposed to look like. I don’t care for guessing games AT ALL.

As you move the slider, the Value property of the trackBar1_middleservo object changes. You get that value as an int, and then convert the int to an array of bytes, and send the 4 bytes in the array to the serial port.

On the Arduino end, then, you expect that each of the 4 bytes will be a position for the servo to move to. That is NOT a realistic expectation AT ALL.

Store the value of the Value property in a variable whose type is byte. Shitcan the code to convert the byte to an array of bytes. Send the ONE byte to the serial port.

PaulS:
You still have not posted your code correctly. The <> icon makes it stupidly simple to do so.

Your Arduino code has an if/else if set of statements where the two conditions bear no relationship to each other. If there is serial data to read, do something. Else, if the value in x is between 1 and 180, do something else.

I see no relationship between the if and the else if. So, it is difficult to believe that the else if statement is appropriate.

I am certain that your C# code is not half in italics. But, since what we see is, we have to guess what it was supposed to look like. I don’t care for guessing games AT ALL.

As you move the slider, the Value property of the trackBar1_middleservo object changes. You get that value as an int, and then convert the int to an array of bytes, and send the 4 bytes in the array to the serial port.

On the Arduino end, then, you expect that each of the 4 bytes will be a position for the servo to move to. That is NOT a realistic expectation AT ALL.

Store the value of the Value property in a variable whose type is byte. Shitcan the code to convert the byte to an array of bytes. Send the ONE byte to the serial port.

This is all the code available.
Thank you for the information. However, I still do not know how to do it. Please help