0

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'));
}
3
  • 1
    use Bounce2 library in Arduino to handle the buttons. it will do the debouncing and it will report the change of the state of the button instead of the state of the button. Commented Nov 21, 2021 at 18:56
  • If you're not into implementing what Juraj said, you can implement a global variable which tracks the state of the button. Only updates it when the button is down or released. Commented Nov 21, 2021 at 20:22
  • The accepted answer here implements a global variable to track the state of a button. Commented Nov 22, 2021 at 10:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.