Arduino passes the value to the Processing display through the serial port.

Arduino passes the value to the Processing display through the serial port.

Arduino side code:

int red=255; //Create variables to store the data you need to send
int green = 2;
int blue = 3;
void setup()
{
  Serial.begin(9600);//Define a data transfer rate of 9600 bits
}
void loop()
{ 
  Serial.print(red);
  Serial.print(",");
  Serial.print(green);
  Serial.print(",");
  Serial.print(blue);
  Serial.print("\n");
  delay(1000);
}

The above red, green, blue can be replaced with the value obtained by the color sensor in practical applications.

Processing code:

import processing.serial.*;//Introducing the serial library
 
int r = 0;
int g = 0;
int b = 0;
 
Serial myPort;//Create a Serial object called "myPort"
void setup() {
  myPort = new Serial(this,"COM7", 9600);
  myPort.bufferUntil('\n');  //buffer until meet '\n', then call the event listener
  //Define myPort's port and data transfer rate
  //Should be consistent with Arduino
}
void draw() {
  background(204);
  fill(r, g, b);
  rect(30, 20, 55, 55);
}
//listen to the event. when buffer filled, run this method
 
void serialEvent(Serial p) {
  String inString = p.readString();
  print(inString);
  
  String[] list = split(inString, ',');
// list[0] is now "Chernenko", list[1] is "Andropov"...
  r = int(list[0]);
  g = int(list[1]);
  b = int(list[2]);
}

Leave a Reply