{"id":2523,"date":"2023-08-05T11:49:12","date_gmt":"2023-08-05T03:49:12","guid":{"rendered":"https:\/\/104.es\/?p=2523"},"modified":"2023-08-05T11:49:44","modified_gmt":"2023-08-05T03:49:44","slug":"microbitmicropython-hc-sr04","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2023\/08\/05\/microbitmicropython-hc-sr04\/","title":{"rendered":"\u3010micro:bit\u3011MicroPython\u8d85\u97f3\u6ce2HC-SR04"},"content":{"rendered":"\n<p>\u5728\u9019\u500b\u55ae\u5143\u4f7f\u7528\u7684\u662f\u4ee5\u4e0b\u9019\u500b\u8d85\u97f3\u6ce2\u6a21\u7d44\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-\u53f0\u7063\u7269\u806f\u79d1\u6280-taiwaniot-studio wp-block-embed-\u53f0\u7063\u7269\u806f\u79d1\u6280-taiwaniot-studio\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"SYvNVOlo42\"><a href=\"https:\/\/www.taiwaniot.com.tw\/product\/hc-sr04\/\">HC-SR04 \u8d85\u97f3\u6ce2\u6e2c\u8ddd \/ \u907f\u969c\u6a21\u7d44  \u8d85\u8072\u6ce2\u50b3\u611f\u5668<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"HC-SR04 \u8d85\u97f3\u6ce2\u6e2c\u8ddd \/ \u907f\u969c\u6a21\u7d44  \u8d85\u8072\u6ce2\u50b3\u611f\u5668 &#8212; \u53f0\u7063\u7269\u806f\u79d1\u6280 TaiwanIOT Studio\" src=\"https:\/\/www.taiwaniot.com.tw\/product\/hc-sr04\/embed\/#?secret=SYvNVOlo42\" data-secret=\"SYvNVOlo42\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\u5728micro:bit\u7684\u7a4d\u6728\u5f0f\u74b0\u5883\u5982\u679c\u8981\u4f7f\u7528\u8d85\u97f3\u6ce2\u6a21\u7d44\u7684\u8a71\uff0c\u53ef\u4ee5\u5230\u64f4\u5145\u529f\u80fd\u53bb\u555f\u7528\u8d85\u97f3\u6ce2\u7684\u7a4d\u6728\uff0c\u800c\u5728MicroPython\u4e2d\u4e5f\u6709\u71b1\u5fc3\u7684\u7a0b\u5f0f\u8a2d\u8a08\u5e2b\u5beb\u597d\u53ef\u4ee5\u76f4\u63a5\u532f\u5165\u4f7f\u7528\u7684\u6a21\u7d44\u4e86\uff0c\u6b64\u6a21\u7d44\u4e4b\u9023\u7d50\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/fizban99\/microbit_hcsr04\">https:\/\/github.com\/fizban99\/microbit_hcsr04<\/a><\/p>\n\n\n\n<p>\u4f7f\u7528\u65b9\u6cd5\u5f88\u7c21\u55ae\uff0c\u5148\u5230\u4e0a\u9762\u7684\u7db2\u5740\u4e0b\u8f09\u5c08\u6848\u6a94\u6848\uff0c\u5728\u89e3\u58d3\u7e2e\u4e4b\u5f8c\u627e\u51fa\u4e00\u500b\u53eb\u505ahcsr04.py\u7684\u7a0b\u5f0f\u6a94\u6848\uff0c\u628a\u9019\u500b\u6a94\u6848\u4e0a\u50b3\u5230micro:bit\u677f\u5b50\u4e0a\u3002\u5728\u786c\u9ad4\u65b9\u9762\uff0c\u9664\u4e86\u8d85\u97f3\u6ce2\u6a21\u7d44\u7684\u96fb\u6e90\u63a5\u59a5\u4e4b\u5916\uff0c\u5b83\u7684echo\u8981\u63a5\u5230micro:bit\u7684\u7b2c14\u652f\u8173\uff0ctrigger\u8981\u63a5\u5230micro:bit\u7684\u7b2c15\u652f\u8173\uff0c\u9023\u7dda\u5b8c\u6210\u4e4b\u5f8c\uff0c\u5373\u53ef\u5728REPL\u74b0\u5883\u4e2d\u8f38\u5165\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u9032\u884c\u6e2c\u8a66\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from hcsr04 import HCSR04\r\nfrom microbit import *\r\n\r\nsonar=HCSR04()\r\nwhile True:\r\n    print(\"{}cm\".format(sonar.distance_mm()\/10))\r\n    sleep(1000)<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u7a0b\u5f0f\u57f7\u884c\u4e4b\u5f8c\uff0c\u6bcf\u9694\u4e00\u79d2\u9418\u6703\u66f4\u65b0\u4e00\u6b21\u8ddd\u96e2\u8cc7\u8a0a\uff0c\u540c\u5b78\u5011\u53ef\u4ee5\u7528\u624b\u5728\u611f\u6e2c\u5668\u524d\u9762\u8a66\u8a66\u5b83\u7684\u6548\u679c\u5982\u4f55\u5594\u3002<\/p>\n\n\n\n<p>\u540c\u5b78\u8acb\u7279\u5225\u7559\u610f\uff0c\u4e0a\u9762\u7684\u90a3\u500b\u7a0b\u5f0f\u662f\u8981\u5728REPL\u74b0\u5883\u4e0b\u57f7\u884c\u624d\u770b\u5f97\u5230\u8f38\u51fa\u7684\u5167\u5bb9\uff0c\u56e0\u70ba\u5b83\u7528\u7684\u662fprint\u51fd\u6578<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u5247\u662f\u8b93\u8ddd\u96e2\u8cc7\u6599\u53ef\u4ee5\u986f\u793a\u5728micro:bit\u7684LED\u4e0a\uff0c\u5225\u5fd8\u4e86\u8981\u4f7f\u7528display.scroll\u5594\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from hcsr04 import HCSR04\r\nfrom microbit import *\r\n\r\nsonar=HCSR04()\r\nwhile True:\r\n    display.scroll(\"{}cm\".format(sonar.distance_mm()\/10))\r\n    sleep(1000)<\/code><\/pre>\n\n\n\n<p>\u5047\u8a2d\u4e26\u4e0d\u6253\u7b97\u986f\u793a\u6578\u5b57\uff0c\u800c\u662f\u4f9d\u7167\u5b83\u6240\u91cf\u6e2c\u7684\u8ddd\u96e2\u4f86\u6c7a\u5b9a\u662f\u5426\u57f7\u884c\u67d0\u4e9b\u52d5\u4f5c\uff0c\u53ef\u4ee5\u900f\u904e\u7e7c\u96fb\u5668\u4f86\u5e6b\u5fd9\u3002\u7e7c\u96fb\u5668\u67093\u652f\u63a5\u8173\uff0c\u9664\u4e86\u6b63\u8ca0\u96fb\u6e90\u8981\u9023\u63a5\u5230\u5916\u90e85V\u96fb\u6e90\u4e4b\u5916\uff0c\u5b83\u7684S\u63a5\u7dda\u8981\u9023\u63a5\u5230micro:bit\u7684\u67d0\u4e00\u500b\u8f38\u51fa\u63a5\u8173\uff0c\u5047\u8a2d\u6211\u5011\u628a\u5b83\u9023\u5230pin2\u7684\u8a71\uff0c\u5247\u7a0b\u5f0f\u53ef\u4ee5\u7de8\u5beb\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from hcsr04 import HCSR04\r\nfrom microbit import *\r\n\r\nsonar=HCSR04()\r\nwhile True:\r\n    if sonar.distance_mm() &lt; 50:\r\n        pin2.write_digital(1)\r\n        display.show(Image.HEART)\r\n        sleep(500)\r\n    else:\r\n        pin2.write_digital(0)\r\n        display.show(Image.HAPPY)<\/code><\/pre>\n\n\n\n<p>\u628a\u9019\u500b\u7a0b\u5f0f\u71d2\u9304\u9032micro:bit\u4e4b\u5f8c\uff0cmicro:bit\u6703\u5148\u986f\u793a\u4e00\u500b\u7b11\u81c9\uff0c\u7576\u6211\u5011\u628a\u969c\u7919\u7269\u6162\u6162\u9760\u8fd1\u8d85\u97f3\u6ce2\u611f\u6e2c\u5668\u6642\uff0c\u4e00\u65e6\u4f4e\u65bc5\u516c\u5206\u5c31\u6703\u8b8a\u6210\u611b\u5fc3\u5716\u6848\uff0c\u4e26\u6703\u807d\u5230\u7e7c\u96fb\u5668\u4f5c\u52d5\u7684\u8072\u97f3\u5594\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8d85\u97f3\u6ce2\u6a21\u7d44\u662f\u5728\u7269\u806f\u7db2\u61c9\u7528\u4e2d\u7528\u4f86\u6e2c\u91cf\u8ddd\u96e2\u7684\u597d\u5de5\u5177\uff0c\u800c\u4e14\u5b83\u7684\u6a21\u7d44\u4e5f\u4e0d\u8cb4\uff0c\u4f7f\u7528\u8d77\u4f86\u4e5f\u5f88\u65b9\u4fbf\u3002<\/p>\n","protected":false},"author":1,"featured_media":2480,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11,15],"tags":[],"class_list":["post-2523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microbit","category-python"],"jetpack_featured_media_url":"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/oqtafyt5ktw.jpg","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=2523"}],"version-history":[{"count":2,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2523\/revisions"}],"predecessor-version":[{"id":2525,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2523\/revisions\/2525"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media\/2480"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}