/ Annoy_O_Matic / code.py
code.py
  1  # SPDX-FileCopyrightText: 2018 John Edgar Park for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  # Annoy-O-Matic Sound Prank Device
  6  # choose from a variety of sounds and timings to drive your victim bonkers
  7  import time
  8  
  9  import board
 10  import pwmio
 11  
 12  # pylint: disable=unused-variable,consider-using-enumerate,redefined-outer-name,too-many-locals
 13  piezo = pwmio.PWMOut(board.D0, duty_cycle=0, frequency=440,
 14                         variable_frequency=True)
 15  
 16  # pick the mode here:
 17  #  1 = beep
 18  #  2 = doorbell
 19  #  3 = ringtone
 20  #  4 = crickets
 21  #  5 = teen tone
 22  #  6 = demo mode
 23  annoy_mode = 3
 24  
 25  # set general parameters here
 26  interval = 300  # seconds before next annoyance. Default 300 (5 minutes)
 27  
 28  # set beep details here
 29  frequency = 5000  # pitch of the beep in Hz.              Default 5000
 30  length = 0.2  # length (duration) of the beep in seconds. Default 0.2
 31  rest = 0.05  # seconds between beep repeats.              Default 0.05
 32  
 33  # set beep/cricket details here
 34  repeat = 3  # number of times to repeat the beep/cricket. Default 3
 35  
 36  # set ringtone details here
 37  ringtone = 3  # song choices: 1 = Nokia, 2 = iPhone, 3 = Rickroll
 38  ringtone_tempo = 1.6  # suggested Nokia 0.9 , iPhone 1.3 , Rickroll 2.0
 39  
 40  
 41  def annoy_beep(frequency, length, repeat, rest, interval):
 42      for _ in range(repeat):
 43          piezo.frequency = frequency  # 2600 is a nice choice
 44          piezo.duty_cycle = 65536 // 2  # on 50%
 45          time.sleep(length)  # sound on
 46          piezo.duty_cycle = 0  # off
 47          time.sleep(rest)
 48      time.sleep(interval)  # wait time until next beep
 49  
 50  
 51  def annoy_doorbell(interval):
 52      piezo.frequency = 740
 53      piezo.duty_cycle = 65536 // 2
 54      time.sleep(0.85)
 55      piezo.duty_cycle = 0
 56      time.sleep(0.05)
 57      piezo.frequency = 588
 58      piezo.duty_cycle = 65536 // 2
 59      time.sleep(1.25)
 60      piezo.duty_cycle = 0
 61      time.sleep(interval)
 62  
 63  
 64  # pylint: disable=too-many-statements
 65  def annoy_ringtone(ringtone, tempo, interval):
 66      # ringtone 1: Nokia
 67      # ringtone 2: Apple iPhone
 68      # ringtone 3: Rick Astley Never Gonna Give You Up
 69  
 70      # tempo is length of whole note in seconds, e.g. 1.5
 71      # set up time signature
 72      whole_note = tempo  # adjust this to change tempo of everything
 73      dotted_whole_note = whole_note * 1.5
 74      # these notes are fractions of the whole note
 75      half_note = whole_note / 2
 76      dotted_half_note = half_note * 1.5
 77      quarter_note = whole_note / 4
 78      dotted_quarter_note = quarter_note * 1.5
 79      eighth_note = whole_note / 8
 80      dotted_eighth_note = eighth_note * 1.5
 81      sixteenth_note = whole_note / 16
 82  
 83      # set up note values
 84      A2 = 110
 85      As2 = 117  # 's' stands for sharp: A#2
 86      Bb2 = 117
 87      B2 = 123
 88  
 89      C3 = 131
 90      Cs3 = 139
 91      Db3 = 139
 92      D3 = 147
 93      Ds3 = 156
 94      Eb3 = 156
 95      E3 = 165
 96      F3 = 175
 97      Fs3 = 185
 98      Gb3 = 185
 99      G3 = 196
