{"id":29,"date":"2006-02-07T23:59:19","date_gmt":"2006-02-08T04:59:19","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/arduinowiring\/29"},"modified":"2007-08-06T23:37:41","modified_gmt":"2007-08-07T04:37:41","slug":"sending-a-two-byte-value-serially","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/29\/","title":{"rendered":"Sending a two-byte value serially"},"content":{"rendered":"<p>This code reads a 10-bit analog value, splits it into two bytes, and sends it out serially.\n<\/p>\n<p>Written in Arduino, should work for both Arduino and Wiring.<\/p>\n<p><!--more--><\/p>\n<pre>\n\/*\n Analog Two Bytes\n by Tom Igoe \n \n Reads an 10-bit analog value from 0 - 1024,  splits it into two bytes,\n and sends the bytes out the serial port.\n \n Created: 8 Feb. 2006\n*\/\n\nint analogPin = 2;\n\nint val;            \/\/ outgoing ADC value\nint highByte = 0;\nint lowByte = 0;\nvoid setup()\n{\n  \/\/ start serial port at 9600 bps:\n  beginSerial(9600);\n}\n\nvoid loop()\n{\n  \/\/ read analog input, divide by 4 to make the range 0-255:\n  val = analogRead(analogPin); \n  \/\/ divide by 256 to get the high byte:\n  highByte = val \/256;\n  \/\/ take the remainder of the division to get the low byte:\n  lowByte = val % 256;\n \/\/ send the two bytes out to in raw binary form:\n  serialWrite(highByte);\n  serialWrite(lowByte);  \n  \/\/ pause for 10 milliseconds:\n  delay(10);                 \n}\n\n\n\n\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This code reads a 10-bit analog value, splits it into two bytes, and sends it out serially&#8230;.  \/* Analog Two Bytes by Tom Igoe Reads an 10-bit analog value from 0 &#8211; 1024, splits it into two bytes, and sends the bytes out the serial port.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-arduinowiring"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}