Tuesday, March 4, 2008
[prototype04_processing to microcontroller]
Getting two codes (PBASIC and Processing) to talk to each other. The first step in bringing remote data into the intelligence of a local device. This code communicates between the microcontroller and processing. When the mouse is pressed the LED turns on.
PROCESSING
// Example by Tom Igoe
import processing.serial.*;
int j=75;
Serial myPort; // The serial port
PFont myFont; // The display font
String inString; // Input string from serial port
int lf = 10; // ASCII linefeed
void setup() {
size(400,200);
// Make your own font. It's fun!
myFont = loadFont("ArialMT-48.vlw");
textFont(myFont, 48);
// List all the available serial ports:
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].
// Open whatever port is the one you're using.
myPort = new Serial(this, "COM3", 2400);
}
void draw() {
// Twiddle your thumbs
background(0);
if(mousePressed== true) {
myPort.write(j);
println("test");
}
text("Received: " + inString, 10, 50);
//else {
//myPort.write(0);
// }
// int j=75;
// myPort.write(j);
// text("Received: " + inString, 10, 80);
//noLoop();
serialEvent(myPort);
}
void serialEvent(Serial myPort) {
inString = myPort.readString();
}
PBASIC
' {$STAMP BS2}
' {$PBASIC 2.5}
x VAR Byte
y VAR Byte
proIn VAR Byte
Main:
DO
SERIN 16, 16780, [proIn]
IF proIn = 65 THEN
LOW 2
ENDIF
IF proIn > 0 THEN
HIGH 2
ENDIF
LOOP
END
Labels: [cm:sd], pbasic, processing