""" 'tone_demo.py'. ================================================= a short piezo song using tone() """ import time import board import simpleio while True: for f in (262, 294, 330, 349, 392, 440, 494, 523): simpleio.tone(board.A0, f, 0.25) time.sleep(1)
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: MIT """CircuitPython Essentials PWM piezo simpleio example""" import time import board import simpleio while True: for f in (262, 294, 330, 349, 392, 440, 494, 523): # For the M0 boards: simpleio.tone(board.A2, f, 0.25) # on for 1/4 second # For the M4 boards: # simpleio.tone(board.A1, f, 0.25) # on for 1/4 second time.sleep(0.05) # pause between notes time.sleep(0.5)
def tick(self): """Make tick sound via piezo.""" tone(self._piezo, 256, duration=0.030)
if not btn1.value: # button 1 pressed start = True # Check button 2 (GP21) elif not btn2.value: # button 2 pressed start = False if start: # Move motors at 50% speed motor1.throttle = 0.8 # motor1.throttle = 1 or -1 for full speed motor2.throttle = 0.8 distancel = vl53l.range distancep = vl53p.range #print("Rangel: {0}mm".format(distancel)) #print("Rangep: {0}mm".format(distancep)) if distancel < 150 or distancep < 150: simpleio.tone(piezo, 450, 0.1) # Move motors at 50% speed motor1.throttle = -0.5 # motor1.throttle = 1 or -1 for full speed motor2.throttle = -0.5 time.sleep(0.5) # Move motors at 50% speed if distancel <= distancep: motor1.throttle = 0.5 # motor1.throttle = 1 or -1 for full speed motor2.throttle = -0.5 else: motor1.throttle = -0.5 # motor1.throttle = 1 or -1 for full speed motor2.throttle = 0.5 turn_time = randint(2,4)/10 time.sleep(turn_time)
""" import time import board from simpleio import tone NOTES = "ccdcfeccdcgf " BEATS = [1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 4] TONES = { "c": 263, "d": 293, "e": 329, "f": 349, "g": 391, "a": 440, "b": 493, "C": 523 } TEMPO = 300 # play the notes! for i, item in enumerate(NOTES): tempodelay = 60 / TEMPO note = NOTES[i] beat = BEATS[i] print(note, end="") if note == " ": time.sleep(beat * tempodelay) else: tone(board.D9, TONES[note], beat * tempodelay) time.sleep(tempodelay / 2)
# uncomment this line to see the values of the pot # print((ss.analog_read(pot),)) # time.sleep(0.25) # maps the range of the pot to the range of the servo angle = map_range(ss.analog_read(pot), 0, 1023, 180, 0) # sets the servo equal to the relative position on the pot crickit.servo_1.angle = angle # Light sensor + DC motor # uncomment to see values of light # print(light_in.value) # time.sleep(0.5) # reads the on-board light sensor and graphs the brighness with NeoPixels # light value remaped to motor speed peak = map_range(light_in.value, 3000, 62000, 0, 1) # DC motor crickit.dc_motor_1.throttle = peak # full speed forward # hit both buttons to trigger noise if not ss.digital_read(BUTTON_1) and not ss.digital_read(BUTTON_2): print("Buttons 1 and 2 pressed") for f in (262, 294, 330, 349, 392, 440, 494, 523): tone(board.A0, f, 0.25) time.sleep(SLEEP_DELAY)
def alarm(self): self.buzzer = simpleio.tone(self.b1, 440, duration=1.0) return self.buzzer
time.sleep(2) motion = 0 while True: pir_value = pir.value #reads sensors and reports to serial monitor light = photocell.value voltage = analog_voltage(photocell)*1000 print('Photoresistor value: {0} voltage: {1}mV'.format(light, voltage)) print("Temperature: ", am.temperature*1.8 + 32) print("Humidity: ", am.relative_humidity) if pir.value == 0: if not old_value: print("Motion Detected!") simpleio.tone(buzzer, 262, duration=0.5) time.sleep(3) motion = 1 if pir.value == 0: if not old_value: print("Motion Ended") motion = 0 #defining fields. variables will be added to API write feed request at the end. more fields can be added if needed field1 = '&field1=' field2 = '&field2=' field3 = '&field3=' field4 = '&field4=' #defines what is going to be sent to field. data1 follows '&field1='. more data can be added if needed data1 = str(voltage)
stemma_connected = False print("Stemma device disconnected") time.sleep(3) # Analog input on VOLTAGE_MONITOR v_plus = AnalogIn(board.VOLTAGE_MONITOR) # Motor controller outputs motor_out_1 = DigitalInOut(board.MOTOR_OUT_1) motor_out_1.direction = Direction.OUTPUT motor_out_2 = DigitalInOut(board.MOTOR_OUT_2) motor_out_2.direction = Direction.OUTPUT # Piezo speaker outputs piezo = board.PIEZO tone(piezo, 880, duration=0.3) # A5 # Sensor input with pullup on SENSOR_IN sensor = DigitalInOut(board.SENSOR_IN) sensor.direction = Direction.INPUT sensor.pull = Pull.UP ######################### HELPERS ############################## # Helper to convert analog input to voltage def getVoltage(pin): return 2 * (pin.value * 3.3) / 65536 # Helper to give us a nice color swirl def wheel(pos):
def play_tone(freq=440, duration=0.01): tone(board.A0, freq, duration) return
def set_datetime(self, xst_datetime): """Manual input of time using a rotary encoder.""" self._xst_datetime = xst_datetime self._display.colon = False if self._sel_sw.value: # Select switch not pressed # return datetime, sound flag, and "no change" flag return self._xst_datetime, self._sound, False tone(self._piezo, 784, duration=0.300) # G5 piezo while not self._sel_sw.value: # Wait until switch is released pass self.show(self._xst_datetime, date=True) time.sleep(0.5) self._param_val_list[0] = self._xst_datetime.tm_mon self._param_val_list[1] = self._xst_datetime.tm_mday self._param_val_list[2] = self._xst_datetime.tm_year self._param_val_list[3] = self._xst_datetime.tm_hour self._param_val_list[4] = self._xst_datetime.tm_min if self._sound: self._param_val_list[5] = 1 else: self._param_val_list[5] = 0 self._param_index = 0 # Reset index of parameter list self._enc.position = 0 # Reset encoder position value self._changed = False # Reset edit change flag # Select parameter to change self._t0 = time.monotonic() # Start the timeout clock # While select switch not pressed while self._sel_sw.value and time.monotonic() - self._t0 < 10: self._t0 = time.monotonic() # Start the timeout clock # While select switch not pressed while self._sel_sw.value and time.monotonic() - self._t0 < 10: self._param_index = self._enc.position self._param_index = max(0, min(7, self._param_index)) if self._enc.position != self._param_index: self._t0 = time.monotonic() # Start the timeout clock self._enc.position = self._param_index ### Display parameter prompt self._param_text(self._param_index) time.sleep(0.15) # Select switch pressed tone(self._piezo, 880, duration=0.3) # A5 piezo while not self._sel_sw.value: # Wait for switch to release pass if self._param_index == 7: # EXIT was selected self._t0 = 0 # Force process to skip value section # Adjust parameter value while self._sel_sw.value and time.monotonic( ) - self._t0 < 5: # Select switch not pressed self._changed = False # Parameter edits and actions self._t0 = time.monotonic() # Start the timeout clock self._enc.position = self._param_val_list[self._param_index] # While select switch not pressed while self._sel_sw.value and time.monotonic() - self._t0 < 10: self._param_value = self._enc.position self._min = self._param_list[self._param_index][1] # Min self._max = self._param_list[self._param_index][2] # Max self._param_value = max(self._min, min(self._max, self._param_value)) if self._enc.position != self._param_value: self._t0 = time.monotonic() # Start the timeout clock self._enc.position = self._param_value # Display parameter prompt if self._param_index == 0: # Month value self._display.print("{:02d} ".format( self._param_value)) self._changed = True if self._param_index == 2: # 4-digit year vallue self._display.print("{:04d}".format(self._param_value)) self._changed = True if self._param_index in [1, 3, 4]: self._display.print(" {:02d}".format( self._param_value)) self._changed = True if self._param_index == 5: # Sound effects flag if self._param_value == 1: self._display.print(" 1") else: self._display.print(" 0") if self._param_index == 6: # Display _brightness self._brightness = self._param_value / 10 self._display.brightness = self._brightness self._display.print(" {:02d}".format( self._param_value)) time.sleep(0.2) self._param_val_list[self._param_index] = self._param_value self._enc.position = self._param_index + 1 # Move to next param time.sleep(0.2) # Select switch pressed tone(self._piezo, 880, duration=0.3) # A5 piezo while not self._sel_sw.value: # Wait for select switch release pass time.sleep(0.2) # Exit setup process if not self._sel_sw.value: # Select switch pressed pass while not self._sel_sw.value: # Wait for select switch release pass self._sound = self._param_val_list[5] # Structured time: (year, mon, date, hour, min, sec, wday, yday, isdst) self._xst_datetime = time.struct_time( (self._param_val_list[2], self._param_val_list[0], self._param_val_list[1], self._param_val_list[3], self._param_val_list[4], 1, -1, -1, -1)) # Fix weekday and yearday structured time errors self._xst_datetime = time.localtime(time.mktime(self._xst_datetime)) if self._changed: self._display.marquee("- ", delay=0.2, loop=False) self.show(self._xst_datetime, date=True) tone(self._piezo, 784, duration=0.3) # G5 piezo # return with new datetime, sound flag, and "time was changed" flag return self._xst_datetime, self._sound, self._changed
""" 'tone_demo.py'. ================================================= a short piezo song using tone() """ import time import board import simpleio while True: for f in (262, 294, 330, 349, 392, 440, 494, 523): simpleio.tone(board.A0, f, 0.25) time.sleep(1)