/ Chirping_Plush_Owl_Toy / code.py
code.py
1 # SPDX-FileCopyrightText: 2018 Becky Stern for Adafruit Industries 2 # SPDX-FileCopyrightText: 2018 T Main for Adafruit Industries 3 # 4 # SPDX-License-Identifier: MIT 5 6 # Chirp Owl written by Becky Stern and T Main for Adafruit Industries 7 # Tutorial: http://learn.adafruit.com/chirping-plush-owl-toy/ 8 9 # Includes animal sounds by Anne Barela 10 # http://learn.adafruit.com/adafruit-trinket-modded-stuffed-animal/animal-sounds 11 # based in part on Debounce 12 # created 21 November 2006 13 # by David A. Mellis 14 # modified 30 Aug 2011 15 # by Limor Fried 16 # modified 28 Dec 2012 17 # by Mike Walters 18 # CircuitPython Port 2018 19 # by Mikey Sklar 20 # This example code is in the public domain. 21 # http://www.arduino.cc/en/Tutorial/Debounce 22 23 import time 24 import board 25 import digitalio 26 27 # setup for vibration sensor 28 motion = digitalio.DigitalInOut(board.D0) 29 motion.direction = digitalio.Direction.INPUT 30 motion.pull = digitalio.Pull.UP 31 32 # setup for speaker output 33 speaker = digitalio.DigitalInOut(board.D2) 34 speaker.direction = digitalio.Direction.OUTPUT 35 36 def chirp(): 37 for i in range(200,180,-1): 38 play_tone(i,9) 39 40 # emphasis ow "me" 41 def meow(): 42 play_tone(5100,50) # "m" (short) 43 play_tone(394,180) # "eee" (long) 44 for i in range(990,1022,2): # vary "ooo" down 45 play_tone(i,8) 46 play_tone(5100,40) # "w" (short) 47 48 # cat meow (emphasis on "ow") 49 def meow2(): 50 play_tone(5100,55) # "m" (short) 51 play_tone(394,170) # "eee" (long) 52 time.sleep(0.03) # wait a tiny bit 53 for i in range(990,1022,2): # vary "ooo" down 54 play_tone(i,8) 55 play_tone(5100,40) # "w" (short) 56 57 # dog ruff 58 def ruff(): 59 for i in range(890,910,2): # "rrr" (vary down) 60 play_tone(i,3) 61 play_tone(1664,150) # "uuu" (hard to do) 62 play_tone(12200,70) # "ff" (long, hard to do) 63 64 # dog arf 65 def arf(): 66 play_tone(890,25) # "a" (short) 67 for i in range(890,910,2): # "rrr" (vary down) 68 play_tone(i,5) 69 play_tone(4545,80) # intermediate 70 play_tone(12200,70) # "ff" (shorter, hard to do) 71 72 def play_tone(tone_value, duration): 73 microseconds = 10 ** 6 # duration divider, convert to microseconds 74 75 for i in range(0, duration): 76 i += tone_value * 2 77 speaker.value = True 78 time.sleep(tone_value / microseconds) 79 speaker.value = False 80 time.sleep(tone_value / microseconds) 81 82 # loop forever... 83 while True: 84 85 # reverse logic for motion pins 86 if not motion.value: 87 # bird chirp noise 88 # comment out chirp and uncomment a different sound below 89 # for more animal noises 90 chirp() 91 # meow() 92 # meow2() 93 # ruff() 94 # arf() 95 time.sleep(.5) # leave some time to complete rotation