Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using python 2.7 my goal is to have an program that runs two loops simultaneously to control LEDs on two separate bargraph LEDs (12 segments). The bargraph LEDs will represent levels of O2 and CO2. The O2 will always be decreasing and the CO2 will always be increasing. Except when you push a button, which causes the levels to go back to 0 for CO2 and 12 for O2. There are two buttons, one for O2 and one for CO2.

I'd like to run multiprocessing since I've tried threading and its not super smooth. However, when I try the following multiprocessing, I get: "The Program is Still Running. Do you want to kill it?!"". When I run the equivalent with the threading module it works (but again not great).

import smbus
import time
import os
from  multiprocessing import Process
from Adafruit_LED_Backpack import BicolorBargraph24
import pygame
from pygame.locals import *

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(0)

bus = smbus.SMBus(1) 

#Setting up sound effects
pygame.mixer.init()

#O2 and CO2 Sounds
alarm1Sound = pygame.mixer.Sound("/home/pi/Modules/alarm1.wav")
co2AlarmSound = pygame.mixer.Sound("/home/pi/Modules/alert.wav")
o2AlarmSound = pygame.mixer.Sound("/home/pi/Modules/alert.wav")

#---------------------Setting up GPIOs.-----------------#

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)

###########################LED Set up######################################

#-O2 & CO2 Bargraph LED
o2display = BicolorBargraph24.BicolorBargraph24(address=0x71)
Co2display = BicolorBargraph24.BicolorBargraph24(address=0x74)

# Initialize the display. 
o2display.begin()
Co2display.begin()
o2display.write_display()
Co2display.write_display()

#----------------O2 & CO2 Bargraph LED Variables--------------------------

RechargingOxygen=False
ReplacingCo2Scrubber=False

OxygenLevel = 0
OxygenEmptyKnown = False
Co2Level = 0
Co2FullKnown = False

g = BicolorBargraph24.GREEN
y = BicolorBargraph24.YELLOW
r = BicolorBargraph24.RED
b = BicolorBargraph24.OFF

o2ledON = [r, r, r, r, y, y, y, y, y, y, y, y, y, y, y, g, g, g, g, g, g, g, g, g]

Co2ledON = [g, g, g, g, g, y, y, y, y, y, y, y, y, y, y, r, r, r, r, r, r, r, r, r]

def increaseCo2():
  try:
    GPIO.add_event_detect(24, GPIO.FALLING, callback = Co2interrupt, bouncetime=3000)
  except KeyboardInterrupt:
    GPIO.cleanup()
  global co2AlarmSound
  global ReplacingCo2Scrubber
  global Co2ledON
  global Co2Level
  global Co2FullKnown
  while(True):
    if(not ReplacingCo2Scrubber):
      if (Co2Level == 23 and Co2FullKnown == False):
        print "Your Co2 scrubber is toast! Replace your Co2 cartridge!"
        co2AlarmSound.play(loops=3)
        #os.system('aplay alarm1.wav &')
        Co2FullKnown = True
      elif (Co2Level >= 0 and Co2Level <= 23):
        try:
          Co2display.set_bar(Co2Level, Co2ledON[Co2Level])
          Co2display.write_display()
          Co2Level += 1
        except IOError as e:
          print "I/O error({0}): {1}".format(e.errno, e.strerror)
        except:
          print "Unexpected error:", sys.exc_info()[0]
    time.sleep(9)
                
def useoxygen():
  try:
    GPIO.add_event_detect(23, GPIO.FALLING, callback = o2interrupt, bouncetime=3000)   
  except KeyboardInterrupt:
        GPIO.cleanup() 
  global o2AlarmSound
  global RechargingOxygen
  global OxygenLevel
  global OxygenEmptyKnown
  while(True):
    if(not RechargingOxygen):
      if OxygenLevel == 0 and OxygenEmptyKnown == False:
        print "You've run out of Oxygen! Replace your o2 Cartridge!"
        o2AlarmSound.play(loops=3)
        OxygenEmptyKnown = 1
        #os.system('aplay alarm1.wav &')
      elif (OxygenLevel >= 0 and OxygenLevel <= 23):
        try:
          o2display.set_bar(OxygenLevel, b)
          o2display.write_display()
          OxygenLevel -= 1
        except IOError as e:
          print "I/O error({0}): {1}".format(e.errno, e.strerror)
        except:
          print "Unexpected error:", sys.exc_info()[0]
    time.sleep(8)       

#---------------Main Program-----------------------------------

if __name__ == '__main__':

    #This puts the oxygen bargraph up to full, which will eventually decrease to 0
    for index, colour in enumerate(o2ledON):
        o2display.set_bar(index, colour)
        o2display.write_display()
        time.sleep(.1)
        print "o2 level : ",index
  
    o2thread = Process(target=useoxygen)
    Co2thread = Process(target=increaseCo2)
    
    Co2thread.start()
    o2thread.start()


What I have tried:

I have previously used this thread using multithreading and it works but not so great.
Posted
Updated 13-Sep-18 12:38pm
v2
Comments
Richard MacCutchan 14-Sep-18 3:59am    
What do you mean by multiprocessing? Are you running two copies of the same program at the same time?
Member 13983803 14-Sep-18 4:58am    
Hi Richard, thanks for taking the time to read my question. I mean that I'd like to run the multiprocessing module from python in order to simultaneously run two functions at once. In this case, the two functions are 1) useoxgen and 2) increaseCo2. Both while loops within those functions run at the same time.
Richard MacCutchan 14-Sep-18 10:13am    
Sorry I have never used that. I suggest you take a look at 17.2. multiprocessing — Process-based parallelism — Python 3.4.9 documentation[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900