import controlP5.*; import processing.serial.*; Serial myPort; // Create object from Serial class ControlP5 cp5; int myColor = color(0); int c1,c2; float n,n1; int r = 0; int c = 0; int w = 0; int s = 0; int light = 500; String out; String val; // since we're doing serial handshaking, // we need to check if we've heard from the microcontroller boolean firstContact = false; void setup() { myPort = new Serial(this, Serial.list()[1], 9600); myPort.bufferUntil('\n'); background(color(0)); size(400,200); noStroke(); cp5 = new ControlP5(this); ControlFont cf1 = new ControlFont(createFont("Arial",20)); cp5.addToggle("Rain") .setValue(0) .setPosition(50,5) .setSize(149,50) .setColorBackground(color(100)) .setColorForeground(color(200)) .setColorActive(color(0,100,255)) .captionLabel().setControlFont(cf1) ; cp5.addToggle("Sun") .setPosition(200,5) .setSize(149,50) .setValue(0) .setColorBackground(color(100)) .setColorForeground(color(200)) .setColorActive(color(255,255,0)) .captionLabel().setControlFont(cf1) ; } void draw() { fill(50); rect(50,120,300,50); fill(200); rect(50,120,light/3.333,50); text("Light Sensor Value",50,190); } void serialEvent( Serial myPort) { //put the incoming data into a String - //the '\n' is our end delimiter indicating the end of a complete packet val = myPort.readStringUntil('\n'); //make sure our data isn't empty before continuing if (val != null) { //trim whitespace and formatting characters (like carriage return) val = trim(val); println(val); //look for our 'A' string to start the handshake //if it's there, clear the buffer, and send a request for data if (firstContact == false) { if (val.equals("A")) { myPort.clear(); firstContact = true; myPort.write("A"); println("contact"); } } else { //if we've already established contact, keep getting and parsing data println(val); if (firstContact == true) { light = Integer.parseInt(val); } out = str(s)+str(r); myPort.write(out); delay(50); // when you've parsed the data you have, ask for more: myPort.write("A"); } } } public void Rain(int theValue) { if (theValue == 1) { r = 0; } else { r = 1; } } public void Sun(int theValue) { if (theValue == 1) { s = 7; } else { s = 8; } }