Serial Communication
Test
LED in Serial port
Processing code: One value:
Processing code:
//Sends a byte out the serial port, and reads 1 byte in. sets x location a rect.
// x location of rectangle
BFont font;
int xpos;
//change this value for different sensors
byte response = 2;
void setup() {//begin serial communication
beginSerial();
size(255, 200);
font = loadFont("Garamond-Bold.vlw.gz");
//start off our call and response by sending 1;
serialWrite(response);}
void loop() {
//draw background with red value from sensor
background(0,0,0);
textFont(font, 44);
fill(255);
//display rotation value
text(xpos, 5, 30);
fill(0,0,255);
rectMode(CENTER_DIAMETER);
rect(xpos,100, 50, 50);}
void serialEvent() {
//receive byte
xpos = (int) serial;
//send # back to ask for another one
serialWrite(response);}
Processing code: Three value:
Processing code:
/* Serial call-and-response
by Tom Igoe (with adjustments by Dan)Sends a byte out the serial port, and reads 3 bytes in.
sets background color, x location and rotation of a rectUpdated October 12, 2004
*/
int[] serialStuff = new int[3]; // where we'll put what we receive
int serialCount = 0; // a count of how many bytes we receive
int xpos; // x location of rectangle
int rot; // rotation of rectangle
int bkcolor; // background colorBFont font;
void setup() {beginSerial(); //begin serial communication
size(255, 200);
font = loadFont("Garamond-Bold.vlw.gz");
serialWrite(65);//start off our call and response by sending 65;}
void loop() {
background(bkcolor,0,0);//draw background with red value from sensor
textFont(font, 44);
fill(255);//display rotation value
text(xpos, 5, 30);
fill(0,0,255);
rectMode(CENTER_DIAMETER);
//draw rect at "xpos" and rotate with "rot"
translate(xpos,100);
rotate(radians(rot*2));
rect(0,0, 50, 50);}
void serialEvent() {
// add the latest byte from the serial port to array:
serialStuff[serialCount] = serial;
serialCount++;// if we have 3 things set our variables:
if (serialCount > 2 ) {
bkcolor = serialStuff[0];
xpos = serialStuff[1];
rot = serialStuff[2];
// clear the string when we're done, and ask for more:
serialCount = 0;
// send a capital A to request new sensor readings:
serialWrite(65);
}}
PIC code:
DEFINE OSC 4
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEuS 20ADCON1 = %00000010
TRISA = %11111111
grLed var portd.0
ylLed var portd.1txPin var portc.6
rxPin var portc.7output grled
output ylled
output txpin
input rxpinproxVar var byte
potVAR VAR BYTE
pot2VAR var byte
inByte var bytepause 500
high grled
pause 300
low grled
pause 150
high grledmain:
low ylledserin2 rxpin, 16468, 20, main, [inbyte]
high ylled
if inbyte = 65 then
adcin 0, proxvar
adcin 1, potvar
adcin 2, pot2var
serout2 txpin, 16468, [proxvar, potvar, pot2var]
endifif inbyte = 1 then
adcin 0, proxvar
serout2 txpin, 16468, [proxvar]
endifif inbyte = 2 then
adcin 1, potvar
serout2 txpin, 16468, [potvar]
endifif inbyte = 3 then
adcin 2, pot2VAR
serout2 txpin, 16468, [pot2var]
endifgoto main