Go Down

Topic: Control servo with Visual Basic 2008 (Read 4 times) previous topic - next topic

Deco Aoreste

Hello,

I want to control a simple servo with Visual Basic 2008. I want to use a trackbar with 180 points. The servo is attached to digital pin 9. I tried this 2 simple codes:

Visual Basic 2008 code:
Code: [Select]
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
       If RadioButton2.Checked = True Then
           SerialPort1.Write(TrackBar1.Value)
       End If
   End Sub


Arduino code:
Code: [Select]
#include <Servo.h>

Servo myservo;

int SerialValue = 0;

void setup()
{
 myservo.attach(9);
 Serial.begin(9600);
}


void loop() {
 SerialValue = Serial.read();
   myservo.write(SerialValue);
   delay(15);
 }


...But it doesn't work. The servo moves somewhat, but not to the right angle, not even close. It just moves like it's confused and doesn't know what to do.

Does anyone know a solution to this?

Many thanks,
Deco Aoreste

PaulS

In the Arduino code, loop executes in an endless loop, potentially millions of times a second. During each iteration, it will (try to) read a byte from the serial port, and send the servo there, then wait 15 milliseconds for the servo to get there.

You are not checking to see if there is serial data to read, nor are you checking that the value read is reasonable.

If there is no data to be read, Serial.read returns -255. That is probably not an angle that the servo can rotate to.

Add a call to Serial.available(), and only read from the serial port if there is data to read. Only move the servo if the value read from the port is reasonable.

Ran Talbott

Quote
But it doesn't work.


Sure it does.  It just doesn't work the way you think it does  :)

The Arduino's read() doesn't work the same way as VB's:  it only gets one character at a time.  You need to accumulate the characters into a string,  and convert that to a number with a function like http://atoi().  Or you can convert the characters "on-the-fly" by translating each digit as it comes in,  and accumulating the value.

The examples on the reference page for the Serial library don't seem to show those,  but you can find examples posted in the forums.

Deco Aoreste

Thanks for replying.

I tried the things that you said, but it still doesn't work. Please note that I'm a beginner, this is one of my first projects.

So this is my code now:
Code: [Select]
#include <Servo.h>

Servo myservo;

int SerialValue = 0;
char incomingByte;
int outputValue;

void setup()
{
 myservo.attach(9);
 Serial.begin(9600);
}


void loop() {
 if (Serial.available() > 0) {
   SerialValue = Serial.read();
   outputValue = atoi(incomingByte);
   myservo.write(outputValue);
   delay(15);
 }
}


I get an error at this line:
Code: [Select]
outputValue = atoi(incomingByte);
I get this error:

error: invalid conversion from 'char' to 'const char*

What should I do to make it work?

Thanks,
Deco Aoreste

AWOL

You're reading a single "char", and passing it to "atoi", which expects a string (char*).

However, a single decimal digit will result in only very limited movement of the servo.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up