def get_ir_buttons(ir, chan): buttons = [] button_wait = 2 try: buttons = ir.buttons(chan) except: print("IR Sensor Error") sound_tools.play_file(SoundFile.TOUCH) sound_tools.play_file(SoundFile.ERROR) #wait some wait(button_wait) return buttons
def sonic_sensor_object_detect(): global dist global dist_2 global object_detected global object_detected_1 global object_detected_2 global object_detect_run global object_detect_run_2 global object_detect_limit global ss_error global ss_error_2 object_detect_wait_time = 50 error_fix_wait =3000 #Sensor 1 if object_detect_run: try: if ss_error: #Wait some and then try to connect again wait(error_fix_wait) Init_UltrasonicSensor() ss_error=False #wait some so we don't go to fast wait(object_detect_wait_time) dist = get_object_dist() if dist < object_detect_limit: object_detected_1 =True else: object_detected_1 =False except: ss_error = True print("Sonic Sensor 1 Error") sound_tools.play_file(SoundFile.DETECTED) sound_tools.play_file(SoundFile.ERROR) sound_tools.play_file(SoundFile.ONE) if dist == 0 and not ss_error: ss_error = True print("Sonic Sensor 1 ZERO Error") sound_tools.play_file(SoundFile.DETECTED) sound_tools.play_file(SoundFile.ZERO) sound_tools.play_file(SoundFile.ERROR) sound_tools.play_file(SoundFile.ONE) else: object_detected_1 =False if object_detected_1: object_detected = True #Sensor 2 if object_detect_run_2: try: if ss_error_2: #Wait some and then try to connect again wait(error_fix_wait) Init_UltrasonicSensor_2() ss_error_2=False #wait some so we don't go to fast wait(object_detect_wait_time) dist_2 = get_object_dist_2() if dist_2 < object_detect_limit: object_detected_2 = True else: object_detected_2 = False except: ss_error_2 = True print("Sonic Sensor 2 Error") sound_tools.play_file(SoundFile.DETECTED) sound_tools.play_file(SoundFile.ERROR) sound_tools.play_file(SoundFile.TWO) if dist_2 == 0 and not ss_error_2: ss_error_2 = True print("Sonic Sensor 2 ZERO Error") sound_tools.play_file(SoundFile.DETECTED) sound_tools.play_file(SoundFile.ZERO) sound_tools.play_file(SoundFile.ERROR) sound_tools.play_file(SoundFile.TWO) else: object_detected_2 = False #Set Combined Objected detected flag if object_detected_1 or object_detected_2: object_detected = True else: object_detected = False
def object_sound(): global object_detected global object_detected_1 global object_detected_2 global object_detect_run global object_detect_run_2 global object_detect_loop global object_detect_sound_on global ObjectDetectSoundPrint if object_detected and object_detect_run and object_detect_sound_on: if ObjectDetectSoundPrint: print("Object Detect Sound Triggered:", " Object_detected_1= ", object_detected_1, " Object_detected_2= ", object_detected_2 ) if (object_detected_1 and not object_detect_run_2): sound_tools.play_file(SoundFile.DETECTED) elif(object_detected_2 and not object_detect_run_1): sound_tools.play_file(SoundFile.DETECTED) elif object_detected_1 and not object_detected_2: sound_tools.play_file(SoundFile.DETECTED) sound_tools.play_file(SoundFile.ONE) elif not object_detected_1 and object_detected_2: sound_tools.play_file(SoundFile.DETECTED) sound_tools.play_file(SoundFile.TWO) elif object_detected_1 and object_detected_2: sound_tools.play_file(SoundFile.DETECTED) sound_tools.play_file(SoundFile.ONE) sound_tools.play_file(SoundFile.TWO)
def optical_sensor_detect(): global color_detect_loop_fast_flag global color_detected global color_detected_2 global color_detected_name global color_detected_name_2 global color_rgb_int global color_rgb_int_2 global color_rgb_int_ave global color_rgb_int_ave_2 global optical_sensor_run global optical_sensor_run_2 global color_sound_on global color_sound_on_2 global cs global cs_2 global cs_error global cs_error_2 error_fix_wait = 3000 if optical_sensor_run: try: if cs_error: #Wait some and then try to connect again wait(error_fix_wait) cs = ColorSensor(ColorSensorPort) cs_error = False if color_detect_loop_fast_flag: color_rgb_int_ave = cs.reflection() else: color_rgb_int = cs.rgb() color_rgb_int_ave = sum(color_rgb_int) / 3 color_detected = cs.color() color_detected_name = color_name(color_detected) if color_sound_on: color_sound(color_detected, 1) except: cs_error = True print("Color Sensor 1 Error") sound_tools.play_file(SoundFile.COLOR) sound_tools.play_file(SoundFile.ERROR) sound_tools.play_file(SoundFile.ONE) if optical_sensor_run_2: try: if cs_error_2: #Wait some and then try to connect again wait(error_fix_wait) cs_2 = ColorSensor(ColorSensorPort_2) cs_error_2 = False if color_detect_loop_fast_flag: color_rgb_int_ave_2 = cs_2.reflection() else: color_rgb_int_2 = cs_2.rgb() color_rgb_int_ave_2 = sum(color_rgb_int_2) / 3 color_detected_2 = cs_2.color() color_detected_name_2 = color_name(color_detected_2) if color_sound_on_2: color_sound(color_detected_2, 2) except: cs_error_2 = True print("Color Sensor 2 Error") sound_tools.play_file(SoundFile.COLOR) sound_tools.play_file(SoundFile.ERROR) sound_tools.play_file(SoundFile.TWO)
def color_sound(color, color_sensor): # This function says color except based on color_sound_on global color_sound_on global color_sound_on_2 if color_sound_on or color_sound_on_2: if color == Color.WHITE: sound_tools.play_file(SoundFile.WHITE) elif color == Color.BLACK: sound_tools.play_file(SoundFile.BLACK) elif color == Color.BROWN: sound_tools.play_file(SoundFile.BROWN) elif color == Color.BLUE: sound_tools.play_file(SoundFile.BLUE) elif color == Color.GREEN: sound_tools.play_file(SoundFile.GREEN) elif color == Color.YELLOW: sound_tools.play_file(SoundFile.YELLOW) elif color == Color.RED: sound_tools.play_file(SoundFile.RED) elif color == Color.ORANGE: sound_tools.play_file(SoundFile.ORANGE) elif color == Color.PURPLE: sound_tools.play_file(SoundFile.PURPLE) #Give Sensor number if using both color sensors if color_sound_on and color_sound_on_2: if color_sensor == 1: sound_tools.play_file(SoundFile.ONE) elif color_sensor == 2: sound_tools.play_file(SoundFile.TWO)
# IR Button Command Mode if InfraredSensorPortAvailable_1 : buttons = motion_tools.get_ir_buttons(ir,chan) butt_len1=len(buttons) else: buttons.clear() butt_len1=len(buttons) # Exit Button if (Button.BEACON in buttons): main_loop =False #Toggle Optical Sensors ON/OFF if Button.LEFT_UP in buttons: if not UseOpticalSensors: sound_tools.play_file(SoundFile.START) sound_tools.play_file(SoundFile.COLOR) UseOpticalSensors= True if OpticalSensorAvailable: color_tools.optical_sensor_run= True if OpticalSensorAvailable_2: color_tools.optical_sensor_run_2 = True else: UseOpticalSensors= False sound_tools.play_file(SoundFile.STOP) sound_tools.play_file(SoundFile.COLOR) color_tools.optical_sensor_run= False color_tools.optical_sensor_run_2 = False #Toggle Sonic Sensors ON/OFF if Button.LEFT_DOWN in buttons:
def direction_sound(direction, GoSound=True): # This function says Direction Number if GoSound: sound_tools.play_file(SoundFile.GO) if direction == 0: sound_tools.play_file(SoundFile.ZERO) elif direction == 1: sound_tools.play_file(SoundFile.ONE) elif direction == 2: sound_tools.play_file(SoundFile.TWO) elif direction == 3: sound_tools.play_file(SoundFile.THREE) elif direction == 4: sound_tools.play_file(SoundFile.FOUR) elif direction == 5: sound_tools.play_file(SoundFile.FIVE) elif direction == 6: sound_tools.play_file(SoundFile.SIX) elif direction == 7: sound_tools.play_file(SoundFile.SEVEN) elif direction == 8: sound_tools.play_file(SoundFile.EIGHT) elif direction == 9: sound_tools.play_file(SoundFile.NINE) elif direction == 10: sound_tools.play_file(SoundFile.TEN)