{"id":49,"date":"2006-05-30T16:37:00","date_gmt":"2006-05-30T21:37:00","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/arduinowiring\/49"},"modified":"2007-08-07T10:18:56","modified_gmt":"2007-08-07T15:18:56","slug":"serial-call-and-response-arduinowiring","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/49\/","title":{"rendered":"Serial Call-and-Response (Microcontroller side)"},"content":{"rendered":"<p> This code reads three sensors and sends them out serially when prompted by another computer.  It works with this <a href=\"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/processing\/50\">Processing<\/a> example. Arduino\/Wiring, PicBasic Pro, and BX-24 versions are shown.<\/p>\n<p><!--more--><\/p>\n<pre>\r\n\/*\r\n analog Call-and-Response\r\n by Tom Igoe \r\n\r\n Waits for serial input.  If the incoming value\r\n is a valid byte (i.e. 0 - 255), the program then\r\n reads two analog inputs and one digital input.\r\n It divides the anaog inputs by 4\r\n to convert the ranges to 0 = 255, and\r\n sends all three sensor values out serially.\r\n\r\n Arduino hardware connections:\r\n A0: potentiometer on analog in 1\r\n A1: potentiometer on analog in 2\r\n D2: switch on digital in 2\r\n\r\n Created 26 Sept. 2005\r\n Updated 30 May 2006\r\n *\/\r\n\r\nint firstSensor = 0;    \/\/ first analog sensor\r\nint secondSensor = 0;   \/\/ second analog sensor\r\nint thirdSensor = 0;    \/\/ digital sensor\r\nint inByte = 0;         \/\/ incoming serial byte\r\n\r\nvoid setup()\r\n{\r\n  \/\/ start serial port at 9600 bps:\r\n  Serial.begin(9600);\r\n}\r\n\r\nvoid loop()\r\n{\r\n  \/\/ if we get a valid byte, read analog ins:\r\n  if (Serial.available() &gt; 0) {\r\n    \/\/ get incoming byte:\r\n    inByte = Serial.read();\r\n    \/\/ read first analog input, divide by 4 to make the range 0-255:\r\n    firstSensor = analogRead(0)\/4;\r\n    \/\/ delay 10ms to let the ADC recover:\r\n    delay(10);\r\n    \/\/ read second analog input, divide by 4 to make the range 0-255:\r\n    secondSensor = analogRead(1)\/4;\r\n    \/\/ read  switch, multiply by 255\r\n    \/\/ so that you're sending 0 or 255:\r\n    thirdSensor = 255 * digitalRead(2);\r\n    \/\/ send sensor values:\r\n    Serial.print(firstSensor, BYTE);\r\n    Serial.print(secondSensor, BYTE);\r\n    Serial.print(thirdSensor, BYTE);\r\n  }\r\n}<\/pre>\n<p>Here&#8217;s the PicBasic Pro version:<\/p>\n<pre>\r\n' call and response serial example for picBasic Pro.\r\n' By Tom Igoe, 2003\r\n\r\n' This example waits for a byte on the incoming serial connection,\r\n' and checks to see that the byte equals 65.\r\n' It then sends the values of three sensors on pins RA0 - RA2.\r\n\r\n' this example uses two arrays to hold the ADC values,\r\n' and the byte values that they're converted to for sending.\r\n' serial RX is on pin RC7\r\n' serial TX is on pin RC6\r\n\r\n ' Define ADCIN parameters\r\nDEFINE  ADC_BITS        10     ' Set number of bits in result\r\nDEFINE  ADC_CLOCK       3     \t' Set clock source (3=rc)\r\nDEFINE  ADC_SAMPLEUS    50    \t' Set sampling time in uS\r\n\r\n' constant to set the baud rate:\r\ninv9600 con 16468\r\n\r\n' define variables:\r\nadcVar var word(3)\r\nbyteVar var byte(3)\r\nchannel var byte\r\ninByte var byte\r\n\r\nTRISA = %11111111       ' Set PORTA to all input\r\nADCON1 = %10000010      ' Set PORTA analog and right justify result\r\n main:\r\n\r\n  ' read sensors, convert to bytes:\r\n  for channel = 0 to 2\r\n  \tadcin channel, adcVar(channel)\r\n  \tbyteVar(channel) = adcVar(channel) \/ 4\r\n  next\r\n\r\n  ' read serial data in:\r\n  serin2 portc.7, inv9600, [inByte]\r\n\r\n  ' if you got the message from director, send out data:\r\n  if inByte = 65 then\r\n    serout2 portc.6, inv9600, [byteVar(0), byteVar(1), byteVar(2)]\r\n  endif\r\ngoto main<\/pre>\n<p>And here&#8217;s the BX-24 version:<\/p>\n<pre>' call and response serial example for BX-24.\r\n' by Tom Igoe, 2000\r\n\r\n' This example waits for a byte on the incoming serial connection,\r\n' and checks to see that the byte equals 65.\r\n' It then sends the values of three sensors on pins 13, 14, and 15.\r\n' serial RX is on pin 12\r\n' serial TX is on pin 11\r\n\r\ndim adc1Var as byte\r\ndim adc2Var as byte\r\ndim adc3Var as byte\r\ndim inByte as byte\r\ndim gotaByte as boolean\r\ndim inputBuffer(1 To 13) As Byte\r\ndim outputBuffer(1 To 10) As Byte\r\n\r\nsub main ()\r\n ' set up serial port:\r\n call defineCom3(12,11,bx1000_1000)\r\n call openQueue(inputBuffer, 13)\r\n call openQueue(outputBuffer, 10)\r\n call openCom(3,9600,inputBuffer, outputBuffer)\r\n\r\n do\r\n  ' read sensors:\r\n  adc1Var = cByte(getADC(13)4)\r\n  adc2Var = cByte(getADC(14)4)\r\n  adc3Var = cByte(getADC(15)4)\r\n\r\n  ' read serial data in:\r\n  if statusQueue(inputBuffer) = true then\r\n      call getQueue(inputBuffer, inByte, 1)\r\n\r\n     ' if you got the message from director, send out data:\r\n     if inByte = 65 then\r\n       call putQueue(OutputBuffer, adc1Var, 1)\r\n       call putQueue(OutputBuffer, adc2Var, 1)\r\n       call putQueue(OutputBuffer, adc3Var, 1)\r\n     end if\r\n  end if\r\n  loop\r\nend sub<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If the incoming value is a valid byte (i.e. 0 &#8211; 255), the program then reads two analog inputs and one digital input.  It divides the anaog inputs by 4 to convert the ranges to 0 = 255, and sends all three sensor values out serially.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,11,10],"tags":[],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-arduinowiring","category-BX-24","category-picbasic-pro"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/49","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=49"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}