Serial Call-and-Response (Microcontroller side)

This code reads three sensors and sends them out serially when prompted by another computer. It works with this Processing example. Arduino/Wiring, PicBasic Pro, and BX-24 versions are shown.

/*
 analog Call-and-Response
 by Tom Igoe 

 Waits for serial input.  If the incoming value
 is a valid byte (i.e. 0 - 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.

 Arduino hardware connections:
 A0: potentiometer on analog in 1
 A1: potentiometer on analog in 2
 D2: switch on digital in 2

 Created 26 Sept. 2005
 Updated 30 May 2006
 */

int firstSensor = 0;    // first analog sensor
int secondSensor = 0;   // second analog sensor
int thirdSensor = 0;    // digital sensor
int inByte = 0;         // incoming serial byte

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
}

void loop()
{
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    // read first analog input, divide by 4 to make the range 0-255:
    firstSensor = analogRead(0)/4;
    // delay 10ms to let the ADC recover:
    delay(10);
    // read second analog input, divide by 4 to make the range 0-255:
    secondSensor = analogRead(1)/4;
    // read  switch, multiply by 255
    // so that you're sending 0 or 255:
    thirdSensor = 255 * digitalRead(2);
    // send sensor values:
    Serial.print(firstSensor, BYTE);
    Serial.print(secondSensor, BYTE);
    Serial.print(thirdSensor, BYTE);
  }
}

Here’s the PicBasic Pro version:

' call and response serial example for picBasic Pro.
' By Tom Igoe, 2003

' This example waits for a byte on the incoming serial connection,
' and checks to see that the byte equals 65.
' It then sends the values of three sensors on pins RA0 - RA2.

' this example uses two arrays to hold the ADC values,
' and the byte values that they're converted to for sending.
' serial RX is on pin RC7
' serial TX is on pin RC6

 ' Define ADCIN parameters
DEFINE  ADC_BITS        10     ' Set number of bits in result
DEFINE  ADC_CLOCK       3     	' Set clock source (3=rc)
DEFINE  ADC_SAMPLEUS    50    	' Set sampling time in uS

' constant to set the baud rate:
inv9600 con 16468

' define variables:
adcVar var word(3)
byteVar var byte(3)
channel var byte
inByte var byte

TRISA = %11111111       ' Set PORTA to all input
ADCON1 = %10000010      ' Set PORTA analog and right justify result
 main:

  ' read sensors, convert to bytes:
  for channel = 0 to 2
  	adcin channel, adcVar(channel)
  	byteVar(channel) = adcVar(channel) / 4
  next

  ' read serial data in:
  serin2 portc.7, inv9600, [inByte]

  ' if you got the message from director, send out data:
  if inByte = 65 then
    serout2 portc.6, inv9600, [byteVar(0), byteVar(1), byteVar(2)]
  endif
goto main

And here’s the BX-24 version:

' call and response serial example for BX-24.
' by Tom Igoe, 2000

' This example waits for a byte on the incoming serial connection,
' and checks to see that the byte equals 65.
' It then sends the values of three sensors on pins 13, 14, and 15.
' serial RX is on pin 12
' serial TX is on pin 11

dim adc1Var as byte
dim adc2Var as byte
dim adc3Var as byte
dim inByte as byte
dim gotaByte as boolean
dim inputBuffer(1 To 13) As Byte
dim outputBuffer(1 To 10) As Byte

sub main ()
 ' set up serial port:
 call defineCom3(12,11,bx1000_1000)
 call openQueue(inputBuffer, 13)
 call openQueue(outputBuffer, 10)
 call openCom(3,9600,inputBuffer, outputBuffer)

 do
  ' read sensors:
  adc1Var = cByte(getADC(13)4)
  adc2Var = cByte(getADC(14)4)
  adc3Var = cByte(getADC(15)4)

  ' read serial data in:
  if statusQueue(inputBuffer) = true then
      call getQueue(inputBuffer, inByte, 1)

     ' if you got the message from director, send out data:
     if inByte = 65 then
       call putQueue(OutputBuffer, adc1Var, 1)
       call putQueue(OutputBuffer, adc2Var, 1)
       call putQueue(OutputBuffer, adc3Var, 1)
     end if
  end if
  loop
end sub
This entry was posted in arduino/wiring, BX-24, PicBasic Pro. Bookmark the permalink.

Comments are closed.