I am doing a project where there is an interaction between Arduino and Processing, thanks to two buttons using the computer serial.
In the first case, I have the Arduino code where each time one of the two buttons is pressed, a series of photos will appear in Processing (the photos are CorBe and CorMal), if the button on pin 8 is pressed CorBe photos will appear in a loop and if the one on pin 9 is pressed, CorMal photos will appear, these are the first cases, the last two cases of if else and else, are if the button is not being pressed or if they are pressing both.
The point is that I need that only by pressing the button once and even if it is kept pressed, the CorBe or CorMal image appears once each time the button is pressed. So the image needs to appear but without erasing the others. Do you have an answer?
Long story short, every time we press button 1, PNG image CorBe needs to appear once. The same with button 2 and image CorMal.
I attach the two codes here:
Arduino Code
int switchPin = 8;
int switchPinNou = 9;
void setup() {
// Initialize serial communications at a 9600 baud rate
Serial.begin(9600);
pinMode(8, INPUT);
pinMode(13, OUTPUT);
}
void loop(){
if (digitalRead(8) == HIGH && digitalRead(9) == LOW) {
Serial.println("1");
}
else if (digitalRead(9) == HIGH && digitalRead(8) == LOW) {
Serial.println("2");
}
else if (digitalRead(8) == HIGH && digitalRead(9) == HIGH){
Serial.println("3");
}
else{
Serial.println("0");
}
}
Processing code
import processing.serial.*;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
PImage img;
PImage img2;
float value = 0;
void setup (){
String portName = Serial.list()[0]; // Change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
size(1920, 1080);
background(0);
img = loadImage("CorBe.png");
img2 = loadImage("CorMal.png");
//image(img, random(1900), random(1000), 50, 50);
//image(img2, random(1900), random(1000), 50, 50);
}
void draw (){
while (myPort.available() > 0) {
if (value == 1){
println("detectado1");
image(img, random(1900), random(1000), 50, 50);
}
if (value == 2){
println("detectado2");
image(img2, random(1900), random(1000), 50, 50);
}
}
}
void serialEvent (Serial myPort) {
value = float(myPort.readStringUntil('\n'));
}