Ok, at the moment I am working on adding motors to a lego tank that I have. I have one servo in the front to steer and another motor in the rear that will make it go forwards and backwards. i have them both hooked up to a pwm controller from adafruit. I have used their python library and I have some code to move the motors. Now I just am wondering how I can make it so that when the program is running if I hit "w" it will go forward, no having to press enter. Sorry if this seems like a simple problem, I am a beginner programmer.

  • 1
    You'll probably get better help if you rephrase this in terms of code (like, show the code, refer to the library docs, etc) and ask on a python forum. – goldilocks Mar 2 '13 at 16:32
  • Using Lego to learn to program is awesome, that's how they taught us at uni, and it works a treat. If you can link me to the man page I'd be more than happy to have a crack at it for you. – Impulss Mar 2 '13 at 23:02

The full answer is here: https://stackoverflow.com/questions/292095/polling-the-keyboard-in-python

The short answer is:

import sys
import select

def heardEnter():
    i,o,e = select.select([sys.stdin],[],[],0.0001)
    for s in i:
        if s == sys.stdin:
            input = sys.stdin.readline()
            return True
    return False

You will need to generalize this for steering. sys.stdlin.readline is looking to read everything up to the enter key (newline) You probably just want to do sys.stdin.read(1) to get a single keystroke.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.