serial differentiating?

I'm trying to control two servos separately with two separate sliders over serial in a C Basic program, but what i'm wondering is how to allow the Arduino to differentiate between what the position of one slider should be from the other. I think I could specify in the arduino such as for the first servo the values from 0-180 like this: 1a, 2a, 3a, 4a, 5a, etc. And then for servo 2: 1b, 2b, 3b, 4b; and then when it sees the string such as 4b it moves servo two to the 4 degree position. Is this possible? And does anybody have any better suggestions? (please come up with a better suggestion) Any Help is greatly appreciated.

You could send a servo identifier with the servo position from a slider program. The servo code could detect the identifier and set the servo position. Start developing the arduino code using the serial monitor. Below is some servo slider info for another chip.

http://www.rentron.com/Mini-ssc.htm

I'm sorry to be kind of annoying about this but when you say

You could send a servo identifier with the servo position from a slider program.

could you maybe write a short example or point to one, I'm still kind of new to all of this and thanks for the information.

If you want to limit yourrself to 127 points mapped to 180 you can not use the ascii value and send the value with the msb being a 1 for servo 1 or 0 for servo 2, just making sure to store the value in an unsigned variable and losing the msb after selecting which servo

could you maybe write a short example or point to one, I'm still kind of new to all of this and thanks for the information.

I suggest you start with using the serial monitor to develop your arduino code. This will involve several steps where a position string for a servo is received, like "a150," or "b50," (also could be a character and byte, or two bytes), which is captured, evaluated for the first servo identifying character, convert the rest of the characters to a number, then the number is used to set that particular servo's position. There are probably a lot of different ways, but something like this should be fairly simple to start with. Below is a very simple way to capture a string of data.

// zoomkat 7-18-11 serial I/O string test
// type a string in serial monitor followed by a ,
// then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021 delimit"); // so I can keep track of what is loaded
}

void loop() {

  if (Serial.available()) {
    char c = Serial.read();

    if (c == ',')
    {
      Serial.println(readString);
      readString="";
    }
    readString += c; 
  }
}

Dnny44: I'm trying to control two servos separately with two separate sliders over serial in a C Basic program, but what i'm wondering is how to allow the Arduino to differentiate between what the position of one slider should be from the other. I think I could specify in the arduino such as for the first servo the values from 0-180 like this: 1a, 2a, 3a, 4a, 5a, etc. And then for servo 2: 1b, 2b, 3b, 4b; and then when it sees the string such as 4b it moves servo two to the 4 degree position. Is this possible? And does anybody have any better suggestions? (please come up with a better suggestion) Any Help is greatly appreciated.

Why do you need a better suggestion? You might put the code for what servo before the value and then again the way you have it is just fine too.

You seem to have gotten the business of abstracting one thing (movement of the sliders) to make signals to affect other things (the servos). You have gotten it about sending the abstracted info from the PC to the Arduino. Are you having problems making a servo move? Or reading serial at the Arduino end? Or getting the ASCII from the PC into integer or float variables?

There's lots of ways to do what you want but in the Arduino, stay away from C++ Strings okay? They're fat and -every time- you change one it allocates space for the new String out of your very limited SRAM. Use character arrays instead when you have to and leave them out when you don't. Example: you can buffer characters for a number coming in over serial and then convert -or- you can convert the characters to a number in a variable as they come in. One way uses a buffer and the other way is faster.

