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

**URL:** <https://discourse.processing.org/t/load-random-images-every-time-the-button-is-pressed-processing-arduino/9960>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [April 4, 2019, 12:03pm UTC](https://discourse.processing.org/t/load-random-images-every-time-the-button-is-pressed-processing-arduino/9960 "2019-04-04T12:03:56Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![Sol81](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@Sol81](https://discourse.processing.org/u/Sol81)\
**Post date:** [April 8, 2019, 3:18pm UTC](https://discourse.processing.org/t/load-random-images-every-time-the-button-is-pressed-processing-arduino/9960/21 "2019-04-08T15:18:34Z")

</div>

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

```auto
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

```auto
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);                           
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 8, 2019, 4:34pm UTC](https://discourse.processing.org/t/load-random-images-every-time-the-button-is-pressed-processing-arduino/9960/22 "2019-04-08T16:34:57Z")

</div>

possibly ( untested )

```auto
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  
}

```

---

<div class="post-metadata">

**Author:** ![Sol81](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@Sol81](https://discourse.processing.org/u/Sol81)\
**Post date:** [April 8, 2019, 4:45pm UTC](https://discourse.processing.org/t/load-random-images-every-time-the-button-is-pressed-processing-arduino/9960/23 "2019-04-08T16:45:13Z")

</div>

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

[Previous page](https://discourse.processing.org/t/load-random-images-every-time-the-button-is-pressed-processing-arduino/9960.md?page=1)
