Python and servo sweep

Hello,
I used this code from a youtube video which works great (How to actively control a servo using an Arduino Uno, python, and your PC - YouTube), but I was wondering instead of using “move(1,180)” and then “move (1,1)” to get the servo to go one way then the other-how can I tell python to move it back and forth in one action?

All I can do is run one line in the module at a time it seems. I don’t know any python so any help would be appreciated.
Sam

All I can do is run one line in the module at a time it seems.

That's true with any language. You can make it run one line after another, or you could add a new command that tells the Arduino to sweep, instead of move.

PaulS:
That’s true with any language. You can make it run one line after another, or you could add a new command that tells the Arduino to sweep, instead of move.

Paul, yes that is what I figured. How can I do that? Do I create a function and trigger the function?
I have this code here:

def move(servo, angle):

    if (0 <= angle <= 180):
        ser.write(chr(255))
        ser.write(chr(servo))
        ser.write(chr(angle))
    else:
        print("Servo angle must be an integer between 0 and 180.\n")


def init():
    move(1,90)
    move(2,90)
    move(3,90)
    move(4,90)
    move(5,90)
    move(6,90)

init()

I have this code here:

That code sends three pieces of information. You can make the Arduino do anything you want in response to those three pieces of information. How? Well that depends on the still mysterious Arduino code.

As @PaulS has said we need to see the Arduino code.

You may find something useful in this Python - Arduino demo

...R

In particular we need to know if you have a delay in that Arduino code between the commands to move. It takes time for a servo to move and if you command it to move to a position and then immediately tell it to move to a new position that first command is terminated immediately.

Thank you for the replies, I am reading as much as I can to learn.
I know how to make a function (and how to use the sweep example in arduino), I know from reading that you basically can’t run a function from arduino through Python.
I just want to be able to (somehow-whether with python or not) to use a computer gui or something to trigger functions on my arduino. Currently I am trying to get a servo to turn 180 and then back. I also have other things in mind that I need other pins triggered manually through my computer. I know how to use the loop, but I don’t need a loop-I need pins triggered manually.

His code in Arduino is this:

/*
 * ------------------------------
 *   MultipleSerialServoControl
 * ------------------------------
 *
 * Uses the Arduino Serial library
 *  (http://arduino.cc/en/Reference/Serial)
 * and the Arduino Servo library
 *  (http://arduino.cc/en/Reference/Servo)
 * to control multiple servos from a PC using a USB cable.
 *
 * Dependencies:
 *   Arduino 0017 or higher
 *     (http://www.arduino.cc/en/Main/Software)
 *   Python servo.py module
 *     (http://principialabs.com/arduino-python-4-axis-servo-control/)
 *
 * Created:  23 December 2009
 * Author:   Brian D. Wendt
 *   (http://principialabs.com/)
 * Version:  1.1
 * License:  GPLv3
 *   (http://www.fsf.org/licensing/)
 *
 */

// Import the Arduino Servo library
#include <Servo.h> 

// Create a Servo object for each servo
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;

// TO ADD SERVOS:
//   Servo servo5;
//   etc...

// Common servo setup values
int minPulse = 600;   // minimum servo position, us (microseconds)
int maxPulse = 2400;  // maximum servo position, us

// User input for servo and position
int userInput[3];    // raw input from serial buffer, 3 bytes
int startbyte;       // start byte, begin reading input
int servo;           // which servo to pulse?
int pos;             // servo angle 0-180
int i;               // iterator

// LED on Pin 13 for digital on/off demo
int ledPin = 13;
int pinState = LOW;

void setup() 
{ 
  // Attach each Servo object to a digital pin
  servo1.attach(9, minPulse, maxPulse);
  servo2.attach(3, minPulse, maxPulse);
  servo3.attach(4, minPulse, maxPulse);
  servo4.attach(5, minPulse, maxPulse);
  servo5.attach(10, minPulse, maxPulse);
  servo6.attach(11, minPulse, maxPulse);
  // TO ADD SERVOS:
  //   servo5.attach(YOUR_PIN, minPulse, maxPulse);
  //   etc...

  // LED on Pin 13 for digital on/off demo
  pinMode(ledPin, OUTPUT);

  // Open the serial connection, 9600 baud
  Serial.begin(9600);
} 

void loop() 
{ 
  // Wait for serial input (min 3 bytes in buffer)
  if (Serial.available() > 2) {
    // Read the first byte
    startbyte = Serial.read();
    // If it's really the startbyte (255) ...
    if (startbyte == 255) {
      // ... then get the next two bytes
      for (i=0;i<2;i++) {
        userInput[i] = Serial.read();
      }
      // First byte = servo to move?
      servo = userInput[0];
      // Second byte = which position?
      pos = userInput[1];
      // Packet error checking and recovery
      if (pos == 255) { servo = 255; }

      // Assign new position to appropriate servo
      switch (servo) {
        case 1:
          servo1.write(pos);    // move servo1 to 'pos'
          break;
        case 2:
          servo2.write(pos);
          break;
        case 3:
          servo3.write(pos);
          break;
        case 4:
          servo4.write(pos);
          break;
        case 5:
          servo5.write(pos);
          break;
        case 6:
          servo6.write(pos);
          break;

   // TO ADD SERVOS:
   //     case 5:
   //       servo5.write(pos);
   //       break;
   // etc...

        // LED on Pin 13 for digital on/off demo
        case 99:
          if (pos == 180) {
            if (pinState == LOW) { pinState = HIGH; }
            else { pinState = LOW; }
          }
          if (pos == 0) {
            pinState = LOW;
          }
          digitalWrite(ledPin, pinState);
          break;
      }
    }
  }
}

Using 255 as a start byte is a bit silly because that is the value that is returned when there is nothing in the buffer to read.

However you now have to say what this code does and what you want / expect it to do.

Grumpy_Mike:
Using 255 as a start byte is a bit silly because that is the value that is returned when there is nothing in the buffer to read.

However you now have to say what this code does and what you want / expect it to do.

I just want to be able to (somehow-whether with python or not) to use a computer gui or something to trigger functions on my arduino. Currently I am trying to get a servo to turn 180 and then back. I also have other things in mind that I need other pins triggered manually through my computer. I know how to use the loop, but I don't need a loop-I need pins triggered manually.

Currently I am trying to get a servo to turn 180 and then back.

Currently, you are sending 255, a servo number and a position to move to.

With that information, you could make the servo move to the specified position and than back to 0, after a suitable pause.

If you want to do something different, you need to change what you send.

samlf3rd:
I just want to be able to (somehow-whether with python or not) to use a computer gui

This Python GUI demo may be of interest.

...R

You may want to develop your arduino receiving code first, then work on the sending GUI part of the project. Below is some arduino serial multi servo test code that might be easy to develop a GUI to send to.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}