simple serial_2 (call and response)  

'****************************************************************
'* Name : Simple Serial2.BAS *
'* Author : Todd Holoubek *
'* Notice : Copyright (c) 2004 Todd Holoubel *
'* : All Rights Reserved *
'* Date : 6/7/2004 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
DEFINE OSC 4

start:

INCLUDE "modedefs.bas"
' Define ADCIN parameters
' Set number of bits in result
DEFINE ADC_BITS 10
' Set clock source (3=rc)
DEFINE ADC_CLOCK 3
' Set sampling time in microseconds
DEFINE ADC_SAMPLEUS 10
' Set PORTA to all input
TRISA = %11111111
' Set up ADCON1 analog and right justify the result
adcon1 = %10000010
'define adc vars
adcVar0 VAR WORD 'Create variable to store result
adcVar1 VAR WORD 'Create variable to store result
adcVar2 VAR WORD 'Create variable to store result
inByte var byte

main:
'read adc data
ADCIN 0, adcVar0
ADCIN 1, adcVar1
ADCIN 2, adcVar2

adcVar0 = adcVar0/4
adcVar1 = adcVar1/4
adcVar2 = adcVar2/4

'look for the A from processing
serin2 PORTC.7, 16468,[inByte]

if inByte = 65 then
Serout2 PORTC.6,16468,[adcVar0, adcVar1, adcVar2]
endif
GOTO main

'********************************

/* Serial call-and-response for v.90
by Tom Igoe

Sends a byte out the serial port, and reads 3 bytes in.
Sets foregound color, xpos, and ypos of a circle onstage
using the values returned from the serial port.

Thanks to Daniel Shiffman for the improvements

Updated 21 March 2005
*/

import processing.serial.*;

int bgcolor; // background color
int fgcolor; // fill color
Serial port; // the serial port
int[] serialInArray = new int[3]; // where we'll put what we receive
int serialCount = 0; // a count of how many bytes we receive
float xpos, ypos; // Starting position of the ball
boolean firstContact = false; // whether we've heard from the microcontroller

void setup() {
size(256, 256); // stage size
noStroke(); // no border on the next thing drawn

// Set the starting position of the ball (middle of the stage)
xpos = width/2;
ypos = height/2;

// print a list of the serial ports, for debugging purposes:
println(Serial.list());

/* I know that the first port in the serial list on my mac
is always my Keyspan adaptor, so I open Serial.list()[0].
On Windows machines, this generally opens COM1.
Open whatever port is the one you're using.
*/
port = new Serial(this, Serial.list()[0], 9600);
port.write(65); // send a capital A to start the microcontroller sending
}

void draw() {
background(bgcolor);
fill(fgcolor);
// Draw the shape
ellipse(xpos, ypos, 20, 20);
// get any new serial data:
while (port.available() > 0) {
serialEvent();
// note that we heard from the microntroller:
firstContact = true;
}
// if there's no serial data,
// send again until we get some.
// (in case you tend to start Processing
// before you start your external device):
if (firstContact == false) {
delay(300);
port.write(65);
}

}

void serialEvent() {
processByte((char)port.read());
}

void processByte(char inByte) {
// add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// if we have 3 bytes:
if (serialCount > 2 ) {
xpos = (float)serialInArray[0];
ypos = (float)serialInArray[1];
fgcolor = (int)serialInArray[2];
//send a capital A to request new sensor readings:
port.write(65);
// reset serialCount:
serialCount = 0;

}
}