Servo Problem

#include <Servo.h> 
Servo PitchServo;

void setup() {
 
  Serial.begin(9600);
  
 PitchServo.attach(9);
 PitchServo.write(20);
 delay(1000);
 PitchServo.write(160);
 delay(1000);
 PitchServo.write(90);
 delay(1000);
}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

   
    int pitchWert = Serial.read();
   PitchServo.write(pitchWert);
   
    }
  }

When I run this code, the servo does not respond to the data interface. He trembles only or moves randomly. The 3 positions at the beginning (20,160,90) work without error.

Via the interface I send an ASCII string by pressing a button,contains the numbers from 20 - 160.. My Port Monitor shoes, transmitted correctly the values.

The VB Code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "COM3"
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!


        SerialPort1.Open()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SerialPort1.Write(TextBox1.Text)
    End Sub

What am I doing wrong?

Peter
http://boefb.webnode.com/

Keyword is in: "I send an ASCII string". But in your code you expect to receive one value and you don't treat it as ACSIi but you just read the value.

Instead of sending a ASCII string try sending a single byte instead :wink:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SerialPort1.Write(TextBox1.Text)
    End Sub

Why are you sending text? Convert the text to a byte, and send the byte. Making sure that the serial port is open before trying to write to it would be a good thing.

The port is of course open. This is already stated in the Form_Load.
I send the value now as a byte. This works exactly once. Other values are not executed by the servo.

Peter
http://boefb.webnode.com/

Post your new code

Imports System.IO.Ports
Public Class Form1

    Public ComWert As Byte
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "COM3"
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
        SerialPort1.Open()


    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ComWert = TextBox1.Text
        SerialPort1.Write(ComWert)

    End Sub

    
End Class

The port is of course open.

That is NOT a valid assumption. That you TRIED to open the port IS a valid statement. That it succeeded is the invalid assumption. You should ALWAYS check such assumptions before they bite you in the posterior.

Im not overly familiar with VB, but unless that assignment operator is taking your ascii value and converting it (Idoubt it), it won't work.

Try something like this, and keep to values of 0-255 in your textbox:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ComWert = Convert.toByte(TextBox1.Text)
        SerialPort1.Write(ComWert)

End Sub

but unless that assignment operator is taking your ascii value and converting it (Idoubt it)

It is not.

You should ALWAYS check such assumptions before they bite you in the posterior.

I will check that with a port sniffer, as I also mentioned in the first post.

My Port Monitor shoes, transmitted correctly the values

ComWert = Convert.toByte(TextBox1.Text)
Does not work well. Or as well as

Public ComWert As Byte

I'm not sure what causes this line to the port declaration.

SerialPort1.Encoding = System.Text.Encoding.Default

That's already a recoding in bytes?

ComWert = Convert.toByte(TextBox1.Text)

Does not work well. Or as well as

Public ComWert As Byte

You're going to have to help me out here, tell me if Im wrong, but isn't that just declaring that variable as a byte? Then when you straight up assign it a value from a String it's just going to give you the ascii value for the number 5 for example?

And why does the Convert.ToByte not work well? That should ensure the byte contains the numerical equivalent of the ascii numerical character.

I apologize. I can not express myself well in English unfortunately. That was not a criticism.

I will check that with a port sniffer, as I also mentioned in the first post.

By which time it will be too late.

Do it right:

if(SerialPort1.IsOpen)
   SerialPort1.Write(whatever)

That's already a recoding in bytes?

Yes, the characters that make up the string are encoded as bytes. That is NOT what you want. You do NOT want to send the value as a string.

When you use the useless phrase "it doesn't work", we have NOTHING to go on to help you. Post the code you are using, and explain EXACTLY (as near as you can, anyway) what happens.

You CAN use Serial.print() on the Arduino end to send data to the VB app. What does the Arduino actually read from the serial port?

Try this, not ideal, but what is?:

while (Serial.available() > 0) 
{ 
	char ascii_val[2];
	ascii_val[0] = Serial.read();
	ascii_val[1] = 0;
	int numberical_val = atoi( ascii_val );
	PitchServo.write(numberical_val);   
}

Try this, not ideal, but what is?:

What range of values is that going to convert to an int? 0 to 9. Why bother using atoi() and an array, when

int numberical_val = Serial.read() - '0';

accomplishes EXACTLY the same thing?

Last I checked, a char (byte) could hold 255 values.

Edit:

Last time I should of checked however, was before hitting the submit button, but I will stand by my use of atoi(), purely for preempting what comes next, the "A byte only gives me 9 values to set my server to, I can potentially send 0-180 in, I want to send an ascii string representing the full range in"

but I will stand by my use of atoi(), purely for preempting what comes next, the "A byte only gives me 9 values to set my server to, I can potentially send 0-180 in, I want to send an ascii string representing the full range in"

As a character, the only values that atoi() recognizes are '0' to '9'. While a byte can hold more than that, ONLY '0' to '9' will cause atoi() to continue processing the array. Since the array ONLY holds two values, and the second is a NULL, the only values that numberical_val can ever be assigned, USING THAT CODE, are 0 to 9.

Thanks to all for the quick and patient assistance. I have now solved with an external software that provides both the Arduino sketch and the control software.