Tested and working on Processing 4 with Arduino Mega. Button from pin 2 to ground. Uses built in LED.
Arduino sketch:
String lastString;
int led = LED_BUILTIN;
const int button = 2;
int buttonPushCounter = 0;
int buttonState = 1;
int lastButtonState = 1;
void setup() {
Serial.begin(115200);
pinMode(button, INPUT_PULLUP);// reverses button logic, no external resistors required
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
if (Serial.available() > 0) {
char in = Serial.read();
if ((in == 'h') || (in == 'H')) {
digitalWrite(led, HIGH);
} else if ((in == 'l') || (in == 'L')) {
digitalWrite(led, LOW);
}
}
buttonState = digitalRead(button);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is LOW then the button went from off to on
buttonPushCounter++;
if (buttonPushCounter % 2 == 0) {
Serial.println("green");
} else {
Serial.println("red");
}
delay(500);
}
}
lastButtonState = buttonState;
}
Processing sketch:
import processing.serial.*;
Serial myPort;
int rval, gval, bval;
int state;
String[] com = {"COM2", "COM3", "COM4",
"COM5", "COM6", "COM7", "COM8", "COM9", "COM10", "COM11",
"COM12", "COM13", "COM14", "COM15", "COM16", "COM17", "COM18",
"COM19", "COM20", "COM21", "COM22", "COM23", "COM24", "COM25", "COM26"};
void settings() {
fullScreen(P3D);
}
void setup() {
background(0, 59, 0);
noStroke();
fill(204);
connect(1, 24, 200);
}
void draw() {
background(rval, gval, bval);
switch(state) {
case 0:
rval = 128;
gval = 128;
bval = 128;
break;
case 1:
rval = 0;
gval = 255;
bval = 0;
break;
case 2:
rval = 255;
gval = 0;
bval = 0;
break;
case 3:
rval = 255;
gval = 165;
bval = 0;
myPort.write('H');
break;
case 4:
rval = 0;
gval = 60;
bval = 0;
myPort.write('L');
break;
case 5:
rval = 0;
gval = 255;
bval = 0;
myPort.write('h');
break;
case 6:
rval = 0;
gval = 0;
bval = 255;
myPort.write('l');
break;
default:
rval = 128;
gval = 128;
bval = 128;
break;
}
// use arrow keys to do stuff
checkDirections();
}
void checkDirections() {
if (keyPressed) {
if (keyCode == UP) {
state = 3;
} else if (keyCode == DOWN) {
state = 4;
} else if (keyCode == LEFT) {
state = 5;
} else if (keyCode == RIGHT) {
state = 6;
}
delay(160);
}
}
// serial seek and connect handler
boolean connect(int portNumber, int numCom, int limit) {
background (0, 14, 255);
int tries = 0;
while (tries < limit) {
try {
printArray (Serial.list()[portNumber]);
myPort = new Serial(this, com[numCom], 115200);
}
catch(Exception e) {
System.err.println("Retrying " + "Port " + com[numCom] + " on Port Number " + portNumber);
numCom += 1;
tries++;
if (numCom < 0) {
numCom = 24;
} else if (numCom > 24) {
numCom = 0;
}
if (tries % 25 == 0) {
portNumber += 1;
if (portNumber > 4) {
portNumber = 0;
}
}
delay(200);
continue;
}
break;
}
if (tries < limit) {
println("Connected in " + str(tries + 1) + " tries.");
background(0, 255, 0);
delay(200);
background(0);
return true;
} else {
System.err.println("Connection failed.");
return false;
}
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
if (inString.equals("green")) {
state = 1;
} else if (inString.equals("red")) {
state = 2;
}
}
}
Good luck.