title: Is there an "interupt" command i should be using in my program
body: <p>I would kindly ask for some support with my first program i am trying to write.</p>
  <p>I have a raspberry pi 4 and am using the program Thonny and Python 3.9.2.</p>
  <p>My program is trying to do the following.</p>
  <p>There is a Microphone, and if it hears something it should turn on the yellow light. If it hears something 3 times in a set period of time it will turn on the red light.</p>
  <p>The display runs though a script of explination and when the yellow or red light comes on it should switch to those expressions.</p>
  <p>This is the problem I am having:</p>
  <p>(to the best of my inspection) if the display is trying to change to the next line in the scrip and the yellow LED message is triggered the display gets &quot;confused&quot; and puts out nonsense.</p>
  <p>I have tried to tell the display to only put out the discriptive script if the yellow and red light are off, but this didnt seem to really help.</p>
  <p>The other observation i have is with the yellow and red LED. If the RED led triggers i want the counter reset on the yellow counter so that both yellow and red stay on for the same time, but the yellow light just stays on for the remainder of its time.sleep cycle.</p>
  <p>I am a complete beginner and cant figure this out, is there an interupt command or something i could use here?</p>
  <p>Here is the code for reference:</p>
  <pre><code>#V02 Trying to clean up the code and remove unessisary lines but working on fixing the strange characters that arrive and trying to get the system to reboot on its own.
  
  import RPi.GPIO as GPIO #starts the IO functionality of the pi
  import time # we need this for the sleep function
  import threading #threading so we can get more than one thing to run at the same time
  from time import sleep #sleep function
  GPIO.setmode(GPIO.BCM) # setting for the IO
  GPIO.setwarnings(False) #Suppress warnings
  GPIO.setup(12,GPIO.OUT) #green
  GPIO.setup(16,GPIO.OUT) #yellow
  GPIO.setup(20,GPIO.OUT) #red
  GPIO.setup(21,GPIO.IN)  #NOISE SENSOR
  # GPIO to LCD mapping
  LCD_RS = 4 # BCM GPIO 4
  LCD_E = 17 # BCM GPIO 17
  LCD_D4 = 6 # BCM GPIO 6
  LCD_D5 = 13 # BCM GPIO 13
  LCD_D6 = 19 # BCM GPIO 19
  LCD_D7 = 26 # BCM GPIO 26
  
  # Device constants
  LCD_CHR = True # Character mode
  LCD_CMD = False # Command mode
  LCD_CHARS = 16 # Characters per line (16 max)
  LCD_LINE_1 = 0x80 # LCD memory location for 1st line
  LCD_LINE_2 = 0xC0 # LCD memory location 2nd line
  
  GPIO.output(16,GPIO.LOW) #set all pins to LOW Start postion for program
  GPIO.output(20,GPIO.LOW) #set all pins to LOW Start postion for program
  GPIO.output(12,GPIO.LOW) #set all pins to LOW Start postion for program
  
  def Green(): #This is the status LED for the Green LED
      while True:
          GPIO.output(12,GPIO.HIGH)
          time.sleep(2.5)
          GPIO.output(12,GPIO.LOW)
          time.sleep(.5)
          
  def Yellow():
      while True:
          if GPIO.input(21) == 1: #this is the microphone sending a detection signal 
  
              print(&quot;Sound Detected&quot;)
              GPIO.output(16,GPIO.HIGH) #turn on the yellow LED
              lcd_text(&quot;I heard that&quot;,LCD_LINE_1)
              lcd_text(&quot;Relax&quot;,LCD_LINE_2)
              time.sleep(10)
              GPIO.output(16,GPIO.LOW) #turn off the yellow LED
              x = 0
          
  def Red():
      x = 0
      while True:
              if GPIO.input(16) == 1 and GPIO.input(21) == 1: #if Yellow light and Microphone activate at the same time we start counter for RED LED 
                      x += 1
                      print(x)
                      time.sleep(.45)
              if x &gt; 2:
                      print(&quot;Too noisy in here&quot;)
                      GPIO.output(16,GPIO.HIGH)#if the x counter (sound counter) reaches greater than 3 we set the yellow and red LED on for 5 seconds
                      GPIO.output(20,GPIO.HIGH)
                      lcd_text(&quot;Schnauze&quot;,LCD_LINE_1)
                      lcd_text(&quot;sonst Beule&quot;,LCD_LINE_2)         
                      time.sleep(10)
                      GPIO.output(20,GPIO.LOW)
                      GPIO.output(16,GPIO.LOW)
                      x = 0
              if GPIO.input(16) == 0: #if the yellow light goes out before we reach x &gt; 2 we set x back to 0
                      x = 0
  
  
  
  t1 = threading.Thread(target=Green)
  t2 = threading.Thread(target=Yellow)
  t3 = threading.Thread(target=Red)
  
  t1.start()
  t2.start()
  t3.start()
  
  
  
  # Define main program code
  def main():
   
   GPIO.setwarnings(False)
   GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
   GPIO.setup(LCD_E, GPIO.OUT) # Set GPIO's to output mode
   GPIO.setup(LCD_RS, GPIO.OUT)
   GPIO.setup(LCD_D4, GPIO.OUT)
   GPIO.setup(LCD_D5, GPIO.OUT)
   GPIO.setup(LCD_D6, GPIO.OUT)
   GPIO.setup(LCD_D7, GPIO.OUT)
  
  # Initialize display
   lcd_init()
  
  # Loop - send text and sleep 3 seconds between texts
  
   while True:
       
         if GPIO.input(16) == 0 and GPIO.input(20) == 0:
           lcd_text(&quot;Welcome to&quot;,LCD_LINE_1)
           lcd_text(&quot;P4DQ&quot;,LCD_LINE_2)
           time.sleep(5) # 3 second delay
           lcd_init() # start the LCD init program at the end of the loop to clear out strange characters on the display
           
         if GPIO.input(16) == 0 and GPIO.input(20) == 0:
           lcd_text(&quot;I detect loud&quot;,LCD_LINE_1)
           lcd_text(&quot;noise&quot;,LCD_LINE_2)
           time.sleep(5) # 3 second delay
           lcd_init() # start the LCD init program at the end of the loop to clear out strange characters on the display
           
         if GPIO.input(16) == 0 and GPIO.input(20) == 0:
           lcd_text(&quot;Green LED&quot;,LCD_LINE_1)
           lcd_text(&quot;I'm listening&quot;,LCD_LINE_2)
           time.sleep(5) # 3 second delay
           lcd_init() # start the LCD init program at the end of the loop to clear out strange characters on the display
           
         if GPIO.input(16) == 0 and GPIO.input(20) == 0:
           lcd_text(&quot;Yellow LED&quot;,LCD_LINE_1)
           lcd_text(&quot;I hear something&quot;,LCD_LINE_2)
           time.sleep(5)
           lcd_init() # start the LCD init program at the end of the loop to clear out strange characters on the display
         
         if GPIO.input(16) == 0 and GPIO.input(20) == 0:
           lcd_text(&quot;Red LED&quot;,LCD_LINE_1)
           lcd_text(&quot;It's TOO LOUD :(&quot;,LCD_LINE_2)
           time.sleep(5)
           lcd_init() # start the LCD init program at the end of the loop to clear out strange characters on the display
           
         if GPIO.input(16) == 0 and GPIO.input(20) == 0:
           lcd_init() # start the LCD init program at the end of the loop to clear out strange characters on the display
           
    
  
  # End of main program code
  
  # Initialize and clear display
  def lcd_init():
   lcd_write(0x33,LCD_CMD) # Initialize
   lcd_write(0x32,LCD_CMD) # Set to 4-bit mode
   lcd_write(0x06,LCD_CMD) # Cursor move direction
   lcd_write(0x0C,LCD_CMD) # Turn cursor off
   lcd_write(0x28,LCD_CMD) # 2 line display
   lcd_write(0x01,LCD_CMD) # Clear display
   time.sleep(0.0005) # Delay to allow commands to process
  
  
  def lcd_write(bits, mode):
  # High bits
   GPIO.output(LCD_RS, mode) # RS
  
   GPIO.output(LCD_D4, False)
   GPIO.output(LCD_D5, False)
   GPIO.output(LCD_D6, False)
   GPIO.output(LCD_D7, False)
   if bits&amp;0x10==0x10:
       GPIO.output(LCD_D4, True)
   if bits&amp;0x20==0x20:
       GPIO.output(LCD_D5, True)
   if bits&amp;0x40==0x40:
       GPIO.output(LCD_D6, True)
   if bits&amp;0x80==0x80:
       GPIO.output(LCD_D7, True)
  
  # Toggle 'Enable' pin
   lcd_toggle_enable()
  
  # Low bits
   GPIO.output(LCD_D4, False)
   GPIO.output(LCD_D5, False)
   GPIO.output(LCD_D6, False)
   GPIO.output(LCD_D7, False)
   if bits&amp;0x01==0x01:
       GPIO.output(LCD_D4, True)
   if bits&amp;0x02==0x02:
       GPIO.output(LCD_D5, True)
   if bits&amp;0x04==0x04:
       GPIO.output(LCD_D6, True)
   if bits&amp;0x08==0x08:
       GPIO.output(LCD_D7, True)
  
  # Toggle 'Enable' pin
   lcd_toggle_enable()
  
  def lcd_toggle_enable():
   time.sleep(0.0005)
   GPIO.output(LCD_E, True)
   time.sleep(0.0005)
   GPIO.output(LCD_E, False)
   time.sleep(0.0005)
  
  def lcd_text(message,line):
   # Send text to display
   message = message.ljust(LCD_CHARS,&quot; &quot;)
  
   lcd_write(line, LCD_CMD)
  
   for i in range(LCD_CHARS):
       lcd_write(ord(message[i]),LCD_CHR)
  
  
  #Begin program
  try:
   main()
  
  finally:
   lcd_write(0x01, LCD_CMD)
   GPIO.cleanup()
  </code></pre>