# Imports... because we kinda need them... from pybricks.hubs import PrimeHub from pybricks.pupdevices import Motor, ColorSensor from pybricks.parameters import Port, Stop, Color, Direction from pybricks.robotics import DriveBase from pybricks.tools import wait, StopWatch, multitask, run_task # Sets all the default values for the runs hub = PrimeHub() left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE) right_motor = Motor(Port.B) left_arm = Motor(Port.C)#, Direction.COUNTERCLOCKWISE) right_arm = Motor(Port.D) drive_base = DriveBase(left_motor, right_motor, wheel_diameter=68.8, axle_track=180) drive_base.settings(550,700,100,100) drive_base.use_gyro(True) color_sensor = ColorSensor(Port.F) Color.ORANGE = Color(37, 85, 95) Color.BLUE = Color(230,100,100) Color.YELLOW = Color(53, 75, 84) color_sensor.detectable_colors([Color.YELLOW, Color.BLUE, Color.GREEN, Color.WHITE, Color.RED, Color.ORANGE, Color.NONE]) hub.speaker.volume(50) # Set the volume of the speaker color_sensor.detectable_colors() # Notes to hertz for nice music cn = { "Cs3": 138.59, "D3": 146.83, "Ds3": 155.56, "E3": 164.81, "F3": 174.61, "Fs3": 185.00, "G3": 196.00, "Gs3": 207.65, "A3": 220.00, "As3": 233.08, "B3": 246.94, "C4": 261.63, "Cs4": 277.18, "D4": 293.66, "Ds4": 311.13, "E4": 329.63, "F4": 349.23, "Fs4": 369.99, "G4": 392.00, "Gs4": 415.30, "A4": 440.00, "As4": 466.16, "B4": 493.88, "C5": 523.25, "Cs5": 554.37 } # RUNS - contains the mission code that will be executed on color sensor detection async def Run1(): left_arm.run_angle(1000, 300) right_arm.run_angle(1000,500) await drive_base.straight(320) await right_arm.run_angle(5000,-500, Stop.HOLD) await right_arm.run_angle(5000,500, Stop.HOLD) await right_arm.run_angle(5000,-500, Stop.HOLD) await right_arm.run_angle(5000,500, Stop.HOLD) await right_arm.run_angle(5000,-500, Stop.HOLD) await drive_base.turn(-20) await drive_base.straight(277) await drive_base.turn(20) await drive_base.straight(65) await drive_base.turn(-30) right_arm.run_angle(50,500) await drive_base.turn(45) await drive_base.straight(-145) await drive_base.turn(-60) await drive_base.straight(90) await left_arm.run_angle(1000,-450) await drive_base.straight(-145) await left_arm.run_angle(1000,450) await drive_base.straight(10) await drive_base.turn(35) await drive_base.straight(-600) async def Run2(): await drive_base.straight(200) await drive_base.turn(-20) await drive_base.straight(525) await drive_base.turn(60) await drive_base.straight(50) await right_arm.run_angle(2000,1000) await drive_base.straight(-50) await drive_base.turn(45) await drive_base.straight(50) await right_arm.run_angle(350,-1000) await drive_base.straight(-100) await drive_base.turn(-100) await drive_base.straight(-600) async def Run3(): await drive_base.straight(920) await drive_base.turn(-90) await drive_base.straight(60) drive_base.turn(-10) await left_arm.run_angle(10000,-4000) await drive_base.straight(-110) await drive_base.turn(90) await drive_base.straight(2000) async def Run4(): await drive_base.straight(519) await left_arm.run_angle(300, -100) await left_arm.run_angle(300, 500) await drive_base.straight(180) await drive_base.turn(-37) await drive_base.straight(50) await right_arm.run_angle(300, -400) await drive_base.straight(-150) await drive_base.turn(125) await drive_base.straight(50) await right_arm.run_angle(300, 400) await drive_base.straight(-75) await right_arm.run_angle(300, 300) await drive_base.turn(-40) await drive_base.straight(250) await right_arm.run_angle(100, -300) await drive_base.straight(30) await right_arm.run_angle(50,-250) await drive_base.turn(30) await drive_base.straight(-200) await drive_base.turn(-50) await drive_base.straight(-800) async def Run5(): await drive_base.straight(420) await right_arm.run_angle(300,-100) await drive_base.straight(-100) await right_arm.run_angle(300, 100) await drive_base.straight(-350) async def Run6(): left_arm.run_angle(500,200) right_arm.run_angle(500,200) await drive_base.straight(70) await drive_base.turn(-55) await drive_base.straight(900) await drive_base.turn(92.5) await drive_base.straight(75) await drive_base.straight(21) await right_arm.run_angle(500,-250) await right_arm.run_angle(500,250) await drive_base.straight(-20) await drive_base.turn(55) await left_arm.run_angle(300,-400) await drive_base.turn(70) await left_arm.run_angle(300,400) await drive_base.turn(-60) await drive_base.straight(900) # Celebration sound types class CelebrationSound: VICTORY_FANFARE = 0 WAITING_SOUND = 1 SUCCESS_CHIME = 2 TA_DA = 3 POWER_UP = 4 RICKROLL_INSPIRED = 5 # Sounds functions async def play_victory_fanfare(): """Classic victory fanfare""" notes = [ (cn["B3"], 200), (cn["B3"], 100), (cn["B3"], 100), (cn["E4"], 600) ] for freq, duration in notes: await hub.speaker.beep(freq, duration) await wait(50) async def play_waiting_sound(): # T3rm1na1 V3l0c1ty arpeggio recreated in pybricks notes = [ (cn["Cs4"], 100), (cn["E4"], 100), (cn["Cs4"], 100), (cn["Cs5"], 100), (cn["Cs4"], 100), (cn["Gs4"], 100), (cn["E4"], 100), (cn["Cs4"], 100), (cn["Gs4"], 100), (cn["Cs4"], 100), (cn["Cs5"], 100), (cn["Gs4"], 100), (cn["Cs4"], 100), (cn["Gs4"], 100), (cn["Cs5"], 100), (cn["Gs4"], 100) ] for freq, duration in notes: await hub.speaker.beep(freq, duration) async def play_success_chime(): """Simple success notification""" notes = [ (cn["E4"], 150), (cn["Gs4"], 150), (cn["B4"], 300), ] for freq, duration in notes: await hub.speaker.beep(freq, duration) await wait(50) async def play_ta_da(): """Classic "ta-da!" sound""" notes = [ (cn["Cs4"], 200), (cn["Ds4"], 200), (cn["E4"], 200), (cn["Ds4"], 200), (cn["E4"], 200), (cn["Fs4"], 400), (cn["Gs4"], 600), (cn["Fs4"], 400), (cn["Gs4"], 200), (cn["A4"], 200), (cn["B4"], 200), (cn["C5"], 200), (cn["Cs5"], 200) ] for freq, duration in notes: await hub.speaker.beep(freq, duration) #await wait(100) async def play_power_up(): """Rising power-up sound""" frequencies = [ 164.81, 207.65, 246.94, 329.63, 415.30, 493.88 ] for freq in frequencies: await hub.speaker.beep(freq, 50) await wait(10) await hub.speaker.beep(659.24, 200) async def play_rickroll_inspired(): """Fun 80s-style dance beat inspired sound""" # Upbeat bouncy rhythm pattern = [ (cn["B3"], 100), (cn["Cs4"], 100), (cn["Ds4"], 100), (cn["E4"], 100), (cn["Cs4"], 100), (cn["B3"], 100), (cn["A3"], 100), (cn["B3"], 200), (cn["Cs4"], 100), (cn["B3"], 100), (cn["A3"], 100), (cn["Gs3"], 200), ] for freq, duration in pattern: await hub.speaker.beep(freq, duration) #await wait(50) # Basically a big if else statement that calls the functions above async def celebrate_mission_complete(sound_type=CelebrationSound.RICKROLL_INSPIRED): """ Main celebration function to call after completing a mission. Plays a sound and shows light animation. Args: sound_type: CelebrationSound enum value (default: SUCCESS_CHIME) """ # Light show hub.light.on(Color.GREEN) # Play the selected celebration sound if sound_type == CelebrationSound.VICTORY_FANFARE: await play_victory_fanfare() elif sound_type == CelebrationSound.WAITING_SOUND: await play_waiting_sound() elif sound_type == CelebrationSound.SUCCESS_CHIME: await play_success_chime() elif sound_type == CelebrationSound.TA_DA: await play_ta_da() elif sound_type == CelebrationSound.POWER_UP: await play_power_up() elif sound_type == CelebrationSound.RICKROLL_INSPIRED: await play_rickroll_inspired() else: await play_success_chime() # Default fallback # Blink the light for _ in range(3): hub.light.off() await wait(100) hub.light.on(Color.GREEN) await wait(100) hub.light.off() # This where everything happens async def main(): # MAIN LOOP while True: await celebrate_mission_complete(CelebrationSound.RICKROLL_INSPIRED) color_reflected_percent = await color_sensor.reflection() print(f"Color reflection percentage: {color_reflected_percent}") color_detected = await color_sensor.color() print(f'Detected color: {color_detected}') hsv = await color_sensor.hsv() print(f"Measured HSV: {hsv}") if color_reflected_percent > 0: if color_detected == Color.GREEN: print('Running Mission 1') await Run1() await celebrate_mission_complete(CelebrationSound.VICTORY_FANFARE) elif color_detected == Color.WHITE: print('Running Mission 2') await Run2() await celebrate_mission_complete(CelebrationSound.RICKROLL_INSPIRED) elif color_detected == Color.YELLOW: print('Running Mission 3') await Run3() await celebrate_mission_complete(CelebrationSound.SUCCESS_CHIME) elif color_detected == Color.ORANGE: print('Running Mission 4') await Run4() await celebrate_mission_complete(CelebrationSound.POWER_UP) elif color_detected == Color.BLUE: print('Running Mission 5') await Run5() await celebrate_mission_complete(CelebrationSound.POWER_UP) elif color_detected == Color.RED: print('Running Mission 6') await Run6() await celebrate_mission_complete(CelebrationSound.TA_DA) else: hub.light.off() left_motor.stop() right_motor.stop() else: print("No color was detected.") await wait(100) # prevent loop from iterating fast run_task(main())