// Step1constintbuttonPin=4;// Button on pin 4voidsetup(){pinMode(buttonPin,INPUT_PULLUP);Serial.begin(9600);// Start USB serial communication}voidloop(){// Send button state to Processingif(digitalRead(buttonPin)==LOW){Serial.println("P");// Send "P" = Pressed}else{Serial.println("R");// Send "R" = Released}delay(100);// Wait to avoid flooding serial port}
// Step1importprocessing.serial.*;SerialmyPort;StringbuttonLabel="";// Default labelvoidsetup(){size(300,200);println(Serial.list());StringportName="/dev/tty.usbmodem1101";myPort=newSerial(this,portName,9600);// Set your port namemyPort.bufferUntil('\n');// Read input until newline}voiddraw(){background(220);fill(0);textAlign(CENTER,CENTER);textSize(20);text(buttonLabel,width/2,height/2);// Show current label}// Receive data from ArduinovoidserialEvent(SerialmyPort){StringinData=myPort.readStringUntil('\n');// Read line from Arduinoif(inData!=null){inData=inData.trim();// Remove whitespaceif(inData.equals("P")){buttonLabel="Arduino Button Pressed";}elseif(inData.equals("R")){buttonLabel="";}println("Received: "+inData);}}
// Step2importprocessing.serial.*;SerialmyPort;ButtonledButton;StringbuttonLabel="LED";// Default label text in Step1 was ""voidsetup(){size(300,200);println(Serial.list());StringportName="/dev/tty.usbmodem1101";myPort=newSerial(this,portName,9600);// Set your port nameledButton=newButton(buttonLabel,100,80,100,40);// Create button}voiddraw(){background(220);ledButton.display();}// Sending data to ArduinovoidmousePressed(){if(ledButton.isClicked(mouseX,mouseY)){myPort.write('1');// Send '1' to Arduino to turn on LED}}// Button classclassButton{Stringlabel;intx,y,w,h;Button(Stringlabel,intx,inty,intw,inth){this.label=label;this.x=x;this.y=y;this.w=w;this.h=h;}voiddisplay(){fill(200);rect(x,y,w,h);fill(0);textAlign(CENTER,CENTER);text(label,x+w/2,y+h/2);}booleanisClicked(intmx,intmy){return(mx>x&&mx<x+w&&my>y&&my<y+h);}}
// Step2constintledPin=3;// LED on pin 3voidsetup(){pinMode(ledPin,OUTPUT);digitalWrite(ledPin,LOW);Serial.begin(9600);// Start USB serial communication}voidloop(){// Receive LED control from Processingif(Serial.available()){// Check if data received from Processingcharc=Serial.read();if(c=='1'){// If character '1' receiveddigitalWrite(ledPin,HIGH);// Turn LED ondelay(500);// Keep it on for 0.5 secondsdigitalWrite(ledPin,LOW);// Turn LED off}}}
// Step3constintbuttonPin=4;// Button on pin 4constintledPin=3;// LED on pin 3voidsetup(){pinMode(ledPin,OUTPUT);pinMode(buttonPin,INPUT_PULLUP);Serial.begin(9600);// Start USB serial communication}voidloop(){// Send button state to Processing (Step1)if(digitalRead(buttonPin)==LOW){Serial.println("P");// Send "P" = Pressed}else{Serial.println("R");// Send "R" = Released}// Receive LED control from Processing (Step2)if(Serial.available()){// Check if data received from Processingcharc=Serial.read();if(c=='1'){// If character '1' receiveddigitalWrite(ledPin,HIGH);// Turn LED ondelay(500);// Keep it on for 0.5 secondsdigitalWrite(ledPin,LOW);// Turn LED off}}delay(100);// Wait to avoid flooding serial}
// Step3importprocessing.serial.*;SerialmyPort;ButtonledButton;StringbuttonLabel="LED";// Default label text in Step1 was ""voidsetup(){size(300,200);println(Serial.list());StringportName="/dev/tty.usbmodem1101";myPort=newSerial(this,portName,9600);// Set your port namemyPort.bufferUntil('\n');// Read input until newlineledButton=newButton(buttonLabel,100,80,100,40);// Create button}voiddraw(){background(220);ledButton.label=buttonLabel;// Update label from Arduino inputledButton.display();}// Receive data from Arduino (Step1)voidserialEvent(SerialmyPort){StringinData=myPort.readStringUntil('\n');if(inData!=null){inData=inData.trim();if(inData.equals("P")){buttonLabel="Arduino Button Pressed";}elseif(inData.equals("R")){buttonLabel="LED";}println("Received: "+inData);}}// Sending data to Arduino (Step2)voidmousePressed(){// Only allow LED control when not physically pressedif(buttonLabel.equals("LED")&&ledButton.isClicked(mouseX,mouseY)){myPort.write('1');// Send command to Arduino to light LED}}// Button classclassButton{Stringlabel;intx,y,w,h;Button(Stringlabel,intx,inty,intw,inth){this.label=label;this.x=x;this.y=y;this.w=w;this.h=h;}voiddisplay(){fill(200);rect(x,y,w,h);fill(0);textAlign(CENTER,CENTER);text(label,x+w/2,y+h/2);}booleanisClicked(intmx,intmy){return(mx>x&&mx<x+w&&my>y&&my<y+h);}}