if (Serial.available() > 0) {

int val = Serial.read(); // read the incoming byte: switch(val) {

case 'a' : pulseWidth1 = Serial.read(); break ; case 'center1' : pulseWidth1 = centerServo1; break; case 'center2' : pulseWidth2 = centerServo1; break; case 's' : pulseWidth2 = Serial.read(); break ; defualt : pulseWidth1 = 90; pulseWidth2 = 90; break;

This is the code I'm working with but I think, as someone said above, the value being sent from the Basic program isn't the same as the 0-180 I need the board to see to set the position of the servo. Right now whenever I move the slider it sends an "a" to the arduino followed by the value of the slider. How can I translate the values to be read correctly by the arduino.

Also I still don't quite understand how to read part of something like "a120" and read "a" as one character and then convert the rest of the string into a number.

zoomkat: I suggest you start with using the serial monitor to develop your arduino code. This will involve several steps where a position string for a servo is received, like "a150," or "b50," (also could be a character and byte, or two bytes), which is captured, evaluated for the first servo identifying character, convert the rest of the characters to a number, then the number is used to set that particular servo's position. There are probably a lot of different ways, but something like this should be fairly simple to start with. Below is a very simple way to capture a string of data.

// zoomkat 7-18-11 serial I/O string test
// type a string in serial monitor followed by a ,
// then send or enter
// for IDE 0019 and later

String readString;

void setup() {   Serial.begin(9600);   Serial.println("serial test 0021 delimit"); // so I can keep track of what is loaded }

void loop() {

  if (Serial.available()) {     char c = Serial.read();

    if (c == ',')     {       Serial.println(readString);       readString="";     }     readString += c;   } }

Also I still don’t quite understand how to read part of something like “a120” and read “a” as one character and then convert the rest of the string into a number.

Study the below stringobjects for fairly easy string parsing. Bottom is some servo code that uses the substring function to capture the desired part of a longer captured string for a servo position.

http://arduino.cc/en/Reference/StringObject

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();
      
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
  } 
}

Code in quote tags doesn’t display brackets right. Yes the IDE has “copy for forum” but it’s crap that I have yet to figure out why they don’t either fix or remove it. The way that works and shows the whole code is to highlight and copy the IDE code then copy to your post and wrap code tags around it. On the forum in the Post editor there’s a button with a # sign that makes the html code tags.

However with the part below I can see one three errors.

errors 1 & 2) switch is on the int val. You have 2 cases where case is nonsense. WTH is ‘center1’ and ‘center2’? You read 1 8-bit character into val, are center1 and center2 supposed to be constants or defines? Those single quotes do not belong either. ‘A’ is an 8-bit value = 65. ‘center1’ is nonsense. “center1” is an 8-byte string when you include the terminating zero that C puts there.

error 3) While you do check Serial.available() > 0 before the first read you don’t check it again before any other reads. Even at 115200 baud your code is much faster than serial characters arrive. If the data arrives well before that routine then hey it should work but still you should not just read without checking. Even something like while (Serial.available() < 1); before the next read is better though if the text stops, your code hangs it’s better than a delay though no less blocking.

if (Serial.available() > 0) {

int val = Serial.read(); // read the incoming byte:
switch(val) {

case ‘a’ :
pulseWidth1 = Serial.read(); break ;
case ‘center1’ :
pulseWidth1 = centerServo1; break;
case ‘center2’ :
pulseWidth2 = centerServo1; break;
case ‘s’ :
pulseWidth2 = Serial.read(); break ;
defualt :
pulseWidth1 = 90;
pulseWidth2 = 90;
break;

winner10920: If you want to limit yourrself to 127 points mapped to 180 you can not use the ascii value and send the value with the msb being a 1 for servo 1 or 0 for servo 2, just making sure to store the value in an unsigned variable and losing the msb after selecting which servo

I think this is my problem but I don't entirely understand. Right now my C-Basic program has two sliders, and there code to send data over serial is: Servo A

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

Servo B

Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll
        SerialPort1.Open()
        SerialPort1.Write("s")
        SerialPort1.Write(TrackBar2.Value)
        SerialPort1.Close()
        TextBox2.Text = TrackBar2.Value
    End Sub

And this is the Arduino code that goes with it:

// check for serial input
  if (Serial.available() > 0) {

    int val = Serial.read();   // read the incoming byte:
    switch(val) {
        
    case 'a' :  
            pulseWidth1 = Serial.read();  break ; 
    case 's' :  
            pulseWidth2 = Serial.read();  break ;

I think he is right and all I need to do is to convert the value from the slider to a value Arduino can understand however I have no idea how to accomplish this. Any help is greatly appreciated.