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.
-
1You'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– goldilocks2013年03月02日 16:32:41 +00:00Commented 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.Impulss– Impulss2013年03月02日 23:02:19 +00:00Commented Mar 2, 2013 at 23:02
1 Answer 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.