title: Can i change this code to make it stop my raspberry pi car when no key is pressed? (python)
body: <p>First off, I have Q set to the stop function for my car, but I would like to make it so the car only moves when a key is pressed. E.g., when I press W the car goes forward, but I have to use Q to stop the car. I want to make it so that I have to hold down W for the car to go forward, and when I release the W key I want the car to stop. </p>
  
  <p>I have worked at this for a while now as a school project, but I am getting nowhere because I don't know what to search for on Google or there's no info on the topic. So I have come here to see if anyone can help me. Thanks in advance for anyone who takes any time in helping me resolve my problem.</p>
  
  <p>Here is my code. I don't care if you completely change it to achieve what I want, as long as it still works in my scenario. If there's any more info you need, please feel free to ask me in the comments.</p>
  
  <p>*Even though it's for a school project, I will put in my report that I had to post this here and that the person who finds a fix will get credited for the help. :) </p>
  
  <pre><code>import RPi.GPIO as io
  io.setmode(io.BCM)
  import time, sys, tty, termios
  from termcolor import colored, cprint
  
  motor1_in1_pin = 4
  motor1_in2_pin = 17
  io.setup(motor1_in1_pin, io.OUT)
  io.setup(motor1_in2_pin, io.OUT)
  
  motor2_in1_pin = 24
  motor2_in2_pin = 25
  motor2_in3_pin = 22
  io.setup(motor2_in1_pin, io.OUT)
  io.setup(motor2_in2_pin, io.OUT)
  io.setup(motor2_in3_pin, io.OUT)
  
  
  io.setup(18, io.OUT)
  io.output(18, False)
  
  
  io.setup(23, io.OUT)
  io.output(23, False)
  
  
  io.setup(27, io.OUT)
  io.output(27, False)
  
  def getch():
      fd = sys.stdin.fileno()
      old_settings = termios.tcgetattr(fd)
      try:
          tty.setraw(sys.stdin.fileno())
          ch = sys.stdin.read(1)
      finally:
          termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
      return ch
  
  
  
  
  def motor1_forward():
      io.output(motor1_in1_pin, True)
      io.output(motor1_in2_pin, False)
  
  def motor1_reverse():
      io.output(motor1_in1_pin, False)
      io.output(motor1_in2_pin, True)
  
  def motor1_stop():
      io.output(motor1_in1_pin, False)
      io.output(motor1_in2_pin, False)
  
  def motor2_forward():
      io.output(motor2_in1_pin, True)
      io.output(motor2_in2_pin, False)
      io.output(motor2_in3_pin, False)
  
  def motor2_reverse():
      io.output(motor2_in1_pin, False)
      io.output(motor2_in2_pin, True)
      io.output(motor2_in3_pin, True)
  
  def motor2_stop():
      io.output(motor2_in1_pin, False)
      io.output(motor2_in2_pin, False)
      io.output(motor2_in3_pin, False)
  
  def toggleLights():
  
      global lightStatus
  
      if(lightStatus == False):
          io.output(18, True)
          io.output(23, True)
          lightStatus = True
      else:
          io.output(18, False)
          io.output(23, False)
          lightStatus = False
  
  
  
  def toggleSteering(direction):
  
      global wheelStatus
  
      if(direction == "right"):
          if(wheelStatus == "centre"):
              motor1_forward()         
              wheelStatus = "right"
          elif(wheelStatus == "left"):
              motor1_stop()
              wheelStatus = "centre"
  
      if(direction == "left"):
          if(wheelStatus == "centre"):
              motor1_reverse()
              wheelStatus = "left"
          elif(wheelStatus == "right"):
              motor1_stop()
              wheelStatus = "centre"
  
  
  io.output(motor1_in1_pin, False)
  io.output(motor1_in2_pin, False)
  io.output(motor2_in1_pin, False)
  io.output(motor2_in2_pin, False)
  io.output(motor2_in3_pin, False)
  
  lightStatus2 = False
  lightStatus = False
  wheelStatus = "centre"
  
  
  bold = "\033[1m"
  reset = "\033[0;0m"
  cprint("Jacksons Awesome Rc car Program", 'blue')
  print " " +bold + "w/s:" +reset + " Acceleration"
  print " " +bold + "a/d:" +reset + " Steering"
  print " " +bold + "q:" +reset + " Brake"
  print("l: lights")
  print("p: Dishwasher")
  
  
  
  while True:
      char = getch()  
      if(char == "w"):
          motor2_forward()
  
      if(char == "q"):
          motor2_stop()
  
      if(char == "s"):
          motor2_reverse()
  
      if(char == "q"):
          motor2_stop()
  
      if(char == "a"):
          toggleSteering("left")
  
      if(char == "d"):
          toggleSteering("right")
  
      if(char == "l"):
          toggleLights()
  
      if(char == "o"):
          cPrint("hi", 'red')
  
      if(char == "p"):
          print " " +bold + "Dishwasher is a mechanical device for cleaning dishes and eating-utensils. Dishwashers can be found in restaurants and private homes" +reset + " "
  
      if(char == "x"):
          cprint("Program Ended", 'red')
          break
  
      char = ""
  
  
  
  
  io.cleanup()
  </code></pre>