2

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.

asked Mar 2, 2013 at 3:52
2
  • 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. Commented Mar 2, 2013 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. Commented Mar 2, 2013 at 23:02

1 Answer 1

1

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.

answered Mar 6, 2013 at 17:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.