#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
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.
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
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
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.
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?
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.