Load random images every time the button is pressed - Processing - Arduino

I have made the changes but the
if ( arduino send something good )
is not working, what should I replace it with?

Now my processing code is

import processing.serial.*;
Serial port;
int oldval, val;
boolean diagp = false;
PImage img_bg, img_Bullet;


void setup() {
  printArray(Serial.list());
  port = new Serial(this, Serial.list()[2], 9600);  
   img_bg = loadImage("Background_Throne.jpg");
  background(img_bg);
  size(1500, 900);
  img_Bullet = loadImage("Bullet_Trace.png");
}

void draw() { 
  if ( port.available() > 0 ) {
    val = char(port.read());
    println(val);
  }

  if (val !=oldval && val == 2 ) {
    //
    println("button pressed event");
  }
  oldval = val;  // memory
  
  if ( diagp) println(val);
  
    if ( arduino send something good ){  
   image(img_Bullet, random (0, 1500), random (0, 900), 34, 45);
}

and my arduino code is

int switchPin = 7;                      
int LEDPin = 13;


void setup() {
  pinMode(7, INPUT);            
  pinMode(13, OUTPUT);
  Serial.begin(9600);                    
}

void loop() {
  if (digitalRead(7) == HIGH) {  
    Serial.write(2);               
    digitalWrite(13, HIGH);
  } else {                              
    Serial.write(1);               
    digitalWrite(13, LOW);
}
  delay(100);                           
}

possibly ( untested )

void draw() { 
  if ( port.available() > 0 ) {
    val = char(port.read());
    if ( diagp) println(val);
  }
  if (val !=oldval && val == 2 ) {
    println("button pressed event");
    image(img_Bullet, random (0, 1500), random (0, 900), 34, 45);
  }
  oldval = val;  // memory  
}
1 Like

Amazing!!! Thank you so much it is working perfectly.
I really appreciate your help :smile:

1 Like