Raspberry Pi – Buzzer speaker via GPIO
Hardware
Dit voorbeeld laat zien hoe je een toon en duur kan laten horen met een buzzer of speaker via via de GPIO pinnen van een Raspberry Pi doormiddel van een Python script.
Wat is een piëzo-elektrische luidspreker
Een keramische luidspreker, ook wel piëzo-elektrische luidspreker genoemd, is een type luidspreker.
Het stralend element is hier een keramisch materiaal dat onder invloed van een aangelegde elektrische spanning vervormt. Het Piëzo-elektrisch effect. Sommige kristallijne of keramische materialen vervormen onder invloed van een aangelegde elektrische spanning.
Deze luidsprekers zijn gebaseerd op het feit dat bij sommige kristallen bij het aanbrengen van een spanning er een buiging van het kristal ontstaat. De beweging is beperkt. Daarom wordt dit principe enkel gebruikt bij tweeters. Een tweeter heeft over het algemeen een frequentiebereik van 2000 tot 20.000 hertz. Sommige tweeters gaan nog hoger, al is dit boven de gehoorgrens van het menselijk gehoor.
Het zijn goedkope en minder goede hoogtonige luidsprekers. Meestal vindt men ze ingebouwd in een trechtervormige plastic behuizing. Ze zijn alleen geschikt voor de hoge frequenties. De geluidskwaliteit is over het algemeen matig. Deze luidsprekers kan men aantreffen in polsuurwerken, goedkope boxen.
Het omgekeerde effect bestaat ook. Het Piëzo-elektrisch effect. Diezelfde materialen wekken een elektrische spanning op als ze vervormd worden. Dit effect wordt in tal van toepassingen gebruikt: Piëzo-elektrische en keramische pick-up elementen voor platendraaier, gitaar, microfoons. Gasaanstekers en sigarettenaanstekers.
Bron: Wikipedia
Aansluiten op de Raspberry Pi
Sluit de Buzzer speaker servo aan zoals aangegeven op onderstaand schema:
Script zonder toonfrequentie:
Met onderstaand python script kun je een simpel een duur opgeven om de speakerpin hoog te maken, te gebruiken bij een buzzer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import RPi.GPIO as GPIO # Importeer de GPIO bibliotheek. import time ## Importeer de bibliotheek voor tijdfuncties buzzer_pin = 22 # Geef het nummer van de pin op waar de speaker is aangesloten. GPIO.setmode(GPIO.BCM) # GebruikBroadcom GPIO benaming van de pinnen. GPIO.setup(buzzer_pin, GPIO.OUT) # Zet de speaker pin als uitgang. print "Druk op CTRL+C om het programma te beeindigen." try: while True: GPIO.output(buzzer_pin, True) # Zet speakerpin hoog. time.sleep(.3) # Wacht even. GPIO.output(buzzer_pin, False) # Zet speakerpin laag. time.sleep(.5) # Wacht even. except KeyboardInterrupt: GPIO.output(buzzer_pin, False) # Zet speakerpin laag. # GPIO netjes afsluiten GPIO.cleanup() |
Script met toonfrequentie
Met onderstaand python script kun je een frequentie en duur opgeven gevolgd door enter de speaker zal dan een toon laten horen.
Ps. commentaar regels in het script zijn Engels.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import RPi.GPIO as GPIO #import the GPIO library import time #import the time library buzzer_pin = 22 #set the buzzer pin variable to number 18 GPIO.setmode(GPIO.BCM)#Use the Broadcom method for naming the GPIO pins GPIO.setup(buzzer_pin, GPIO.OUT) #Set pin 22 as an output pin def buzz(pitch, duration): #create the function "buzz" and feed it the pitch and duration) period = 1.0 / pitch #in physics, the period (sec/cyc) is the inverse of the frequency (cyc/sec) delay = period / 2 #calcuate the time for half of the wave cycles = int(duration * pitch) #the number of waves to produce is the duration times the frequency for i in range(cycles): #start a loop from 0 to the variable "cycles" calculated above GPIO.output(buzzer_pin, True) #set pin 18 to high time.sleep(delay) #wait with pin 18 high GPIO.output(buzzer_pin, False) #set pin 18 to low time.sleep(delay) #wait with pin 18 low while True: #start infinite loop pitch_s = raw_input("Enter Pitch (200 to 2000): ") #ask the user to type in the pitch pitch = float(pitch_s) #convert user input to a floating decimal duration_s = raw_input("Enter Duration (seconds): ") #ask the user to type in the duration duration = float(duration_s) #convert user input to a floating decimal buzz(pitch, duration) #feed the pitch and duration to the function, "buzz" |
Start het script met: sudo python toon.py , druk op CTRL+C om het script te stoppen!
Bron:Â raspberrypikid.wordpress.com
Script with melodies
Source: https://github.com/gumslone/raspi_buzzer_player/blob/master/buzzer_player.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 |
#!/usr/bin/env python #--------------------------------------------------- # This is free and unencumbered software released into the public domain. # # Anyone is free to copy, modify, publish, use, compile, sell, or # distribute this software, either in source code form or as a compiled # binary, for any purpose, commercial or non-commercial, and by any # means. # # In jurisdictions that recognize copyright laws, the author or authors # of this software dedicate any and all copyright interest in the # software to the public domain. We make this dedication for the benefit # of the public at large and to the detriment of our heirs and # successors. We intend this dedication to be an overt act of # relinquishment in perpetuity of all present and future rights to this # software under copyright law. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. # # For more information, please refer to <http://unlicense.org> #--------------------------------------------------- # # Passive buzzer Pi # VCC ----------------- 3.3V # GND ------------------ GND # SIG ---------------- Pin Gpio27 # # some notes for melodies were taken from: # http://www.astlessons.com/pianoforkids1.html # http://www.astlessons.com/pianoforkids2.html # where you can get more notes # #--------------------------------------------------- import RPi.GPIO as GPIO import time buzzer_pin = 27 notes = { 'B0' : 31, 'C1' : 33, 'CS1' : 35, 'D1' : 37, 'DS1' : 39, 'EB1' : 39, 'E1' : 41, 'F1' : 44, 'FS1' : 46, 'G1' : 49, 'GS1' : 52, 'A1' : 55, 'AS1' : 58, 'BB1' : 58, 'B1' : 62, 'C2' : 65, 'CS2' : 69, 'D2' : 73, 'DS2' : 78, 'EB2' : 78, 'E2' : 82, 'F2' : 87, 'FS2' : 93, 'G2' : 98, 'GS2' : 104, 'A2' : 110, 'AS2' : 117, 'BB2' : 123, 'B2' : 123, 'C3' : 131, 'CS3' : 139, 'D3' : 147, 'DS3' : 156, 'EB3' : 156, 'E3' : 165, 'F3' : 175, 'FS3' : 185, 'G3' : 196, 'GS3' : 208, 'A3' : 220, 'AS3' : 233, 'BB3' : 233, 'B3' : 247, 'C4' : 262, 'CS4' : 277, 'D4' : 294, 'DS4' : 311, 'EB4' : 311, 'E4' : 330, 'F4' : 349, 'FS4' : 370, 'G4' : 392, 'GS4' : 415, 'A4' : 440, 'AS4' : 466, 'BB4' : 466, 'B4' : 494, 'C5' : 523, 'CS5' : 554, 'D5' : 587, 'DS5' : 622, 'EB5' : 622, 'E5' : 659, 'F5' : 698, 'FS5' : 740, 'G5' : 784, 'GS5' : 831, 'A5' : 880, 'AS5' : 932, 'BB5' : 932, 'B5' : 988, 'C6' : 1047, 'CS6' : 1109, 'D6' : 1175, 'DS6' : 1245, 'EB6' : 1245, 'E6' : 1319, 'F6' : 1397, 'FS6' : 1480, 'G6' : 1568, 'GS6' : 1661, 'A6' : 1760, 'AS6' : 1865, 'BB6' : 1865, 'B6' : 1976, 'C7' : 2093, 'CS7' : 2217, 'D7' : 2349, 'DS7' : 2489, 'EB7' : 2489, 'E7' : 2637, 'F7' : 2794, 'FS7' : 2960, 'G7' : 3136, 'GS7' : 3322, 'A7' : 3520, 'AS7' : 3729, 'BB7' : 3729, 'B7' : 3951, 'C8' : 4186, 'CS8' : 4435, 'D8' : 4699, 'DS8' : 4978 } melody = [ notes['E7'], notes['E7'], 0, notes['E7'], 0, notes['C7'], notes['E7'], 0, notes['G7'], 0, 0, 0, notes['G6'], 0, 0, 0, notes['C7'], 0, 0, notes['G6'], 0, 0, notes['E6'], 0, 0, notes['A6'], 0, notes['B6'], 0, notes['AS6'], notes['A6'], 0, notes['G6'], notes['E7'], notes['G7'], notes['A7'], 0, notes['F7'], notes['G7'], 0, notes['E7'], 0, notes['C7'], notes['D7'], notes['B6'], 0, 0, notes['C7'], 0, 0, notes['G6'], 0, 0, notes['E6'], 0, 0, notes['A6'], 0, notes['B6'], 0, notes['AS6'], notes['A6'], 0, notes['G6'], notes['E7'], notes['G7'], notes['A7'], 0, notes['F7'], notes['G7'], 0, notes['E7'], 0, notes['C7'], notes['D7'], notes['B6'], 0, 0 ] tempo = [ 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, ] underworld_melody = [ notes['C4'], notes['C5'], notes['A3'], notes['A4'], notes['AS3'], notes['AS4'], 0, 0, notes['C4'], notes['C5'], notes['A3'], notes['A4'], notes['AS3'], notes['AS4'], 0, 0, notes['F3'], notes['F4'], notes['D3'], notes['D4'], notes['DS3'], notes['DS4'], 0, 0, notes['F3'], notes['F4'], notes['D3'], notes['D4'], notes['DS3'], notes['DS4'], 0, 0, notes['DS4'], notes['CS4'], notes['D4'], notes['CS4'], notes['DS4'], notes['DS4'], notes['GS3'], notes['G3'], notes['CS4'], notes['C4'], notes['FS4'], notes['F4'], notes['E3'], notes['AS4'], notes['A4'], notes['GS4'], notes['DS4'], notes['B3'], notes['AS3'], notes['A3'], notes['GS3'], 0, 0, 0 ] underworld_tempo = [ 12, 12, 12, 12, 12, 12, 6, 3, 12, 12, 12, 12, 12, 12, 6, 3, 12, 12, 12, 12, 12, 12, 6, 3, 12, 12, 12, 12, 12, 12, 6, 6, 18, 18, 18, 6, 6, 6, 6, 6, 6, 18, 18, 18, 18, 18, 18, 10, 10, 10, 10, 10, 10, 3, 3, 3 ] adventure_time_melody = [ notes['D5'], notes['G5'], notes['G5'], notes['G5'], notes['G5'], notes['FS5'], notes['FS5'], notes['E5'], notes['D5'], notes['E5'], notes['D5'], notes['D5'], notes['C5'], notes['B5'], notes['A5'], notes['G4'], 0, notes['C5'], notes['B5'], notes['A5'], notes['G4'], 0, notes['G5'], 0, notes['G5'], notes['G5'], 0, notes['G5'], notes['FS5'], 0, notes['E5'], notes['E5'], notes['D5'], notes['D5'], notes['C5'], notes['C5'], notes['C5'], notes['D5'], notes['D5'], notes['A5'], notes['B5'], notes['A5'], notes['G4'], notes['G5'] ] adventure_time_tempo = [ 24, 24, 12, 12, 12, 24, 12, 24, 24, 24, 12, 24, 12, 12, 12, 12, 24, 12, 24, 24, 12, 24, 24, 24, 24, 12, 24, 12, 24, 24, 24, 12, 12, 24, 8, 24, 24, 8, 8, 24, 12, 24, 24, 12 ] star_wars_melody = [ notes['G4'], notes['G4'], notes['G4'], notes['EB4'], 0, notes['BB4'], notes['G4'], notes['EB4'], 0, notes['BB4'], notes['G4'], 0, notes['D4'], notes['D4'], notes['D4'], notes['EB4'], 0, notes['BB3'], notes['FS3'], notes['EB3'], 0, notes['BB3'], notes['G3'], 0, notes['G4'], 0, notes['G3'], notes['G3'], 0, notes['G4'], 0, notes['FS4'], notes['F4'], notes['E4'], notes['EB4'], notes['E4'], 0, notes['GS3'], notes['CS3'], 0, notes['C3'], notes['B3'], notes['BB3'], notes['A3'], notes['BB3'], 0, notes['EB3'], notes['FS3'], notes['EB3'], notes['FS3'], notes['BB3'], 0, notes['G3'], notes['BB3'], notes['D4'], 0, notes['G4'], 0, notes['G3'], notes['G3'], 0, notes['G4'], 0, notes['FS4'], notes['F4'], notes['E4'], notes['EB4'], notes['E4'], 0, notes['GS3'], notes['CS3'], 0, notes['C3'], notes['B3'], notes['BB3'], notes['A3'], notes['BB3'], 0, notes['EB3'], notes['FS3'], notes['EB3'], notes['BB3'], notes['G3'], notes['EB3'], 0, notes['BB3'], notes['G3'], ] star_wars_tempo = [ 2, 2, 2, 4, 8, 6, 2, 4, 8, 6, 2, 8, 2, 2, 2, 4, 8, 6, 2, 4, 8, 6, 2, 8, 2, 16, 4, 4, 8, 2, 8, 4, 6, 6, 4, 4, 8, 4, 2, 8, 4, 4, 6, 4, 2, 8, 4, 2, 4, 4, 2, 8, 4, 6, 2, 8, 2, 16, 4, 4, 8, 2, 8, 4, 6, 6, 4, 4, 8, 4, 2, 8, 4, 4, 6, 4, 2, 8, 4, 2, 2, 4, 2, 4, 8, 4, 2, ] popcorn_melody = [ notes['A4'], notes['G4'], notes['A4'], notes['E4'], notes['C4'], notes['E4'], notes['A3'], notes['A4'], notes['G4'], notes['A4'], notes['E4'], notes['C4'], notes['E4'], notes['A3'], notes['A4'], notes['B4'], notes['C5'], notes['B4'], notes['C5'], notes['A4'], notes['B4'], notes['A4'], notes['B4'], notes['G4'], notes['A4'], notes['G4'],notes['A4'], notes['F4'], notes['A4'], notes['A4'], notes['G4'], notes['A4'], notes['E4'], notes['C4'], notes['E4'], notes['A3'], notes['A4'], notes['G4'], notes['A4'], notes['E4'], notes['C4'], notes['E4'], notes['A3'], notes['A4'], notes['B4'], notes['C5'], notes['B4'], notes['C5'], notes['A4'], notes['B4'], notes['A4'], notes['B4'], notes['G4'], notes['A4'], notes['G4'],notes['A4'], notes['B4'], notes['C5'], notes['E5'], notes['D5'], notes['E5'], notes['C5'], notes['G4'], notes['C5'], notes['E4'], notes['E5'], notes['D5'], notes['E5'], notes['C5'], notes['G4'], notes['C5'], notes['E4'], notes['E5'], notes['FS5'], notes['G5'], notes['FS5'], notes['G5'], notes['E5'], notes['FS5'], notes['E5'], notes['FS5'], notes['D5'], notes['E5'], notes['D5'],notes['E5'], notes['C5'], notes['E5'], ### notes['E5'], notes['D5'], notes['E5'], notes['C5'], notes['G4'], notes['C5'], notes['E4'], notes['E5'], notes['D5'], notes['E5'], notes['C5'], notes['G4'], notes['C5'], notes['E4'], notes['E5'], notes['FS5'], notes['G5'], notes['FS5'], notes['G5'], notes['E5'], notes['FS5'], notes['E5'], notes['FS5'], notes['D5'], notes['E5'], notes['D5'],notes['B4'], notes['D5'], notes['E5'], ] popcorn_tempo = [ 8,8,8,8,8,8,4, 8,8,8,8,8,8,4, 8,8,8,8,8,8,8,8,8,8, 8,8,8,8,4, 8,8,8,8,8,8,4, 8,8,8,8,8,8,4, 8,8,8,8,8,8,8,8,8,8, 8,8,8,8,4, 8,8,8,8,8,8,4, 8,8,8,8,8,8,4, 8,8,8,8,8,8,8,8,8,8, 8,8,8,8,4, 8,8,8,8,8,8,4, 8,8,8,8,8,8,4, 8,8,8,8,8,8,8,8,8,8, 8,8,8,8,4, ] twinkle_twinkle_melody = [ notes['C4'], notes['C4'], notes['G4'], notes['G4'], notes['A4'], notes['A4'], notes['G4'], notes['F4'], notes['F4'], notes['E4'], notes['E4'], notes['D4'], notes['D4'], notes['C4'], notes['G4'], notes['G4'], notes['F4'], notes['F4'], notes['E4'], notes['E4'], notes['D4'], notes['G4'], notes['G4'], notes['F4'], notes['F4'], notes['E4'], notes['E4'], notes['D4'], notes['C4'], notes['C4'], notes['G4'], notes['G4'], notes['A4'], notes['A4'], notes['G4'], notes['F4'], notes['F4'], notes['E4'], notes['E4'], notes['D4'], notes['D4'], notes['C4'], ] twinkle_twinkle_tempo = [ 4,4,4,4,4,4,2, 4,4,4,4,4,4,2, 4,4,4,4,4,4,2, 4,4,4,4,4,4,2, 4,4,4,4,4,4,2, 4,4,4,4,4,4,2, ] crazy_frog_melody = [ notes['A4'], notes['C5'], notes['A4'], notes['A4'], notes['D5'], notes['A4'], notes['G4'], notes['A4'], notes['E5'], notes['A4'], notes['A4'], notes['F5'], notes['E5'], notes['C5'], notes['A4'], notes['E5'], notes['A5'], notes['A4'], notes['G4'], notes['G4'], notes['E4'], notes['B4'], notes['A4'],0, notes['A4'], notes['C5'], notes['A4'], notes['A4'], notes['D5'], notes['A4'], notes['G4'], notes['A4'], notes['E5'], notes['A4'], notes['A4'], notes['F5'], notes['E5'], notes['C5'], notes['A4'], notes['E5'], notes['A5'], notes['A4'], notes['G4'], notes['G4'], notes['E4'], notes['B4'], notes['A4'],0, notes['A3'], notes['G3'], notes['E3'], notes['D3'], notes['A4'], notes['C5'], notes['A4'], notes['A4'], notes['D5'], notes['A4'], notes['G4'], notes['A4'], notes['E5'], notes['A4'], notes['A4'], notes['F5'], notes['E5'], notes['C5'], notes['A4'], notes['E5'], notes['A5'], notes['A4'], notes['G4'], notes['G4'], notes['E4'], notes['B4'], notes['A4'], ] crazy_frog_tempo = [ 2,4,4,8,4,4,4, 2,4,4,8,4,4,4, 4,4,4,8,4,8,4,4, 1,4, 2,4,4,8,4,4,4, 2,4,4,8,4,4,4, 4,4,4,8,4,8,4,4, 1,4, 8,4,4,4, 2,4,4,8,4,4,4, 2,4,4,8,4,4,4, 4,4,4,8,4,8,4,4, 1, ] deck_the_halls_melody = [ notes['G5'], notes['F5'], notes['E5'], notes['D5'], notes['C5'], notes['D5'], notes['E5'], notes['C5'], notes['D5'], notes['E5'], notes['F5'], notes['D5'], notes['E5'], notes['D5'], notes['C5'], notes['B4'], notes['C5'], 0, notes['G5'], notes['F5'], notes['E5'], notes['D5'], notes['C5'], notes['D5'], notes['E5'], notes['C5'], notes['D5'], notes['E5'], notes['F5'], notes['D5'], notes['E5'], notes['D5'], notes['C5'], notes['B4'], notes['C5'], 0, notes['D5'], notes['E5'], notes['F5'], notes['D5'], notes['E5'], notes['F5'], notes['G5'], notes['D5'], notes['E5'], notes['F5'], notes['G5'], notes['A5'], notes['B5'], notes['C6'], notes['B5'], notes['A5'], notes['G5'], 0, notes['G5'], notes['F5'], notes['E5'], notes['D5'], notes['C5'], notes['D5'], notes['E5'], notes['C5'], notes['D5'], notes['E5'], notes['F5'], notes['D5'], notes['E5'], notes['D5'], notes['C5'], notes['B4'], notes['C5'], 0, ] deck_the_halls_tempo = [ 2, 4, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 2, 4, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 2, 4, 2, 2, 2, 2, 2,4,2,2, 2,4,2,2, 4,4,2,4,4,2, 2,2,2,2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 2, 4, 2, 2, 2, 2, ] manaderna_melody = [ notes['E4'],notes['E4'],notes['F4'],notes['G4'], notes['G4'],notes['F4'],notes['E4'],notes['D4'], notes['C4'],notes['C4'],notes['D4'],notes['E4'], notes['E4'],0,notes['D4'],notes['D4'],0, notes['E4'],notes['E4'],notes['F4'],notes['G4'], notes['G4'],notes['F4'],notes['E4'],notes['D4'], notes['C4'],notes['C4'],notes['D4'],notes['E4'], notes['D4'],0,notes['C4'],notes['C4'],0, notes['D4'],notes['D4'],notes['E4'],notes['C4'], notes['D4'],notes['E4'],notes['F4'],notes['E4'],notes['C4'], notes['D4'],notes['E4'],notes['F4'],notes['E4'],notes['D4'], notes['C4'],notes['D4'],notes['G3'],0, notes['E4'],notes['E4'],notes['F4'],notes['G4'], notes['G4'],notes['F4'],notes['E4'],notes['D4'], notes['C4'],notes['C4'],notes['D4'],notes['E4'], notes['D4'],0,notes['C4'],notes['C4'], ] manaderna_tempo = [ 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,4,4,2,4, 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,4,4,2,4, 2,2,2,2, 2,4,4,2,2, 2,4,4,2,2, 2,2,1,4, 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,4,4,2, ] bonnagard_melody = [ notes['C5'],notes['C5'],notes['C5'],notes['G4'], notes['A4'],notes['A4'],notes['G4'], notes['E5'],notes['E5'],notes['D5'],notes['D5'], notes['C5'],0,notes['G4'], notes['C5'],notes['C5'],notes['C5'],notes['G4'], notes['A4'],notes['A4'],notes['G4'], notes['E5'],notes['E5'],notes['D5'],notes['D5'], notes['C5'],0,notes['G4'],notes['G4'], notes['C5'],notes['C5'],notes['C5'],notes['G4'],notes['G4'], notes['C5'],notes['C5'],notes['G4'], notes['C5'],notes['C5'],notes['C5'],notes['C5'],notes['C5'],notes['C5'], notes['C5'],notes['C5'],notes['C5'],notes['C5'],notes['C5'],notes['C5'],0, notes['C5'],notes['C5'],notes['C5'],notes['G4'], notes['A4'],notes['A4'],notes['G4'], notes['E5'],notes['E5'],notes['D5'],notes['D5'], notes['C5'],0, ] bonnagard_tempo = [ 2,2,2,2, 2,2,1, 2,2,2,2, 1,2,2, 2,2,2,2, 2,2,1, 2,2,2,2, 1,2,4,4, 2,2,2,4,4, 2,2,1, 4,4,2,4,4,2, 4,4,4,4,2,2,4, 2,2,2,2, 2,2,1, 2,2,2,2, 1,1, ] final_countdown_melody = [ notes['A3'],notes['E5'],notes['D5'],notes['E5'],notes['A4'], notes['F3'],notes['F5'],notes['E5'],notes['F5'],notes['E5'],notes['D5'], notes['D3'],notes['F5'],notes['E5'],notes['F5'],notes['A4'], notes['G3'],0,notes['D5'],notes['C5'],notes['D5'],notes['C5'],notes['B4'],notes['D5'], notes['C5'],notes['A3'],notes['E5'],notes['D5'],notes['E5'],notes['A4'], notes['F3'],notes['F5'],notes['E5'],notes['F5'],notes['E5'],notes['D5'], notes['D3'],notes['F5'],notes['E5'],notes['F5'],notes['A4'], notes['G3'],0,notes['D5'],notes['C5'],notes['D5'],notes['C5'],notes['B4'],notes['D5'], notes['C5'],notes['B4'],notes['C5'],notes['D5'],notes['C5'],notes['D5'], notes['E5'],notes['D5'],notes['C5'],notes['B4'],notes['A4'],notes['F5'], notes['E5'],notes['E5'],notes['F5'],notes['E5'],notes['D5'], notes['E5'], ] final_countdown_tempo = [ 1,16,16,4,4, 1,16,16,8,8,4, 1,16,16,4,4, 2,4,16,16,8,8,8,8, 4,4,16,16,4,4, 1,16,16,8,8,4, 1,16,16,4,4, 2,4,16,16,8,8,8,8, 4,16,16,4,16,16, 8,8,8,8,4,4, 2,8,4,16,16, 1, ] def buzz(frequency, length): #create the function "buzz" and feed it the pitch and duration) if(frequency==0): time.sleep(length) return period = 1.0 / frequency #in physics, the period (sec/cyc) is the inverse of the frequency (cyc/sec) delayValue = period / 2 #calcuate the time for half of the wave numCycles = int(length * frequency) #the number of waves to produce is the duration times the frequency for i in range(numCycles): #start a loop from 0 to the variable "cycles" calculated above GPIO.output(buzzer_pin, True) #set pin 27 to high time.sleep(delayValue) #wait with pin 27 high GPIO.output(buzzer_pin, False) #set pin 27 to low time.sleep(delayValue) #wait with pin 27 low def setup(): GPIO.setmode(GPIO.BCM) GPIO.setup(buzzer_pin, GPIO.IN) GPIO.setup(buzzer_pin, GPIO.OUT) def destroy(): GPIO.cleanup() # Release resource def play(melody,tempo,pause,pace=0.800): for i in range(0, len(melody)): # Play song noteDuration = pace/tempo[i] buzz(melody[i],noteDuration) # Change the frequency along the song note pauseBetweenNotes = noteDuration * pause time.sleep(pauseBetweenNotes) if __name__ == '__main__': # Program start from here try: setup() print "The Final Countdown" play(final_countdown_melody, final_countdown_tempo, 0.30, 1.2000) time.sleep(2) print "Per Olssons Bonnagard (Old MacDonald Had A Farm) Melody" play(bonnagard_melody, bonnagard_tempo, 0.30, 0.800) time.sleep(2) print "Manaderna (Symphony No. 9) Melody" play(manaderna_melody, manaderna_tempo, 0.30, 0.800) time.sleep(2) print "Deck The Halls Melody" play(deck_the_halls_melody, deck_the_halls_tempo, 0.30, 0.800) time.sleep(2) print "Crazy Frog (Axel F) Theme" play(crazy_frog_melody, crazy_frog_tempo, 0.30, 0.900) time.sleep(2) print "Twinkle, Twinkle, Little Star Melody" play(twinkle_twinkle_melody, twinkle_twinkle_tempo, 0.50, 1.000) time.sleep(2) print "Popcorn Melody" play(popcorn_melody, popcorn_tempo, 0.50, 1.000) time.sleep(2) print "Star Wars Theme" play(star_wars_melody, star_wars_tempo, 0.50, 1.000) time.sleep(2) print "Super Mario Theme" play(melody, tempo, 1.3, 0.800) time.sleep(2) print "Super Mario Underworld Theme" play(underworld_melody, underworld_tempo, 1.3, 0.800) time.sleep(2) print "Adventure Time Theme" play(adventure_time_melody, adventure_time_tempo, 1.3, 1.500) destroy() except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. destroy() |