Aaron Decker
Aaron Decker

Reputation: 558

C# serial command to move Arduino servo X degrees?

I have an Arduino wired up with a servo (Pin 9, 5.5v and Ground), it will run with any ol' testing on the Arduino; however, when I send a serial command to move it, well nothing happens. The rx light flashes so I know the Arduino is getting the info. I think the issue is in my byte conversion.

Code Time:

Arduino Code:

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
void setup() 
{ 
    myservo.attach(9); 
    // attaches the servo on pin 9 to the servo object and sets the rotation to 0
    myservo.write(0); 
} 

int pos1= 0;
int pos2= 0;
int pos3= 0;
int totalMove = 0;

void loop() 
{ 
    if (Serial.available() > 0 && totalMove > 0)
    {
        pos1 = Serial.read() - '0'; 
        // pos2 = Serial.read() - '0'; 
        // pos3 = Serial.read() - '0'; 
        // totalMove = ((pos3) + (pos2*10) + pos1*100);

        myservo.write(pos1);
     }
} 

You see the other pos holders because evenutally I would like to be able to send values larger than 9, however for now I just need to get it to respond :)

C# code:

public void moveServo()
{
    if (!serialPort1.IsOpen)
    {
        Console.WriteLine("Oops");
        serialPort1.Open();
        return;
    }

    serialPort1.DtrEnable = true;

    serialPort1.DataReceived += 
        new System.IO.Ports.SerialDataReceivedEventHandler(
            serialPort1_DataReceived);

    serialPort1.Write(new byte[] {57}, 0, 1);
}

Any ideas?

Upvotes: 1

Views: 4011

Answers (3)

radmehr
radmehr

Reputation: 1

maybe it's because of your logic level.

the lpt and serial port output is 2.5v and some driver's need 5v to set and reset.

so you need an ic like max232 to convert logic level from 2.5v to 5volt.

Upvotes: 0

A.H.
A.H.

Reputation: 66263

You are sending this byte

serialPort1.Write(new byte[] {57}, 0, 1);

which basically is the character '9'. The receiver code is

pos1 = Serial.read() - '0'; 

which means that pos1 has the value 9 (note the missing '). This value is then written directly to the Servo instance.

myservo.write(pos1);

Summing together all parts: You can effectively send only the values 0 to 9 to the servo. But the reference page tells you that write requires the range 0 to 180. Sending only 0 to 9 to the servo might just wiggle it a little bit.

Upvotes: 1

jackjumper
jackjumper

Reputation: 163

Have you made sure it works using other software? That's typically the first step, even if you have to use hyperterminal. That'll shake out any cable problems and give you the correct parameters.

Also I recommend PortMon, from SysInternals. It lets you monitor serial port activity while your application runs.

Make sure you're setting all the serial port parameters; baud rate, data bits, stop bits, parity, handshake, and read and write timeout. You can also set the value to use for a NewLine character.

Also, rather than relying on the data recieved event, you might try reading it yourself.

Upvotes: 1

Related Questions