Using Python to select which servo to control?

Just need a little bit of help on this one. I have a GUI set up in python that feeds the data of a slider back into the Arduino to move the servo, which is working fine. The issue I'm facing is I'm not too sure how to add multiple servo's and have it so the sliders will only control one particular servo e.g Slider 1 controls the base servo and slider 2 controls the arm servo. I originally thought on having python send a specific number to the Arduino when a specific slider was moved , which would then select the servo with something like "if (servo == 1)" where I would put the code for the servo underneath, but all of my attempts have failed so far. Any ideas?

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.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this, or its equivalent in Python.

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

If you always send the data for the servos in the same order the Arduino can easily identify which is which.

…R
Python - Arduino demo

Robin2:
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.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this, or its equivalent in Python.

Serial.print('<'); // start marker

Serial.print(value1);
Serial.print(’,’); // comma separator
Serial.print(value2);
Serial.println(’>’); // end marker




If you always send the data for the servos in the same order the Arduino can easily identify which is which.

...R
[Python - Arduino demo](http://forum.arduino.cc/index.php?topic=225329.msg1810764#msg1810764)

Cheers for this but I seem to be having an issue with ‘showNewData’ and my servo function ‘servo’ in void loop. I keep getting ‘showNewData’ and ‘servo’ was not declared in this scope.

void loop()
{
  recvWithStartEndMarkers();
  showNewData();
  servo();
}

Not sure why that isn’t working as I followed what was written.

Not sure why that isn't working as I followed what was written.

There is nothing wrong with that snippet. It must be in the code you didn't post.

cannar:
Not sure why that isn't working as I followed what was written.

Post the complete program that YOU have uploaded to your Arduino.

...R

Here’s the complete code.

#include <Servo.h>
Servo myservo1;   
Servo myservo2;   
String inByte;
int pos;
const byte numpos = 32;
int receievedpos[numpos];

boolean newData = false;

void setup() {
 
  myservo1.attach(6);
  myservo2.attach(10);
  Serial.begin(9600);
}

void loop()
{
  recvWithStartEndMarkers();
  showNewData();
  servo();
}

void recvWithStartEndMarkers(){
  static boolean recvInProgress = false;
  static byte ndx = 0;
  pos startMarker = '<'
  pos endMarker = '>'
  pos inByte;
  
  
  if(Serial.available()> 0 && newData == false)  // if data available in serial port
    { 
    if (recvInProgress == true){
      if (inByte != endMarker){
        receivedpos[ndx] = rc;
        ndx++;
        if (ndx >= numpos){
          ndx = numpos -1;
        }
      }
    else {
      receivedpos[ndx] = '\0';
      recvInProgress = false;
      ndx = 0;
      newData = true;
    }

    else if (inByte == startMarker){
      recvInProgress = true;
    }

void showNewData(){
  if (newData == true){
    serial.print("This just in...");
    serial.printIn(recievedpos);
    newData = false;
  }
}
    
void servo(){
  if Serial.available(){
  
    inByte = Serial.readStringUntil('\n'); // read data until newline
    pos = inByte.toInt();   // change datatype from string to integer    
      if (startMarker == 'A'){
           
        myservo1.write(pos);     // move servo
        Serial.print("Servo1 in position: ");  
        Serial.println(inByte);

      }
       if (startMarker == 'B')
        {       
        myservo2.write(pos);     // move servo
        Serial.print("Servo2 in position: ");  
        Serial.println(inByte);    
        }
  }
}

I’ve tried to incorporate the code from the example that Robin gave with my existing code… so it’s probably wrong but I tried :stuck_out_tongue_closed_eyes:

You have got something mixed up when you created the function recvWithStartEndMarkers(). There are missing closing }

Copy and paste the code from my tutorial rather than re-typing it.

Because of the missing } the compiler does not know where the function ends and cannot detect the other functions.

...R

You sent me a PM asking "how to change the code you supplied to accept string instead of char". I don't give advice privately as that prevents other people from learning along with you.

The short answer is don't use the String class on an Arduino. It can cause memory corruption. It does not matter what your Python code calls it when it is received by the Arduino it will be a series of chars.

EDIT ... I just had another thought. Are you using Python3? If so you need to convert between Python3's unicode characters and the Arduino's Ascii characters. See the Python3 example in my Python - Arduino demo

...R