{"id":16635,"date":"2021-02-24T23:18:43","date_gmt":"2021-02-25T04:18:43","guid":{"rendered":"https:\/\/scruss.com\/blog\/?p=16635"},"modified":"2024-02-19T10:28:42","modified_gmt":"2024-02-19T15:28:42","slug":"nyan-cat-except-it-gets-faster-rtttl-on-the-raspberry-pi-pico","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2021\/02\/24\/nyan-cat-except-it-gets-faster-rtttl-on-the-raspberry-pi-pico\/","title":{"rendered":"Nyan Cat, except it gets faster \u2014 RTTTL on the Raspberry Pi Pico"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"383\" src=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/02\/spkr.jpg\" alt=\"Raspberry Pi Pico with small piezo speaker connected to pins 23 (ground) and 26 (GPIO 20)\" class=\"wp-image-16636\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/02\/spkr.jpg 680w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/02\/spkr-320x180.jpg 320w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/02\/spkr-160x90.jpg 160w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><figcaption class=\"wp-element-caption\">piezo between pins 26 and 23. Sounds a little like <a href=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2024\/02\/Stewart-Russell-hello-i-am-terrible-RaspberryPiPico-MicroPython-1364646564363440130.mp3\">this<\/a><\/figcaption><\/figure>\n\n\n\n<p>It was inevitable:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A <a href=\"https:\/\/www.raspberrypi.org\/documentation\/rp2040\/getting-started\/\">Raspberry Pi Pico<\/a>; plus<\/li>\n\n\n\n<li>a tiny piezo PC beeper; plus<\/li>\n\n\n\n<li>MicroPython; plus<\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/dhylands\/upy-rtttl\">dhylands \/ upy-rtttl<\/a>; plus<\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/KohaSuomi\/emb-rtttl\/blob\/master\/rtttl\/nyancat.rtttl\">nyancat.rtttl<\/a>; plus<\/li>\n\n\n\n<li>my unfailing sense of knowing when to stop, then ignoring it<\/li>\n<\/ol>\n\n\n\n<p>brings you this wonderful creation, which plays the Nyan Cat theme forever, except it gets 20% faster each time. This is weapons-grade annoying (thank&#8217;ee kindly), so I&#8217;m not going to include a recording here. If you must hear an approximation, paste the RTTTL into <a href=\"https:\/\/adamonsoon.github.io\/rtttl-play\/\">Play RTTTL Online<\/a> and enjoy.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Raspberry Pi Pico RTTTL example\n# nyan cat, but it gets faster\n# scruss - 2021-02: sorry, not sorry ...\n\n# Uses rtttl.py from\n#  github.com\/dhylands\/upy-rtttl\n# Nyan Cat RTTTL from\n#  github.com\/KohaSuomi\/emb-rtttl\/blob\/master\/rtttl\/nyancat.rtttl\n\nfrom rtttl import RTTTL\nfrom time import sleep_ms\nfrom machine import Pin, PWM\n\nb = 90    # bpm variable\n\n# pin 26 - GP20; just the right distance from GND at pin 23\n#  to use one of those PC beepers with the 4-pin headers\npwm = PWM(Pin(20))\nled = Pin(&#039;LED&#039;, Pin.OUT)\n\n\ndef play_tone(freq, msec):\n    # play RTTL notes, also flash onboard LED\n    print(&#039;freq = {:6.1f} msec = {:6.1f}&#039;.format(freq, msec))\n    if freq &gt; 0:\n        pwm.freq(int(freq))       # Set frequency\n        pwm.duty_u16(32767)       # 50% duty cycle\n    led.on()\n    sleep_ms(int(0.9 * msec))     # Play for a number of msec\n    pwm.duty_u16(0)               # Stop playing for gap between notes\n    led.off()\n    sleep_ms(int(0.1 * msec))     # Pause for a number of msec\n\n\nwhile True:\n    nyan = &#039;nyancat:d=4,o=5,b=&#039; + str(b) + &#039;:16d#6,16e6,8f#6,8b6,16d#6,16e6,16f#6,16b6,16c#7,16d#7,16c#7,16a#6,8b6,8f#6,16d#6,16e6,8f#6,8b6,16c#7,16a#6,16b6,16c#7,16e7,16d#7,16e7,16c#7,8f#6,8g#6,16d#6,16d#6,16p,16b,16d6,16c#6,16b,16p,8b,8c#6,8d6,16d6,16c#6,16b,16c#6,16d#6,16f#6,16g#6,16d#6,16f#6,16c#6,16d#6,16b,16c#6,16b,8d#6,8f#6,16g#6,16d#6,16f#6,16c#6,16d#6,16b,16d6,16d#6,16d6,16c#6,16b,16c#6,8d6,16b,16c#6,16d#6,16f#6,16c#6,16d#6,16c#6,16b,8c#6,8b,8c#6,8f#6,8g#6,16d#6,16d#6,16p,16b,16d6,16c#6,16b,16p,8b,8c#6,8d6,16d6,16c#6,16b,16c#6,16d#6,16f#6,16g#6,16d#6,16f#6,16c#6,16d#6,16b,16c#6,16b,8d#6,8f#6,16g#6,16d#6,16f#6,16c#6,16d#6,16b,16d6,16d#6,16d6,16c#6,16b,16c#6,8d6,16b,16c#6,16d#6,16f#6,16c#6,16d#6,16c#6,16b,8c#6,8b,8c#6,8b,16f#,16g#,8b,16f#,16g#,16b,16c#6,16d#6,16b,16e6,16d#6,16e6,16f#6,8b,8b,16f#,16g#,16b,16f#,16e6,16d#6,16c#6,16b,16f#,16d#,16e,16f#,8b,16f#,16g#,8b,16f#,16g#,16b,16b,16c#6,16d#6,16b,16f#,16g#,16f#,8b,16b,16a#,16b,16f#,16g#,16b,16e6,16d#6,16e6,16f#6,8b,8a#,8b,16f#,16g#,8b,16f#,16g#,16b,16c#6,16d#6,16b,16e6,16d#6,16e6,16f#6,8b,8b,16f#,16g#,16b,16f#,16e6,16d#6,16c#6,16b,16f#,16d#,16e,16f#,8b,16f#,16g#,8b,16f#,16g#,16b,16b,16c#6,16d#6,16b,16f#,16g#,16f#,8b,16b,16a#,16b,16f#,16g#,16b,16e6,16d#6,16e6,16f#6,8b,8c#6&#039;\n    tune = RTTTL(nyan)\n    print(&#039;bpm: &#039;, b)\n    for freq, msec in tune.notes():\n        play_tone(freq, msec)\n    b = int(b * 1.2)\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>It was inevitable: brings you this wonderful creation, which plays the Nyan Cat theme forever, except it gets 20% faster each time. This is weapons-grade annoying (thank&#8217;ee kindly), so I&#8217;m not going to include a recording here. If you must hear an approximation, paste the RTTTL into Play RTTTL Online and enjoy.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7,2],"tags":[],"class_list":["post-16635","post","type-post","status-publish","format-standard","hentry","category-computers-suck","category-goatee-stroking-musing-or-something"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-4kj","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/16635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=16635"}],"version-history":[{"count":6,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/16635\/revisions"}],"predecessor-version":[{"id":17518,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/16635\/revisions\/17518"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=16635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=16635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=16635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}