# Processing (image display) via Arduino ultrasonic sensor

**URL:** https://discourse.processing.org/t/processing-image-display-via-arduino-ultrasonic-sensor/947
**Category:** Electronics (Arduino, etc.)
**Created:** [June 14, 2018, 7:25am UTC](https://discourse.processing.org/t/processing-image-display-via-arduino-ultrasonic-sensor/947 "2018-06-14T07:25:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![komats](https://avatars.discourse-cdn.com/v4/letter/k/7bcc69/32.png) [@komats](https://discourse.processing.org/u/komats)
#### Post date: [June 14, 2018, 7:25am UTC](https://discourse.processing.org/t/processing-image-display-via-arduino-ultrasonic-sensor/947/1 "2018-06-14T07:25:10Z")

</div>

Hi,  
I am just starting to explore posibilities to make something work in **processing via arduino** (sensors). On school exhibition I want to make a picture display that shows one image at the time on the wall (with a projector), depending how close or far person is located from distance sensor.  
This is a sketch of what I am thinking about:

 ![example](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/f9e9ccbfa5e57dc249fea9751c6f46d5e856230b.jpg)  
To see if it really works, I explored some tutorials that explains how to connect Arduino input with Processing. **Currently, I have these codes in both programs:**  
**for Arduino:**

```auto
/*
This code reads the Analog Voltage output from the
LV-MaxSonar sensors
Please note that we do not recommend using averaging with our sensors.
Mode and Median filters are recommended.
*/
const int anPin1 = 0;
long distance1;

void setup() {
  Serial.begin(9600); // sets the serial port to 9600
}

void read_sensors(){
  /*
  Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
  Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
  */
  distance1 = analogRead(anPin1)/2;
}

void print_all(){
  /*
  Serial.print("S1");
  Serial.print("inches");
  */
  Serial.print(" ");
  Serial.print(distance1);
  Serial.println();
}

void loop() {
  read_sensors();
  print_all();
  delay(50); 
}

```

**And for Processing:**

```auto
import processing.serial.*;  
Serial myPort;  
String data="" ;
PFont myFont;  
int distance;

void setup(){
  size(1366,900); // size of processing window
  //background(0);// setting background color to black
  
  myPort = new Serial(this, "/dev/cu.usbmodemfd111", 9600);
  myPort.bufferUntil('\n');
}

void draw(){

  background(0);
  textAlign(CENTER);
  fill(255);
  text(data,820,400);
  textSize(100);
  fill(#4B5DCE);
  text(" Distance : cm",450,400);
  noFill();
  stroke(#4B5DCE);
  
}

void serialEvent(Serial myPort){
  
  data=myPort.readStringUntil('\n');
 
}

```

_And this distance reading works perfect!_

**Unfortunately didn’t found any specific example for what i am looking for.**  
**So the question is, how can I get, for example, 3 pictures working this way:**

if(distance\>60){  
image(photo1, 0, 0); //display first photograpgy  
}  
else if (40\<distance\<60) {  
image(photo2, 0, 0);  
}  
else if (distance\<40) {  
image(photo3, 0, 0);  
}

Do I have to write something like that in draw ()?  
And how can I get income distance value as number from which depends displayed image?  
And should I upload any specific library for this?

Would be great if someone could suggest any examples or codes for this.

_ **Hoping for advise,** _🙃  
_ **komats** _

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [June 16, 2018, 12:53am UTC](https://discourse.processing.org/t/processing-image-display-via-arduino-ultrasonic-sensor/947/2 "2018-06-16T00:53:52Z")

</div>

Code below presenting the concept. Please don’t forget to cross-link between your different posts like [this one](https://forum.processing.org/two/discussion/28055/processing-image-display-via-arduino-ultrasonic-sensor/p1). It helps to maintain relevant posts together.

Notice also the changes handling data arriving from your arduino.

Kf

```java
import processing.serial.*;  

final int FAR_DISTA=60;
final int CLOSE_DISTA=60;

Serial myPort;  
String data="" ;
PFont myFont;  
int distance;

PImage[] imageStack=new PImage[3];

void setup() {
  size(1366, 900); // size of processing window
  //background(0);// setting background color to black

  myPort = new Serial(this, "/dev/cu.usbmodemfd111", 9600);
  myPort.bufferUntil('\n');
  
  imageStack[0]=loadImage("image0.jpg");
  imageStack[1]=loadImage("image1.jpg");
  imageStack[2]=loadImage("image2.jpg");
}

void draw() {

  background(0);
  textAlign(CENTER);
  fill(255);
  text(data, 820, 400);
  textSize(100);
  fill(#4B5DCE);
  text(" Distance : cm", 450, 400);
  noFill();
  stroke(#4B5DCE);

  if (distance>FAR_DISTA) {
    image(imageStack[2], 0, 0); //display first photograpgy
  } else if (distance>CLOSE_DISTA && distance<FAR_DISTA) {
    image(imageStack[1], 0, 0);
  } else if (distance<40) {
    image(imageStack[0], 0, 0);
  }
}

void serialEvent(Serial myPort) {

  data=myPort.readStringUntil('\n');  
  if (data!=null) {   
    distance=int(trim(data));
  }
}

```

---

<div class="post-metadata">

### Author: ![komats](https://avatars.discourse-cdn.com/v4/letter/k/7bcc69/32.png) [@komats](https://discourse.processing.org/u/komats)
#### Post date: [June 16, 2018, 6:22am UTC](https://discourse.processing.org/t/processing-image-display-via-arduino-ultrasonic-sensor/947/3 "2018-06-16T06:22:23Z")

</div>

Thanks @kfrajer !  
Sorry, that I didn’t close this topic (don’t know how). I got an answer!  
I thought that maybe in this website users are not so active, so I asked this question in older forum.  
But I also am looking through your suggestion, in which there are some new things for me. Thanks for reply!
