def run(): display = Display() generator = Generator() button_a = Debouncer(board.D9, digitalio.Pull.UP, 0.01) button_b = Debouncer(board.D6, digitalio.Pull.UP, 0.01) button_c = Debouncer(board.D5, digitalio.Pull.UP, 0.01) encoder_button = Debouncer(board.D12, digitalio.Pull.UP, 0.01) encoder = rotaryio.IncrementalEncoder(board.D10, board.D11) current_position = None # current encoder position change = 0 # the change in encoder position delta = 0 # how much to change the frequency by shape = shapes.SINE # the active waveform frequency = 440 # the current frequency display.update_shape(shape) # initialize the display contents display.update_frequency(frequency) while True: encoder_button.update() button_a.update() button_b.update() button_c.update() current_position, change = get_encoder_change(encoder, current_position) if change != 0: if not button_a.value: delta = change * 1000 elif not button_b.value: delta = change * 100 elif not button_c.value: delta = change * 10 else: delta = change frequency = change_frequency(frequency, delta) if encoder_button.fell: shape = change_shape(shape) display.update_shape(shape) display.update_frequency(frequency) generator.update(shape, frequency)
State.exit(self, machine) def update(self, machine): if switch.fell: if audio.paused: audio.resume() servo.throttle = self.paused_servo self.paused_servo = 0.0 machine.resume_state(machine.paused_state) elif not switch.value: if time.monotonic() - self.switch_pressed_at > 1.0: machine.go_to_state('raising') ################################################################################ # Create the state machine pretty_state_machine = StateMachine() pretty_state_machine.add_state(WaitingState()) pretty_state_machine.add_state(DroppingState()) pretty_state_machine.add_state(BurstState()) pretty_state_machine.add_state(ShowerState()) pretty_state_machine.add_state(IdleState()) pretty_state_machine.add_state(RaisingState()) pretty_state_machine.add_state(PausedState()) pretty_state_machine.go_to_state('waiting') while True: switch.update() pretty_state_machine.update()
crickit.continuous_servo_1.set_pulse_width_range(min_pulse=500, max_pulse=2500) speed = -0.04 #this is clockwise/forward at a moderate tempo def play_voice(vo): mixer.stop_voice(vo) mixer.play(samples[vo], voice=vo, loop=False) while True: clock_pin.update() #debouncer at work voice_1_pin.update() voice_2_pin.update() voice_3_pin.update() voice_4_pin.update() touch_1_pad.update() touch_4_pad.update() touch_2_3_pad.update() crickit.continuous_servo_1.throttle = speed # spin the disc at speed defined by touch pads if clock_pin.fell: # sensor noticed change from white (reflection) to black (no reflection) # this means a clock tick has begun, time to check if any steps will play led.value = 0 if voice_1_pin.value: # a black step (no reflection) mark during clock tick, play a sound! led.value = 1 # light up LED when step is read # print('| .kick. | | | |') play_voice(0) if voice_2_pin.value:
def get_encoder_change(encoder, pos): new_position = encoder.position if pos is None: return (new_position, 0) else: return (new_position, new_position - pos) #-------------------------------------------------------------------------------- # Main loop while True: now = time.monotonic() button.update() if mode == 1: if now >= (last_movement_at + sweep_time / 36): last_movement_at = now angle += delta if (angle > 180) or (angle < 0): delta *= -1 angle += delta if button.fell: servo.angle = 0 if mode == 0: mode = 1 sweep_time = 1.0 last_movement_at = now