int[]serialInArray=newint[3];// Where we'll put what we receive
intserialCount=0;// A count of how many bytes we receive
intxpos,ypos;// Starting position of the ball
booleanfirstContact=false;// Whether we've heard from the microcontroller
voidsetup(){
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 FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
StringportName=Serial.list()[0];
myPort=newSerial(this,portName,9600);
}
voiddraw(){
background(bgcolor);
fill(fgcolor);
// Draw the shape
ellipse(xpos,ypos,20,20);
}
voidserialEvent(SerialmyPort){
// read a byte from the serial port:
intinByte=myPort.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if(firstContact==false){
if(inByte=='A'){
myPort.clear();// clear the serial port buffer
firstContact=true;// you've had first contact from the microcontroller
myPort.write('A');// ask for more
}
}
else{
// Add the latest byte from the serial port to array:
serialInArray[serialCount]=inByte;
serialCount++;
// If we have 3 bytes:
if(serialCount>2){
xpos=serialInArray[0];
ypos=serialInArray[1];
fgcolor=serialInArray[2];
// print the values (for debugging purposes only):
println(xpos+"\t"+ypos+"\t"+fgcolor);
// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount=0;
}
}
}
*/
/*
Max/MSPversion5patchtorunwiththisexample:
{
"boxes":[{
"box":{
"maxclass":"message",
"text":"65",
"patching_rect":[339.0,466.0,32.5,18.0],
"numoutlets":1,
"fontsize":12.0,
"outlettype":[""],
"id":"obj-9",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"newobj",
"text":"sel 1",
"patching_rect":[339.0,437.0,36.0,20.0],
"numoutlets":2,
"fontsize":12.0,
"outlettype":["bang",""],
"id":"obj-6",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"comment",
"text":"Serial Call-Response \n\nSends a byte out the serial port, and reads 3 bytes in. Sets foregound color, xpos, and ypos of a circle using the values returned from the serial port. \n\nNote: This patch assumes that the device on the other end of the serial port is going to send a single byte of value 65 (ASCII A) on startup. The sketch waits for that byte, then sends an ASCII A whenever it wants more data. \n\ncreated 14 Apr 2009\nby Scott Fitzgerald and Tom Igoe",
"linecount":11,
"patching_rect":[404.0,52.0,464.0,158.0],
"numoutlets":0,
"fontsize":12.0,
"id":"obj-5",
"fontname":"Arial",
"numinlets":1
}
}
,{
"box":{
"maxclass":"comment",
"text":"trigger (or [t]) forces right-left conventions. All the drawing and processing will happen before Max requests new values. When this trigger fires, it sends an ASCII A to ask Arduino for new values.",
"linecount":3,
"patching_rect":[239.0,505.0,425.0,48.0],
"numoutlets":0,
"fontsize":12.0,
"id":"obj-65",
"fontname":"Arial",
"numinlets":1
}
}
,{
"box":{
"maxclass":"comment",
"text":"reinitializes the gates when turned on and off",
"linecount":2,
"patching_rect":[170.0,370.0,135.0,34.0],
"numoutlets":0,
"fontsize":12.0,
"id":"obj-64",
"fontname":"Arial",
"numinlets":1
}
}
,{
"box":{
"maxclass":"comment",
"text":"checks for the ascii value of \"A\" to begin cominucation. After initial communication is made, this block shuts down.",
"linecount":3,
"patching_rect":[460.0,355.0,233.0,48.0],
"numoutlets":0,
"fontsize":12.0,
"id":"obj-63",
"fontname":"Arial",
"numinlets":1
}
}
,{
"box":{
"maxclass":"newobj",
"text":"p \"draw the circle\"",
"patching_rect":[217.0,645.0,269.0,19.0],
"numoutlets":1,
"fontsize":10.0,
"outlettype":[""],
"id":"obj-62",
"fontname":"Verdana",
"numinlets":3,
"patcher":{
"fileversion":1,
"rect":[54.0,94.0,640.0,480.0],
"bglocked":0,
"defrect":[54.0,94.0,640.0,480.0],
"openrect":[0.0,0.0,0.0,0.0],
"openinpresentation":0,
"default_fontsize":10.0,
"default_fontface":0,
"default_fontname":"Verdana",
"gridonopen":0,
"gridsize":[25.0,25.0],
"gridsnaponopen":0,
"toolbarvisible":1,
"boxanimatetime":200,
"imprint":0,
"boxes":[{
"box":{
"maxclass":"message",
"text":"frgb 255 255 255",
"patching_rect":[375.0,150.0,98.0,18.0],
"numoutlets":1,
"fontsize":11.595187,
"outlettype":[""],
"id":"obj-47",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"message",
"text":"frgb 0 0 0",
"patching_rect":[275.0,125.0,59.0,18.0],
"numoutlets":1,
"fontsize":11.595187,
"outlettype":[""],
"id":"obj-46",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"newobj",
"text":"sel 255 0",
"patching_rect":[300.0,100.0,66.0,21.0],
"numoutlets":3,
"fontsize":12.0,
"outlettype":["bang","bang",""],
"id":"obj-45",
"fontname":"Verdana",
"numinlets":1
}
}
,{
"box":{
"maxclass":"newobj",
"text":"pack 0 0 0 0",
"patching_rect":[50.0,125.0,180.0,21.0],
"numoutlets":1,
"fontsize":12.0,
"outlettype":[""],
"id":"obj-43",
"fontname":"Verdana",
"numinlets":4
}
}
,{
"box":{
"maxclass":"newobj",
"text":"+ 10",
"patching_rect":[200.0,100.0,40.0,21.0],
"numoutlets":1,
"fontsize":12.0,
"outlettype":["int"],
"id":"obj-42",
"fontname":"Verdana",
"numinlets":2
}
}
,{
"box":{
"maxclass":"newobj",
"text":"+ 10",
"patching_rect":[75.0,100.0,40.0,21.0],
"numoutlets":1,
"fontsize":12.0,
"outlettype":["int"],
"id":"obj-41",
"fontname":"Verdana",
"numinlets":2
}
}
,{
"box":{
"maxclass":"message",
"text":"clear, paintoval $1 $2 $3 $4",
"patching_rect":[50.0,150.0,152.0,18.0],
"numoutlets":1,
"fontsize":11.595187,
"outlettype":[""],
"id":"obj-40",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"inlet",
"patching_rect":[57.5,40.0,25.0,25.0],
"numoutlets":1,
"outlettype":["int"],
"id":"obj-58",
"numinlets":0,
"comment":""
}
}
,{
"box":{
"maxclass":"inlet",
"patching_rect":[120.0,40.0,25.0,25.0],
"numoutlets":1,
"outlettype":["int"],
"id":"obj-59",
"numinlets":0,
"comment":""
}
}
,{
"box":{
"maxclass":"inlet",
"patching_rect":[300.0,40.0,25.0,25.0],
"numoutlets":1,
"outlettype":["int"],
"id":"obj-60",
"numinlets":0,
"comment":""
}
}
,{
"box":{
"maxclass":"outlet",
"patching_rect":[228.333344,228.0,25.0,25.0],
"numoutlets":0,
"id":"obj-61",
"numinlets":1,
"comment":""
}
}
],
"lines":[{
"patchline":{
"source":["obj-47",0],
"destination":["obj-61",0],
"hidden":0,
"midpoints":[]
}
}
,{
"patchline":{
"source":["obj-46",0],
"destination":["obj-61",0],
"hidden":0,
"midpoints":[]
}
}
,{
"patchline":{
"source":["obj-40",0],
"destination":["obj-61",0],
"hidden":0,
"midpoints":[]
}
}
,{
"patchline":{
"source":["obj-60",0],
"destination":["obj-45",0],
"hidden":0,
"midpoints":[]
}
}
,{
"patchline":{
"source":["obj-59",0],
"destination":["obj-42",0],
"hidden":0,
"midpoints":[]
}
}
,{
"patchline":{
"source":["obj-59",0],
"destination":["obj-43",1],
"hidden":0,
"midpoints":[]
}
}
,{
"patchline":{
"source":["obj-58",0],
"destination":["obj-41",0],
"hidden":0,
"midpoints":[]
}
}
,{
"patchline":{
"source":["obj-58",0],
"destination":["obj-43",0],
"hidden":0,
"midpoints":[]
}
}
,{
"patchline":{
"source":["obj-42",0],
"destination":["obj-43",3],
"hidden":0,
"midpoints":[]
}
}
,{
"patchline":{
"source":["obj-41",0],
"destination":["obj-43",2],
"hidden":0,
"midpoints":[]
}
}
,{
"patchline":{
"source":["obj-45",1],
"destination":["obj-47",0],
"hidden":0,
"midpoints":[]
}
}
,{
"patchline":{
"source":["obj-45",0],
"destination":["obj-46",0],
"hidden":0,
"midpoints":[]
}
}
,{
"patchline":{
"source":["obj-43",0],
"destination":["obj-40",0],
"hidden":0,
"midpoints":[]
}
}
]
}
,
"saved_object_attributes":{
"fontface":0,
"fontsize":10.0,
"default_fontface":0,
"default_fontname":"Verdana",
"default_fontsize":10.0,
"fontname":"Verdana",
"globalpatchername":""
}
}
}
,{
"box":{
"maxclass":"newobj",
"text":"0",
"patching_rect":[310.0,378.0,32.5,20.0],
"numoutlets":1,
"fontsize":12.0,
"outlettype":["int"],
"id":"obj-57",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"newobj",
"text":"!- 1",
"patching_rect":[385.0,436.0,32.5,20.0],
"numoutlets":1,
"fontsize":12.0,
"outlettype":["int"],
"id":"obj-55",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"newobj",
"text":"gate 1 1",
"patching_rect":[385.0,355.0,54.0,20.0],
"numoutlets":1,
"fontsize":12.0,
"outlettype":[""],
"id":"obj-54",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"toggle",
"patching_rect":[385.0,405.0,20.0,20.0],
"numoutlets":1,
"outlettype":["int"],
"id":"obj-53",
"numinlets":1
}
}
,{
"box":{
"maxclass":"newobj",
"text":"gate 1 0",
"patching_rect":[194.0,455.0,54.0,20.0],
"numoutlets":1,
"fontsize":12.0,
"outlettype":[""],
"id":"obj-50",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"newobj",
"text":"sel 65",
"patching_rect":[385.0,380.0,43.0,20.0],
"numoutlets":2,
"fontsize":12.0,
"outlettype":["bang",""],
"id":"obj-48",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"lcd",
"patching_rect":[217.0,695.0,256.0,256.0],
"numoutlets":4,
"outlettype":["list","list","int",""],
"id":"obj-39",
"numinlets":1
}
}
,{
"box":{
"maxclass":"newobj",
"text":"t 65 l",
"patching_rect":[194.0,504.0,42.0,21.0],
"numoutlets":2,
"fontsize":12.0,
"outlettype":["int",""],
"id":"obj-35",
"fontname":"Verdana",
"numinlets":1
}
}
,{
"box":{
"maxclass":"comment",
"text":"val3",
"patching_rect":[535.0,604.0,37.0,21.0],
"numoutlets":0,
"fontsize":12.0,
"id":"obj-1",
"fontname":"Verdana",
"numinlets":1
}
}
,{
"box":{
"maxclass":"number",
"patching_rect":[467.0,604.0,56.0,21.0],
"numoutlets":2,
"fontsize":12.0,
"outlettype":["int","bang"],
"id":"obj-3",
"fontname":"Verdana",
"numinlets":1
}
}
,{
"box":{
"maxclass":"comment",
"text":"val2",
"patching_rect":[410.0,605.0,37.0,21.0],
"numoutlets":0,
"fontsize":12.0,
"id":"obj-18",
"fontname":"Verdana",
"numinlets":1
}
}
,{
"box":{
"maxclass":"comment",
"text":"val1",
"patching_rect":[282.0,605.0,37.0,21.0],
"numoutlets":0,
"fontsize":12.0,
"id":"obj-20",
"fontname":"Verdana",
"numinlets":1
}
}
,{
"box":{
"maxclass":"number",
"patching_rect":[342.0,605.0,56.0,21.0],
"numoutlets":2,
"fontsize":12.0,
"outlettype":["int","bang"],
"id":"obj-22",
"fontname":"Verdana",
"numinlets":1
}
}
,{
"box":{
"maxclass":"number",
"patching_rect":[217.0,605.0,55.0,21.0],
"numoutlets":2,
"fontsize":12.0,
"outlettype":["int","bang"],
"id":"obj-23",
"fontname":"Verdana",
"numinlets":1
}
}
,{
"box":{
"maxclass":"newobj",
"text":"unpack 0 0 0",
"patching_rect":[217.0,570.0,269.0,21.0],
"numoutlets":3,
"fontsize":12.0,
"outlettype":["int","int","int"],
"id":"obj-29",
"fontname":"Verdana",
"numinlets":1
}
}
,{
"box":{
"maxclass":"newobj",
"text":"zl group 3",
"patching_rect":[194.0,480.0,71.0,21.0],
"numoutlets":2,
"fontsize":12.0,
"outlettype":["",""],
"id":"obj-31",
"fontname":"Verdana",
"numinlets":2
}
}
,{
"box":{
"maxclass":"newobj",
"text":"select 0 1",
"patching_rect":[312.0,200.0,62.0,20.0],
"numoutlets":3,
"fontsize":12.0,
"outlettype":["bang","bang",""],
"id":"obj-30",
"fontname":"Arial",
"numinlets":1
}
}
,{
"box":{
"maxclass":"comment",
"text":"click here to close the serial port",
"patching_rect":[360.0,291.0,206.0,20.0],
"numoutlets":0,
"fontsize":12.0,
"id":"obj-26",
"fontname":"Arial",
"numinlets":1
}
}
,{
"box":{
"maxclass":"comment",
"text":"click here to open the serial port",
"patching_rect":[385.0,265.0,206.0,20.0],
"numoutlets":0,
"fontsize":12.0,
"id":"obj-27",
"fontname":"Arial",
"numinlets":1
}
}
,{
"box":{
"maxclass":"message",
"text":"close",
"patching_rect":[312.0,291.0,39.0,18.0],
"numoutlets":1,
"fontsize":12.0,
"outlettype":[""],
"id":"obj-21",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"message",
"text":"port a",
"patching_rect":[334.0,265.0,41.0,18.0],
"numoutlets":1,
"fontsize":12.0,
"outlettype":[""],
"id":"obj-19",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"comment",
"text":"Click here to get a list of serial ports",
"patching_rect":[405.0,239.0,207.0,20.0],
"numoutlets":0,
"fontsize":12.0,
"id":"obj-2",
"fontname":"Arial",
"numinlets":1
}
}
,{
"box":{
"maxclass":"toggle",
"patching_rect":[229.0,155.0,22.0,22.0],
"numoutlets":1,
"outlettype":["int"],
"id":"obj-11",
"numinlets":1
}
}
,{
"box":{
"maxclass":"newobj",
"text":"qmetro 10",
"patching_rect":[229.0,200.0,65.0,20.0],
"numoutlets":1,
"fontsize":12.0,
"outlettype":["bang"],
"id":"obj-12",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"message",
"text":"print",
"patching_rect":[354.0,239.0,36.0,18.0],
"numoutlets":1,
"fontsize":12.0,
"outlettype":[""],
"id":"obj-13",
"fontname":"Arial",
"numinlets":2
}
}
,{
"box":{
"maxclass":"newobj",
"text":"serial a 9600",
"patching_rect":[229.0,315.0,84.0,20.0],
"numoutlets":2,
"fontsize":12.0,
"outlettype":["int",""],
"id":"obj-14",
"fontname":"Arial",
"numinlets":1
}
}
,{
"box":{
"maxclass":"comment",
"text":"Read serial input buffer every 10 milliseconds",