QUESTION: Triggering video using HC-SR04 distance sensor on RPI 4

Hi,
[First, I already searched here in the forum (Search results for 'ultrasonic' - Processing Foundation) but couldn’t find any similar topic that includes a solution]

I am working on an art installation which triggers a video once the audience gets to the close proximity of the ultrasonic sensor. For the test run I used arduino and laptop to do this, and here is the code that I wrote for it:

import processing.video.*;
import processing.serial.Serial;
 
static final int PORT_INDEX = 2, BAUDS = 9600;
String myString;
boolean switchVar=true;
Movie video;
 
void setup() {
  fullScreen();
  frameRate(12);
  video = new Movie(this,"sample.mp4");
 
  noLoop();
  final String[] ports = Serial.list();
  printArray(ports);
  new Serial(this, ports[PORT_INDEX], BAUDS).bufferUntil(ENTER);
}
 
void draw() {
  background(0);
  noCursor();
  image(video,0,0);
 if (switchVar==false){
 video.loop();
  } else {
      video.pause();
      background(0);
 }
  //println(myString);
}
 
void serialEvent(final Serial s) {
  myString = s.readString().trim();
  redraw = true;
  if (myString.equals("5")||myString.equals("10")||myString.equals("15")) {
 switchVar=false;
  }
 else {
   if  (myString.equals("30")||myString.equals("40")||myString.equals("50")||myString.equals("60")){
      switchVar=true;
       
    }
 }
  } 

 
void movieEvent(Movie video) {
  video.read();
}

This was working perfectly, but now I am trying to cleanup the installation further by eliminating the laptop+arduino and use a Raspberry Pi 4 unit instead. So now I am trying to repurpose the code to be able to run it through Processing that I installed on the RPI4, but I am stuck in that part where I need to use the GPIO pins for the Trig (output) and Echo (input). In another word, I don’t know how use the data that comes in from the Echo to calculate the distance and then trigger the video with it.
Could someone please help me with the coding structure?

Here is how I utilized the pins, if it helps:

Thanks,
Fb

p.s: here is the art installation that I am using this setup for:

1 Like

Hey farhadbahram, I know this is from a while ago, but did you ever figure this out? I am trying to do exactly the same thing.