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.