100      Gs3 = 208
101      Ab3 = 208
102      A3 = 220
103      As3 = 233
104      Bb3 = 233
105      B3 = 247
106  
107      # C4 = 262
108      Cs4 = 277
109      # Db4 = 277
110      D4 = 294
111      # Ds4 = 311
112      # Eb4 = 311
113      E4 = 330
114      # F4 = 349
115      Fs4 = 370
116      # Gb4 = 370
117      G4 = 392
118      # Gs4 = 415
119      # Ab4 = 415
120      # A4 = 440
121      # As4 = 466
122      # Bb4 = 466
123      B4 = 494
124  
125      C5 = 523
126      Cs5 = 554
127      Db5 = 554
128      D5 = 587
129      Ds5 = 622
130      Eb5 = 622
131      E5 = 659
132      F5 = 698
133      Fs5 = 740
134      Gb5 = 740
135      G5 = 784
136      Gs5 = 831
137      Ab5 = 831
138      A5 = 880
139      As5 = 932
140      Bb5 = 932
141      B5 = 987
142  
143      # here's another way to express the note pitch, double the previous octave
144      C6 = C5 * 2
145      Cs6 = Cs5 * 2
146      Db6 = Db5 * 2
147      D6 = D5 * 2
148      Ds6 = Ds5 * 2
149      Eb6 = Eb5 * 2
150      E6 = E5 * 2
151      F6 = F5 * 2
152      Fs6 = Fs5 * 2
153      Gb6 = Gb5 * 2
154      G6 = G5 * 2
155      Gs6 = Gs5 * 2
156      Ab6 = Ab5 * 2
157      A6 = A5 * 2
158      As6 = As5 * 2
159      Bb6 = Bb5 * 2
160      B6 = B5 * 2
161  
162      if ringtone == 1:
163          # Nokia
164          nokia_ringtone = [[E6, eighth_note], [D6, eighth_note],
165                            [Fs5, quarter_note], [Gs5, quarter_note],
166                            [Cs6, eighth_note], [B5, eighth_note],
167                            [D5, quarter_note], [E5, quarter_note],
168                            [B5, eighth_note], [A5, eighth_note],
169                            [Cs5, quarter_note], [E5, quarter_note],
170                            [A5, whole_note]]
171  
172          for n in range(len(nokia_ringtone)):
173              piezo.frequency = (nokia_ringtone[n][0])
174              piezo.duty_cycle = 65536 // 2  # on 50%
175              time.sleep(nokia_ringtone[n][1])  # note duration
176              piezo.duty_cycle = 0  # off
177              time.sleep(0.01)
178  
179      if ringtone == 2:
180          # iPhone Marimba
181          iPhone_ringtone = [[B4, eighth_note], [G4, eighth_note],
182                             [D5, eighth_note], [G4, eighth_note],
183                             [D5, eighth_note], [E5, eighth_note],
184                             [D5, eighth_note], [G4, eighth_note],
185                             [E5, eighth_note], [D5, eighth_note],
186                             [G4, eighth_note], [D5, eighth_note]]
187  
188          for n in range(len(iPhone_ringtone)):
189              piezo.frequency = (iPhone_ringtone[n][0])
190              piezo.duty_cycle = 65536 // 2  # on 50%
191              time.sleep(iPhone_ringtone[n][1])  # note duration
192              piezo.duty_cycle = 0  # off
193              time.sleep(0.01)
194  
195      if ringtone == 3:
196          # Rickroll
197          rick_ringtone = [[A3, sixteenth_note], [B3, sixteenth_note],
198                           [D4, sixteenth_note], [B3, sixteenth_note],
199                           [Fs4, dotted_eighth_note], [Fs4, sixteenth_note],
200                           [Fs4, eighth_note], [E4, eighth_note],
201                           [E4, quarter_note],
202                           [A3, sixteenth_note], [B3, sixteenth_note],
203                           [Cs4, sixteenth_note], [A3, sixteenth_note],
204                           [E4, dotted_eighth_note], [E4, sixteenth_note],
205                           [E4, eighth_note], [D4, eighth_note],
206                           [D4, sixteenth_note], [Cs4, sixteenth_note],
207                           [B3, eighth_note]]
208  
209          for n in range(len(rick_ringtone)):
210              piezo.frequency = (rick_ringtone[n][0])
211              piezo.duty_cycle = 65536 // 2  # on 50%
212              time.sleep(rick_ringtone[n][1])  # note duration
213              piezo.duty_cycle = 0  # off
214              time.sleep(0.035)
215  
216      time.sleep(interval)
217  
218  
219  def annoy_crickets(repeat, interval):
220      for _ in range(repeat):
221          for _ in range(6):
222              piezo.frequency = 8000  # 2600 is a nice choice
223              piezo.duty_cycle = 65536 // 2  # on 50%
224              time.sleep(0.02)  # sound on
225              piezo.duty_cycle = 0  # off
226              time.sleep(0.05)
227          time.sleep(0.2)
228      time.sleep(interval)  # wait time until next beep
229  
230  
231  def annoy_teen_tone(interval):
232      piezo.frequency = 17400
233      piezo.duty_cycle = 65536 // 2  # on 50%
234      time.sleep(10)
235      piezo.duty_cycle = 0
236      time.sleep(interval)
237  
238  
239  while True:
240      if annoy_mode == 1:
241          annoy_beep(frequency, length, repeat, rest, interval)
242      elif annoy_mode == 2:
243          annoy_doorbell(interval)
244      elif annoy_mode == 3:
245          annoy_ringtone(ringtone, ringtone_tempo, interval)
246      elif annoy_mode == 4:
247          annoy_crickets(repeat, interval)
248      elif annoy_mode == 5:
249          annoy_teen_tone(interval)
250      elif annoy_mode == 6:
251          annoy_beep(5000, 0.2, 2, 0.05, 3)
252          annoy_doorbell(3)
253          annoy_ringtone(1, 0.9, 3)
254          annoy_ringtone(2, 1.3, 3)
255          annoy_ringtone(3, 2.0, 3)
256          annoy_crickets(3, 3)
257          annoy_teen_tone(6)