Pages: [1]   Go Down
Author Topic: Control Servo With VB 2010 Problem  (Read 522 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:

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
« Last Edit: March 27, 2013, 10:49:44 am by mamette » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26202
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
« Last Edit: March 27, 2013, 11:02:51 am by AWOL » Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Pages: [1]   Go Up
Jump to: