For this assignment, I programmed the servo so that it acts like an alarm clock. It first rings loud or vibrates. Then, it becomes snoozed and prepares to ring again.

Like how I change my alarm’s setting so that it sometimes rings loud (play music) and sometimes vibrate only, I added a pseudo-random number generator to decide that for me. When “ring loud” option is chosen, the servo oscillates slowly at first and then becomes faster so that the movement is big and loud. For the vibration, I used two fast linear moves and made them repeat many times using a for loop.

I also often change the snooze duration for my alarms, so I wanted the servo to decide the snooze duration for me. To achieve this, I added more pseudo-random number generators that gives me a random angle, random speed, and number of times the snoozing repeats. The snoozing is active quiet because its resetting the alarm.

# Create an object to represent a servo on the given hardware pin.
print("Creating servo object.")
servo = Servo(board.GP0)

print("Starting main script: Alarm")
while True:
    # initial pause
    time.sleep(2.0)

    vibrate_or_ringloud = random.random()

    if vibrate_or_ringloud < 0.6:
        # alarm slowly starting
        print("Alarm starting")
        ringing_move(servo, 30.0, duration=1.5)
        ringing_move(servo, 90.0, duration=1.0)
        ringing_move(servo, 30.0, duration=1.0)

        # alarm loud ringing
        print("Alarm ringing")
        ringing_move(servo, 180.0, q=0, qd=400, b=0.3, duration=10.0)
    else:
        print("Alarm vibrating")
        for x in range(40):
            linear_move(servo, 0.0, 50, speed=1200)
            linear_move(servo, 50, 0.0, speed=1200)

    # slow movements to reset the alarm
    print("Alarm snoozed")
    snooze_repeat = random.randrange(1, 3, 1)
    snooze_speed = random.randrange(25, 50)
    snooze_angle = random.randrange(150, 240, 10)

    for count in range(snooze_repeat):
        print(count)
        print(f"Resetting alarm at speed {snooze_speed} at {snooze_angle} degrees")
        linear_move(servo, 0.0, snooze_angle, speed=snooze_speed)
        linear_move(servo, snooze_angle, 0.0, speed=snooze_speed)

    # brief pause; stillness is the counterpoint
    print("Alarm preparing to ring again")
    time.sleep(0.5)