{"id":621,"date":"2013-02-16T22:21:40","date_gmt":"2013-02-17T04:21:40","guid":{"rendered":"http:\/\/www.anthonyreinke.com\/?p=621"},"modified":"2018-05-07T20:53:06","modified_gmt":"2018-05-08T02:53:06","slug":"my-first-non-tutorial-arduino-project","status":"publish","type":"post","link":"https:\/\/anthonyreinke.com\/index.php\/2013\/02\/16\/my-first-non-tutorial-arduino-project\/","title":{"rendered":"My first non tutorial Arduino project"},"content":{"rendered":"<p>I have been playing with the <a href=\"http:\/\/www.amazon.com\/Smart-Projects-Arduino-UNO-Rev-3\/dp\/B002YQI5L4\/ref=sr_1_4?s=toys-and-games&amp;ie=UTF8&amp;qid=1361075148\" target=\"_blank\" rel=\"noopener\">Arduino Uno board<\/a> and after going through a bunch of tutorials, I wanted to branch out and do my own. \u00a0I have the\u00a0<a href=\"http:\/\/www.amazon.com\/Ultrasonic-Module-HC-SR04-Distance-Arduino\/dp\/B004U8TOE6\">Ultrasonic Module HC-SR04<\/a>\u00a0and a standard piezoelectric buzzer. \u00a0On the ultrasonic module, VCC goes to digital pin 2. \u00a0Trig goes to digital pin 3. \u00a0Echo goes to digital pin 4. \u00a0GND goes to the ground rail which connects to GND pin on the arduino. \u00a0On the buzzer, the positive lead goes to pin 11 and the negitive pin goes to the ground rail which is connected to the GND pin on the arduino. \u00a0 \u00a0Below is the code:<\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #008000;\"><em>void setup() {<\/em>\r\n<em> pinMode (122,OUTPUT);\/\/attach pin 2 to vcc<\/em>\r\n<em> pinMode (5,OUTPUT);\/\/attach pin 5 to GND<\/em>\r\n<em> \/\/ initialize serial communication:<\/em>\r\n<em> Serial.begin(9600);<\/em>\r\n<em> pinMode(11, OUTPUT); \/\/ sets the pin of the buzzer as output<\/em>\r\n<em>}<\/em><\/span><\/pre>\n<pre><span style=\"color: #008000;\"><em>void loop()<\/em>\r\n<em>{<\/em>\r\n<em>digitalWrite(122, HIGH);<\/em>\r\n<em> \/\/ establish variables for duration of the ping,<\/em>\r\n<em> \/\/ and the distance result in inches and centimeters:<\/em>\r\n<em> long duration, inches, cm;<\/em><\/span><\/pre>\n<pre><span style=\"color: #008000;\"><em>\/\/ The PING))) is triggered by a HIGH pulse of 2 or more microseconds.<\/em>\r\n<em> \/\/ Give a short LOW pulse beforehand to ensure a clean HIGH pulse:<\/em>\r\n<em> pinMode(3, OUTPUT);\/\/ attach pin 3 to Trig<\/em>\r\n<em> digitalWrite(3, LOW);<\/em>\r\n<em> delayMicroseconds(122);<\/em>\r\n<em> digitalWrite(3, HIGH);<\/em>\r\n<em> delayMicroseconds(5);<\/em>\r\n<em> digitalWrite(3, LOW);<\/em><\/span><\/pre>\n<pre><span style=\"color: #008000;\"><em>\/\/ The same pin is used to read the signal from the PING))): a HIGH<\/em>\r\n<em> \/\/ pulse whose duration is the time (in microseconds) from the sending<\/em>\r\n<em> \/\/ of the ping to the reception of its echo off of an object.<\/em>\r\n<em> pinMode (4, INPUT);\/\/attach pin 4 to Echo<\/em>\r\n<em> duration = pulseIn(4, HIGH);<\/em><\/span><\/pre>\n<pre><span style=\"color: #008000;\"><em>\/\/ convert the time into a distance<\/em>\r\n<em> inches = microsecondsToInches(duration);<\/em>\r\n<em> cm = microsecondsToCentimeters(duration);<\/em>\r\n\r\n<em> Serial.print(inches);<\/em>\r\n<em> Serial.print(\"in, \");<\/em>\r\n<em> Serial.print(cm);<\/em>\r\n<em> Serial.print(\"cm\");<\/em>\r\n<em> Serial.println();<\/em>\r\n\r\n<em> if (cm &lt; 50) {<\/em>\r\n<em> analogWrite(11,128);<\/em>\r\n<em> } <\/em>\r\n<em> else {<\/em>\r\n<em> digitalWrite(11, LOW);<\/em>\r\n<em> }<\/em>\r\n\r\n<em> delay(100);<\/em>\r\n<em>}<\/em><\/span><\/pre>\n<pre><span style=\"color: #008000;\"><em>long microsecondsToInches(long microseconds)<\/em>\r\n<em>{<\/em>\r\n<em> \/\/ According to Parallax's datasheet for the PING))), there are<\/em>\r\n<em> \/\/ 73.746 microseconds per inch (i.e. sound travels at 1130 feet per<\/em>\r\n<em> \/\/ second). This gives the distance travelled by the ping, outbound<\/em>\r\n<em> \/\/ and return, so we divide by 2 to get the distance of the obstacle.<\/em>\r\n<em> \/\/ See: http:\/\/www.parallax.com\/dl\/docs\/prod\/acc\/28015-PING-v1.3.pdf<\/em>\r\n<em> return microseconds \/ 74 \/ 2;<\/em>\r\n<em>}<\/em><\/span><\/pre>\n<pre><span style=\"color: #008000;\"><em>long microsecondsToCentimeters(long microseconds)<\/em>\r\n<em>{<\/em>\r\n<em> \/\/ The speed of sound is 340 m\/s or 29 microseconds per centimeter.<\/em>\r\n<em> \/\/ The ping travels out and back, so to find the distance of the<\/em>\r\n<em> \/\/ object we take half of the distance travelled.<\/em>\r\n<em> return microseconds \/ 29 \/ 2;<\/em>\r\n<em>}<\/em><\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have been playing with the Arduino Uno board and after going through a bunch of tutorials, I wanted to branch out and do my own. \u00a0I have the\u00a0Ultrasonic Module HC-SR04\u00a0and a standard piezoelectric buzzer. \u00a0On the ultrasonic module, VCC goes to digital pin 2. \u00a0Trig goes to digital pin 3. \u00a0Echo goes to digital [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-621","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/posts\/621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/comments?post=621"}],"version-history":[{"count":1,"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/posts\/621\/revisions"}],"predecessor-version":[{"id":12409,"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/posts\/621\/revisions\/12409"}],"wp:attachment":[{"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/media?parent=621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/categories?post=621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/tags?post=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}