Hello, so i'm working on a project with 2 arduinos and processing. The code works fine coming from 1 arduino bu when I integrate the processing to use 2 serial connects with if statements
![processing forum not working processing forum not working]()
import processing.serial.*;
Serial myPort, myPort2, myPort3, myPort4, myPort5; // The serial port
int xPos = 400; // horizontal position of the graph
float inByte = 0;
int lastxPos=401;
int lastheight=700;
int lastheight2=700;
float inByte2 =0;
int rectX, rectY; // Position of square button
int rectX2, rectY2; // Postion of second Square button
int rectX3, rectY3;
int rectX4, rectY4;
int rectX5, rectY5;
int rectSize = 20; // Diameter of rect
int rectSize2 = 20;
int rectSize3 = 20;
int rectSize4 = 20;
int rectSize5 = 20;
color rectColor,baseColor;
color rectHighlight;
color currentColor;
boolean rectOver = false;
boolean rectOver2 = false;
boolean rectOver3 = false;
boolean rectOver4 = false;
boolean rectOver5 = false;
void setup() {
size(1024, 700);
//rectColor = color(0);
//rectHighlight = color(0);
rectX = width-250-rectSize-10;
rectY = height/3-rectSize/2;
rectX2 = width-200-rectSize2-10;
rectY2 = height/3-rectSize2/2;
rectX3 = width-150-rectSize3-10;
rectY3 = height/3-rectSize3/2;
rectX4 = width-100-rectSize4-10;
rectY4 = height/3-rectSize4/2;
rectX5 = width-50-rectSize5-10;
rectY5 = height/3-rectSize5/2;
myPort = new Serial(this, "COM10", 115200);
myPort2 = new Serial(this, "COM12", 9600);
myPort.bufferUntil('\n');
myPort2.bufferUntil('\n');
background(0,0,0);
}
void draw() {
}
void update(int x, int y)
{
if (overRect(rectX, rectY, rectSize, rectSize))
{
rectOver = true;
rectOver2 = rectOver3 = rectOver4= rectOver5 = false;
}
else if (overRect2(rectX2, rectY2, rectSize2, rectSize2))
{
rectOver2 = true;
rectOver = rectOver3 = rectOver4= rectOver5 = false;
}
else if (overRect3(rectX3, rectY3, rectSize3, rectSize3))
{
rectOver3 = true;
rectOver = rectOver2 = rectOver4= rectOver5 = false;
}
else if (overRect4(rectX4, rectY4, rectSize4, rectSize4))
{
rectOver4 = true;
rectOver = rectOver2 = rectOver3 = rectOver5 = false;
}
else if (overRect5(rectX5, rectY5, rectSize5, rectSize5))
{
rectOver5 = true;
rectOver = rectOver2 = rectOver3 = rectOver4 = false;
}
else
{
rectOver = rectOver2 = rectOver3 = rectOver4= rectOver5 = false;
}
}
/*
void mousePressed() {
if (rectOver) {
// currentColor = rectColor;
// serialEvent(myPort);
}
}
*/
boolean overRect(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
boolean overRect2(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
boolean overRect3(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
boolean overRect4(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
boolean overRect5(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
void serialEvent (Serial Port) {
// get the ASCII string:
strokeWeight(10);
fill(255,255,255);
textSize(15);
text("Module 1, IR1-Foyer", 50 ,80);
text("Module 2, IR1-Reception", 50, 120);
update(mouseX,mouseY);
textSize(15);
fill(255,255,255);
textSize(15);
text("Full ------------------------------", 200, 20);
text("Empty ----------------------------", 200, 690);
text(" 75% -----------------------------", 200, 175);
text(" 50% -----------------------------", 200, 350);
text(" 25% -----------------------------", 200, 525);
text("Water levels tank 1", 650, 690);
text("Volume measure 07/12/2015", 700,20);
strokeCap(SQUARE);
stroke(200,200,200);
strokeWeight(10);
line(320, 1023, 320, 1);
line(480,1023,480, 1);
rect(rectX, rectY, rectSize, rectSize);
rect(rectX2, rectY2, rectSize2, rectSize2);
rect(rectX3, rectY3, rectSize3, rectSize3);
rect(rectX4, rectY4, rectSize4, rectSize4);
rect(rectX5, rectY5, rectSize5, rectSize5);
strokeWeight(150);
if (Port == myPort)
{
String inString = myPort.readStringUntil('\n');
if (inString != null)
{
inString = trim(inString);
inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
if(rectOver == true){
textSize(15);
fill(255,255,0);
text("Module 1, IR1-Foyer", 50 ,80);
strokeWeight(150);
if ( int(height-inByte) >= (lastheight))
{
stroke(0,0,0);
line(lastxPos, lastheight, xPos, height - inByte);
delay(10);
if(lastheight > 525)
{
textSize(18);
fill(255,0,0);
text(" <----------- Warning!", 500, 525);
}
else if(lastheight < 525)
{
textSize(18);
fill(0,0,0);
text(" <----------- Warning!", 500, 525);
}
}
else if ( int(height-inByte) <= (lastheight))
{
stroke(0,0,200);
line(lastxPos, lastheight, xPos, height - inByte);
if(lastheight > 525)
{
textSize(18);
fill(255,0,0);
}
else if(lastheight < 525)
{
textSize(18);
fill(0,0,0);
text(" <----------- Warning!", 500, 525);
}
}
// }
}
}
}
// String inString = Port.readStringUntil('\n');
else if (Port == myPort2)
{
String inString = myPort2.readStringUntil('\n');
if (inString != null)
{
inString = trim(inString);
inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
// if(mousePressed){
if(rectOver2 == true){
textSize(15);
fill(255,255,0);
text("Module 1, IR1- reception", 50 ,120);
if ( int(height-inByte) >= (lastheight))
{
stroke(0,0,0);
line(lastxPos, lastheight, xPos, height - inByte);
delay(10);
if(lastheight > 525)
{
textSize(18);
fill(255,0,0);
text(" <----------- Warning!", 500, 525);
}
else if(lastheight < 525)
{
textSize(18);
fill(0,0,0);
text(" <----------- Warning!", 500, 525);
}
}
else if ( int(height-inByte) <= (lastheight))
{
stroke(0,0,200);
line(lastxPos, lastheight, xPos, height - inByte);
if(lastheight > 525)
{
textSize(18);
fill(255,0,0);
}
else if(lastheight < 525)
{
textSize(18);
fill(0,0,0);
text(" <----------- Warning!", 500, 525);
}
}
}
else
{
// stroke(0,0,0);
//line(700, 1023, 700, height);
}
lastxPos=xPos;
lastheight= int(height-inByte);
}
// }
}
else
{
// stroke(0,0,0);
//line(700, 1023, 700, height);
}
}