Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6177017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:12:00+00:00 2026-05-24T00:12:00+00:00

I am a total newbie with Python, I normally use Perl. I have an

  • 0

I am a total newbie with Python, I normally use Perl.

I have an Arduino wired up with some servos I use to control a web cam, the script in question works perfect, the servos receive the instruction and do what they should:

    #!/usr/bin/env python
    import serial

    usbport = '/dev/ttyACM0'

    # Set up serial baud rate
    ser = serial.Serial(usbport, 9600, timeout=1)


    def move(servo, angle):
        '''Moves the specified servo to the supplied angle.

        Arguments:
        servo
          the servo number to command, an integer from 1-4
        angle
         the desired servo angle, an integer from 0 to 180

    (e.g.) >>> servo.move(2, 90)
           ... # "move servo #2 to 90 degrees"'''

    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"

Basically the command for the above script is:

    python
    import servo
    servo.move(1, 180)

I have converted the script to accept command line input, however the program does not seem to work. can anyone figure out what I am doing wrong, here is my version:

    #!/usr/bin/env python
    import serial
    import sys

    try:
            servo = int(sys.argv[1])
            angle = int(sys.argv[2])
    except IndexError:
            print ('a servo and angle are required')
    sys.exit(2)

    # Set up serial baud rate
    usbport = '/dev/ttyACM0'
    ser = serial.Serial(usbport, 9600, timeout=1)

    def move(servo, angle):
        '''Moves the specified servo to the supplied angle.

    Arguments:
        servo
          the servo number to command, an integer from 1-4
        angle
          the desired servo angle, an integer from 0 to 180

    (e.g.) >>> servo.move(2, 90)
           ... # "move servo #2 to 90 degrees"'''

    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. You typed:"
        print servo
        print angle
    move(servo, angle)

Basically the command for the above script is:

    servo.py 1 180

I know the script works as it should as if I tell it to do a 1 181 I get the error that the angle is out of range, so I’m totally puzzled as to why the info is not passed to the serial/arduino.

Many thanks for any help and sorry for the long winded post.

Gilbert

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:12:02+00:00Added an answer on May 24, 2026 at 12:12 am

    Why adapt the original script? Just use this:

    import servo
    servo.move(int(argv[1]), int(argv[2]))
    

    I’m not sure whether this happened when pasting code into stackoverflow, but the indenting is flawed on multiple places. Please check this too 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Total newbie question but this is driving me mad! I'm trying this: myInt =
I'm a total newbie, but I was writing a little program that worked on
I'm a total newbie to SVN and haven't been able to find an answer
This is a total newbie question, so thanks in advance. I'm trying to get
I am a total Groovy newbie. I saw the following code here . def
total newbie here. i was trying to replace a character in char * but
A total newbie question.... I am developing a Joomla template and in this I
Disclaimer: I'm a total newbie at graph theory and I'm not sure if this
I'm a total newbie at PHP, but this seems so simple, there doesn't seem
I am a total newbie in Java and Eclipse. I googled for lots of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.