Go Down

Topic: Control Servo With VB 2010 Problem (Read 619 times) previous topic - next topic

mamette

Mar 27, 2013, 04:47 pm Last Edit: Mar 27, 2013, 04:49 pm by mamette Reason: 1
Hi, i want to make servo position control with VB 2010. For first try, i want to control servo between 0 - 100 degrees. In VB 2010 i use trackbar to control servo, and i setting the trackbar value between 0 and 10. So, 1 trackbar value represent 10 degree of servo movement. When i tried the system, it works, but there is a problem, the system will works well if the trackbar value ranged between 0 - 9. When i move trackbar value to 10, the servo must be move to 100 degree, but it goes wrong, the servo move to 0 degree, looks like arduino read the trackbar value is 0 not 10.

Then i do other experiment, and the conclusion is arduino only execute trackbar value between 0 and 9. When it between 10-19, or 20 and 29, and so on, arduino will read it as 0 - 9.

Well, how to solve this problem?
Thank you

this is my Arduino Code:
Code: [Select]
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int val,pos;

void setup()
{
 myservo.attach(A0);  // attaches the servo on pin 9 to the servo object
 Serial.begin(115200); //begins serial communication
}
 
void loop()
{
 serialControl();
 myservo.write(val);
}

void serialControl() {
 if (Serial.available()){
   delay(100);
   while(Serial.available()>0){
   pos=int(Serial.read());     //reads the value sent from Visual Basic  
     if(pos=='0')
       val=0;   //rotates the servo 0 degrees
     else if(pos=='1')
       val=10;
     else if(pos=='2')
       val=20;  
     else if(pos=='3')
       val=30;  
     else if(pos=='4')
       val=40;  
     else if(pos=='5')
       val=50;  
     else if(pos=='6')
       val=60;  
     else if(pos=='7')
       val=70;  
     else if(pos=='8')
       val=80;    
     else if(pos=='9')
       val=90;  
     else if(pos== '10')
       val=100;
       
   }
 }
 
}


And this is my VB 2010 Code:
Code: [Select]


Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1


   Shared _continue As Boolean
   Shared _serialPort As SerialPort

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       SerialPort1.Close()
       SerialPort1.PortName = "com18" 'you need to check which com port your arduino is using, and change them if you need
       SerialPort1.BaudRate = 115200
       SerialPort1.DataBits = 8
       SerialPort1.Parity = Parity.None
       SerialPort1.StopBits = StopBits.One
       SerialPort1.Handshake = Handshake.None
       SerialPort1.Encoding = System.Text.Encoding.Default
   End Sub
 
   Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles Label1.Click

   End Sub


   Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
       SerialPort1.Open()
       SerialPort1.Write(TrackBar1.Value)
       SerialPort1.Close()
       TextBox1.Text = TrackBar1.Value
   End Sub

   Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

   End Sub

End Class

AWOL

#1
Mar 27, 2013, 04:59 pm Last Edit: Mar 27, 2013, 05:02 pm by AWOL Reason: 1
Code: [Select]
else if(pos== '10')
You can't transmit and receive two characters like that - you need to form them into a string.
This question comes up every other day here in some form or another.

The problem is, your code receives a '1', moves the servo to 10 degrees,then very shortly after (less time than a servo frame), it receives the '0' and moves the servo to 0 degrees.
"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.

PaulS

If you have a slider, you are converting the slider value to a string to send to the Arduino. Stop doing that. Send the value as a (binary) value. Then, you can read the servo position as a single value (from 0 to 255).

Go Up