{"id":324,"date":"2017-12-18T19:56:34","date_gmt":"2017-12-18T11:56:34","guid":{"rendered":"http:\/\/hardihuang.info\/blog\/?p=324"},"modified":"2018-01-22T07:42:02","modified_gmt":"2018-01-21T23:42:02","slug":"hc-sr04%e8%b6%85%e5%a3%b0%e6%b3%a2%e4%bc%a0%e6%84%9f%e5%99%a8","status":"publish","type":"post","link":"http:\/\/hardihuang.info\/blog\/?p=324","title":{"rendered":"HC-SR04\u8d85\u58f0\u6ce2\u4f20\u611f\u5668"},"content":{"rendered":"<p>\u7814\u7a76\u4e86HC-SR04\u8d85\u58f0\u6ce2\u4f20\u611f\u5668\uff0c\u53ef\u4ee5\u63a2\u6d4b\u8303\u56f4\u662f2cm~4m\uff0c\u7cbe\u5ea6\u53ef\u4ee5\u8fbe\u52303mm\uff0c\u633a\u5389\u5bb3\u7684<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-325\" src=\"http:\/\/hardihuang.info\/blog\/wp-content\/uploads\/2018\/01\/2017121814244283865655l.jpg\" alt=\"\" width=\"720\" height=\"587\" srcset=\"http:\/\/hardihuang.info\/blog\/wp-content\/uploads\/2018\/01\/2017121814244283865655l.jpg 720w, http:\/\/hardihuang.info\/blog\/wp-content\/uploads\/2018\/01\/2017121814244283865655l-300x245.jpg 300w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/p>\n<p><!--more--><\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<pre><code>\r\n\/\/ defines pins numbers\r\nconst int trigPin = 9;\r\nconst int echoPin = 10;\r\n\/\/ defines variables\r\nlong duration;\r\nint distance;\r\nvoid setup() {\r\npinMode(trigPin, OUTPUT); \/\/ Sets the trigPin as an Output\r\npinMode(echoPin, INPUT); \/\/ Sets the echoPin as an Input\r\nSerial.begin(9600); \/\/ Starts the serial communication\r\n}\r\nvoid loop() {\r\n\/\/ Clears the trigPin\r\ndigitalWrite(trigPin, LOW);\r\ndelayMicroseconds(2);\r\n\/\/ Sets the trigPin on HIGH state for 10 micro seconds\r\ndigitalWrite(trigPin, HIGH);\r\ndelayMicroseconds(10);\r\ndigitalWrite(trigPin, LOW);\r\n\/\/ Reads the echoPin, returns the sound wave travel time in microseconds\r\nduration = pulseIn(echoPin, HIGH);\r\n\/\/ Calculating the distance\r\ndistance= duration*0.034\/2;\r\n\/\/ Prints the distance on the Serial Monitor\r\nSerial.print(\"Distance: \");\r\nSerial.println(distance);\r\n}\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7814\u7a76\u4e86HC-SR04\u8d85\u58f0\u6ce2\u4f20\u611f\u5668\uff0c\u53ef\u4ee5\u63a2\u6d4b\u8303\u56f4\u662f2cm~4m\uff0c\u7cbe\u5ea6\u53ef\u4ee5\u8fbe\u52303mm\uff0c\u633a\u5389\u5bb3\u7684<\/p>\n","protected":false},"author":1,"featured_media":325,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"http:\/\/hardihuang.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/324"}],"collection":[{"href":"http:\/\/hardihuang.info\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hardihuang.info\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hardihuang.info\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hardihuang.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=324"}],"version-history":[{"count":2,"href":"http:\/\/hardihuang.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/324\/revisions"}],"predecessor-version":[{"id":354,"href":"http:\/\/hardihuang.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/324\/revisions\/354"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/hardihuang.info\/blog\/index.php?rest_route=\/wp\/v2\/media\/325"}],"wp:attachment":[{"href":"http:\/\/hardihuang.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hardihuang.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hardihuang.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}