VB motors and servo

Hi all

I have project that controlled a remote car using VB connected to arduino Via bluetooth

I have a trackbar to give the value to the servo

And i have a button to give the PWM value to the motor

this is the code that i used for the servo (ps: i take this code from "Control servo with Visual Basic 2008 - Interfacing - Arduino Forum" i tried it and it works)

#include <Servo.h>

Servo myservo;

int pos=0;

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

void loop()
{
 if(Serial.available() > 0)
 {
   char aChar = Serial.read();
   if(aChar == '<')
   {
     pos = 0;
     do{
         while(Serial.available() <1)
           ; // wait for a character
       aChar = Serial.read();    
       if(aChar >= '0' && aChar <= '9')              // is ch a number?  
         pos = pos * 10 + aChar - '0';           // yes, accumulate the value
     }
     while(aChar != '>');
     myservo.write(pos);
     pos = 0;
   }
 }
}

And the VB code

    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        Label8.Text = String.Format("Degree: {0}", TrackBar1.Value)

        If SerialPort1.IsOpen = True Then
            SerialPort1.Write("<")
            SerialPort1.Write(TrackBar1.Value.ToString)
            SerialPort1.Write(">")
        End If

    End Sub

I dont know the part of this code

char aChar = Serial.read();
   if(aChar == '<')
   {
     pos = 0;
     do{
         while(Serial.available() <1)
           ; // wait for a character
       aChar = Serial.read();    
       if(aChar >= '0' && aChar <= '9')              // is ch a number?  
         pos = pos * 10 + aChar - '0';           // yes, accumulate the value
     }

can you explain to me?

and is it possible to add PWM value using button (if i press the button twice the value increasing by 20)?

Im confuse about how to parse the data between servo data from Trackbar and PWM data from button.
I want to send data as a packet and my arduino detect the value from each trackbar value to servo or button value to PWM motor.

Thank you!!!

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

If you get your VB code to send the data in the format of the 3rd example the whole thing will be more reliable.

...R

to increment the trackbar position by 20 when a button is pressed you can add a button click event, e.g.

Public Class Form1
    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        TextBox1.Text = String.Format("Degree: {0}", TrackBar1.Value)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TrackBar1.Value = TrackBar1.Value + 20
        TextBox1.Text = String.Format("Degree: {0}", TrackBar1.Value)
    End Sub
End Class

when the button is pressed the trackbar value is read, incremented by 20 and written back
note that no check is performed when incrementing that the resultant trackbar value does not exceed the maximum value