Servo control over serial help!!!

I have been trying to figure this out for months now and I have looked everywhere but still can’t find the answer. I am using a vex servo connected to pin 9 on my arduino, it works and I have tested it but the code does not seem to work correctly at all.

The idea is to send a value 0 to 255 from vb.net (in this case lets just say the arduino serial terminal) to the micro controler and then the controller will check to see if serial is available if it is then it will change the servo position to the value you sent.

This code works but basically it only has fully forward and backward posistions and you have to program it in.

#include <Servo.h>
Servo myservo;
int val;
void setup()
{
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
if (Serial.available()) {

val = Serial.read();

if (val == ‘F’) {

myservo.write(255);
}

if (val == ‘B’) {

myservo.write(0);

}
}
}

This is the way I want the code to be but this code doesn’t work, can anyone help me fix the below code so that it works?

#include <Servo.h>
Servo myservo;
int val;
void setup()
{
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
if (Serial.available()) {

val = Serial.read();

myservo.write(val);

}
}

Any help will be GREATLY appreciated, I have been trying to figure this out for a very long time!!!

but this code doesn’t work

This may sound a stupid question, but in what way doesn’t it work?

Are you sure you’re sending a binary value from VB, an not a string representing a number?

Assuming the vex servo expects a pulse width between 1 and 2 milliseconds, and assuming you are using the standard Arduino servo library, the following code should do what you want

#include <Servo.h> 

Servo myservo;

int val;

void setup()

{
 Serial.begin(9600);
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
}

void loop()
{
 if (Serial.available()) {
   
   val = Serial.read();
   int pos = map(val,0,255,0,180); // this sets the range of values written to the servo from 0 to 180
   myservo.write(pos);  
     
   }
 }

In vb.net I am sending a byte like this.

Dim value as byte

value = val(txtBox.text)

SerialPort1.Write(value)

I am not sure if this code is correct as you said binary and I am confused as to what you mean. Sample code would be helpful if possible.

Its been a while since I have used vb but if your text box contains values from 0 to 180 then the code you posted should work.

Are you using the Arduino distributed servo library? Does anything happen when you run the code?

The code on the micro controller end uploads fine but it doesn't work right. If I send letters from the arduino serial terminal the servo moves very small amounts while numbers make it jam up. If I send a byte value of o to 180 from vb.net the servo goes all the way forward and won't budge even if I send more bytes of data.

Try your code using the serial monitor, sending the character ! should send it almost to one end, and } almost the other.

Yes, that is working. It is probably using there ascii value from the ascii table.

http://www.arduino.cc/en/Reference/ASCIIchart

Now my only question is how to get the arduino to change the incoming serial data to the number not the ascii values. It might also be on the vb.net side as arduino allows you to format the output as a decimal and more. I have been able to send a number like this from vb.net and have arduino send it back as a number not the ascii values by formatting the output as DEC. Now the only question is how do I do output a decimal in vb.net???