{"id":479,"date":"2011-12-23T13:01:17","date_gmt":"2011-12-23T18:01:17","guid":{"rendered":"http:\/\/hackaweek.com\/hacks\/?page_id=479"},"modified":"2012-06-11T21:01:21","modified_gmt":"2012-06-12T01:01:21","slug":"laser-range-finder-arduino-code","status":"publish","type":"page","link":"http:\/\/hackaweek.com\/hacks\/laser-range-finder-arduino-code\/","title":{"rendered":"Laser Range Finder Arduino Code"},"content":{"rendered":"<p><strong>Copy and paste this code into the Arduino compiler and upload it to your Arduino.<\/strong><\/p>\n<p>\/*<br \/>\n  Simple LRF Demo<br \/>\n by Roy ELtham and Dino Segovis<br \/>\n *\/<\/p>\n<p>\/\/ include the library code:<br \/>\n#include <liquidcrystal .h>\n<p>\/\/ initialize the library with the numbers of the interface pins<br \/>\nLiquidCrystal lcd(7, 8, 9, 10, 11, 12);<\/p>\n<p>\/\/ include the SoftwareSerial library so we can use it to talk to the LRF<br \/>\n#include <softwareserial .h><\/p>\n<p>#define rxPin 2  \/\/ connect to the LRF&#8217;s SOUT pin<br \/>\n#define txPin 3  \/\/ connect to the LRF&#8217;s SIN pin thru a 4.7<\/p>\n<p>\/\/ set up a new serial port<br \/>\nSoftwareSerial lrfSerial =  SoftwareSerial(rxPin, txPin);<\/p>\n<p>void setup()<br \/>\n{<br \/>\n  \/\/ set up the LCD&#8217;s number of columns and rows:<br \/>\n  lcd.begin(16, 4);<\/p>\n<p>  \/\/ define pin modes for tx, rx, led pins:<br \/>\n  pinMode(rxPin, INPUT);<br \/>\n  pinMode(txPin, OUTPUT);<br \/>\n  \/\/ set the data rate for the SoftwareSerial port<br \/>\n  lrfSerial.begin(9600);<\/p>\n<p>\/\/ send a U to the LRF so that it will autodetect the speed to communicate with us at (9600)<br \/>\n  lrfSerial.print(&#8216;U&#8217;);<br \/>\n\/\/ the LRF will send back a : and a newline, we just read and ignore those here<br \/>\n  lrfSerial.read();<br \/>\n  lrfSerial.read();<\/p>\n<p>  \/\/print splash screen<br \/>\n  lcd.print(&#8220;Laser&#8221;);<br \/>\n  lcd.setCursor(0, 1);<br \/>\n  lcd.print(&#8221;   Range&#8221;);<br \/>\n  lcd.setCursor(0, 2);<br \/>\n  lcd.print(&#8221;  Finder&#8221;);<\/p>\n<p>  delay(3000);<br \/>\n  lcd.clear();<br \/>\n  \/\/ send an E to the LRF to adjust to current lighting conditions<br \/>\n  lrfSerial.print(&#8216;E&#8217;);<br \/>\n  delay(1000);<br \/>\n  \/\/tell the user that the LRF is ready<br \/>\n  lcd.print(&#8220;LRF ready.&#8221;);<br \/>\n  delay(1000);<br \/>\n  lcd.clear();<\/p>\n<p>  \/\/ Print a message to the LCD.<br \/>\n  lcd.print(&#8220;Distance in mm&#8221;);<br \/>\n}<\/p>\n<p>void loop()<br \/>\n{<br \/>\n  \/\/ send an R to the LRF telling it to take a measurement<br \/>\n  lrfSerial.print(&#8216;R&#8217;);<\/p>\n<p>  \/\/ read what the LRF sends back<br \/>\n  char lrfData[32];<br \/>\n  int offset = 0;<br \/>\n  lrfData[0] = 0;<br \/>\n  while(1)<br \/>\n  {<br \/>\n    lrfData[offset] = lrfSerial.read();<br \/>\n    \/\/ if we get a : character that indicates the end of the read data (and that the LRF is ready for the next command)<br \/>\n    if (lrfData[offset] == &#8216;:&#8217;)<br \/>\n    {<br \/>\n      lrfData[offset] = 0; \/\/ null terminate the string of bytes and break out of the loop<br \/>\n      break;<br \/>\n    }<br \/>\n    offset++;<br \/>\n  }<\/p>\n<p>  \/\/ set the cursor to column 0, line 1<br \/>\n  \/\/ (note: line 1 is the second row, since counting begins with 0):<br \/>\n  lcd.setCursor(0, 1);<\/p>\n<p>  \/\/ print out the data from the LRF (it sends back a string in the form &#8220;D = xxxx mm&#8221; where xxxx is the distance in milimeters<br \/>\n  lcd.print(lrfData);<\/p>\n<p>  \/\/ delay a tiny bit before the next read<br \/>\n  delay(200);<\/p>\n<p>  \/\/integer to convert lrfData string to an integer value<br \/>\n  int distance = atoi(&#038;lrfData[4]);<br \/>\n  \/\/sound the alert if the distance is less than stated<br \/>\n  if (distance < 200)\n  {\n    tone(5, 440, 200);\n    delay(200);\n  }\n\n\n}\n\n<\/softwareserial><\/softwareserial><\/liquidcrystal>\n","protected":false},"excerpt":{"rendered":"<p>Copy and paste this code into the Arduino compiler and upload it to your Arduino. \/* Simple LRF Demo by Roy ELtham and Dino Segovis *\/ \/\/ include the library code: #include \/\/ initialize the library with the numbers of &hellip; <a href=\"http:\/\/hackaweek.com\/hacks\/laser-range-finder-arduino-code\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-479","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/hackaweek.com\/hacks\/wp-json\/wp\/v2\/pages\/479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hackaweek.com\/hacks\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/hackaweek.com\/hacks\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/hackaweek.com\/hacks\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hackaweek.com\/hacks\/wp-json\/wp\/v2\/comments?post=479"}],"version-history":[{"count":6,"href":"http:\/\/hackaweek.com\/hacks\/wp-json\/wp\/v2\/pages\/479\/revisions"}],"predecessor-version":[{"id":741,"href":"http:\/\/hackaweek.com\/hacks\/wp-json\/wp\/v2\/pages\/479\/revisions\/741"}],"wp:attachment":[{"href":"http:\/\/hackaweek.com\/hacks\/wp-json\/wp\/v2\/media?parent=479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}