Processing (image display) via Arduino ultrasonic sensor

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:


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:

/*
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:

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,:upside_down_face:
komats

1 Like

Code below presenting the concept. Please don’t forget to cross-link between your different posts like this one. It helps to maintain relevant posts together.

Notice also the changes handling data arriving from your arduino.

Kf

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

1 Like

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!