Trigger a video file from computer to play when someone approaches the sonar.

Hello everyone. I am making a small project with an Arduino uno, a sonar sensor and a pc. What I want to do is when someone approaches the sensor, the Arduino start playing a video on my computer. From my research I understand that the Arduino can't play itself a video file, so I have to make it read the sensor data and, when someone is close, the Arduino starts the video player (vlc?) on my computer in order to play the file. I am very new to this and every kind of help will be very very usefull. I think I have to use processing but I need your help to do any of that. Thanks in advance for your time!

SO this is an underwater project?

You will need a program running on the PC, that will wait for input from the Arduino (serial com port would be easiest) and launch the video when the correct input is received.

Oh no. Sorry if I described it wrong. By "sonar", I mean the HC-SR04 sensor.

You described the HC-SR04 correctly, it is a sonar sensor.

Ok then. I am sorry for any missunderstandings. I am really new to all these. So, what you are saying is that I can find a software that reads the data from the Arduino and and when it receives the defined value, it will start playing the video. Am I getting this right?

SONAR does not necessarily have to be underwater. It stands for "Sound Navigation And Ranging" - the medium is irrelevant. You just don't typically see it used in air because it's nowhere near as efficient as other methods, but underwater it is way more common because of the attenuation of electromagnetic waves under water.

Anyway. there's no reason you couldn't use a sonar, but it's a bit overkill. A PIR sensor would surely do the trick? You are just looking for an on/off based on proximity, correct?

As for the PC side... I am fairly sure you can start a video with a serial command. So, simply monitor whichever comm port the arduino is plugged into for a trigger you define.

In your program on the arduino, simply serial.print the state of a pin which is monitoring with a PIR sensor. Pin goes high, arduino sends this via serial, PC receives it, and whatever script you have running on the PC (or even raspberry pi?) switches on a video.

In fact you could do this AND play a video with a raspberry pi... Without an arduino in there at all.

If you are using a Windows PC, there is no obvious way to do what you want to do. I suspect that most people would write a custom program that runs on the PC, monitors the serial port for input from the Arduino, and then executes system commands.

If you have a version of linux installed on the PC, it would be rather easy to use the command line shell to accomplish the above. Otherwise, using a Raspberry Pi as suggested previously would be the simplest approach. Unfortunately the Pi is 3.3V only, so you would need a 3.3V sonar.

For arduino uno (or nano, mega, other stuff without native USB) you’re stuck writing a program to listen on the serial port and react to it.

With an Arduino Leonardo/Micro/other board with native USB, you could have it emulate a keyboard and send a key combo (which you know doesn’t otherwise do anything) and have something like AutoHotKey running a very simple script triggered off that keypress (much shorter learning curve for the pc software.)

fall-apart-dave:
SONAR does not necessarily have to be underwater. It stands for "Sound Navigation And Ranging" - the medium is irrelevant. You just don't typically see it used in air because it's nowhere near as efficient as other methods, but underwater it is way more common because of the attenuation of electromagnetic waves under water.

Anyway. there's no reason you couldn't use a sonar, but it's a bit overkill. A PIR sensor would surely do the trick? You are just looking for an on/off based on proximity, correct?

As for the PC side... I am fairly sure you can start a video with a serial command. So, simply monitor whichever comm port the arduino is plugged into for a trigger you define.

In your program on the arduino, simply serial.print the state of a pin which is monitoring with a PIR sensor. Pin goes high, arduino sends this via serial, PC receives it, and whatever script you have running on the PC (or even raspberry pi?) switches on a video.

In fact you could do this AND play a video with a raspberry pi... Without an arduino in there at all.

jremington:
If you are using a Windows PC, there is no obvious way to do what you want to do. I suspect that most people would write a custom program that runs on the PC, monitors the serial port for input from the Arduino, and then executes system commands.

If you have a version of linux installed on the PC, it would be rather easy to use the command line shell to accomplish the above. Otherwise, using a Raspberry Pi as suggested previously would be the simplest approach. Unfortunately the Pi is 3.3V only, so you would need a 3.3V sonar.

DrAzzy:
For arduino uno (or nano, mega, other stuff without native USB) you're stuck writing a program to listen on the serial port and react to it.

With an Arduino Leonardo/Micro/other board with native USB, you could have it emulate a keyboard and send a key combo (which you know doesn't otherwise do anything) and have something like AutoHotKey running a very simple script triggered off that keypress (much shorter learning curve for the pc software.)

First, thank you all for your help! What i have is an Arduino Uno, the HC-SR04 sensor, and a laptop with no serial com port. I was assigned to do this project with this equipment and i have been provided these stuff only. So Raspberry Pi, other sensors and such are out of the table. It's a small demonstration project and my professor assured me that it can be done easily and suggested to use processing. Make the sensor working and give me some values on arduino ide is the easy part i guess. I saw tutorials and have tried some stuff with sensors and leds on arduino, so i believe i can do this part myself. What you're saying is that there is no way under these circumstances that i can make the pc to read these values and when someone approaches the sonar, to start playing a video? It didn't sound that complicated as an idea. Is it?

I can see that with processing i can monitor the port that i have my arduino connected and make it communicate with my pc. So wouldn't it be possible for processing to read the values and when it gets the right value from the arduino to start playing a video?

Yes it is. that's what we were saying. You can do it that way - you just need to write something for your PC that listens to the comm port and reacts to a command. How you operate VLC or similar over comm port I have no idea.

But on the arduino, you have it listen to the sonar, when it has a contact (repeated, say 10 hits in a row) it sends out a "I see something" on the serial and the PC picks that up.

Write a sketch to monitor the sonar
Use serial print to send the status of the sonar to the PC
Write something for the PC to listen for a status that indicates a target

Sounds simple when you put it like that...

I get it. So what i have to figure out is how i will write something in processing that does that. Someone told me that the processing environment itself has the ability to play a video. If this is true, i guess i have to find out the right code to do that.

Hello again guys. I managed to make processing read the serial port from arduino and print it in it's console. I also managed to play a video file in processing by adding a video codec library. So what i have to figure out is to tell processing to run the video player when a specific number comes from the arduino port. Does anyone have any ideas? Or, if my logic is incorrect, can you tell me why? I can post the sketches i already have if it helps.

Hi CaptainNik.
I am wanting to do a similar thing, except play a video file on loop, and then when a sensor is triggered, switch to a different video file. I have no idea how to do the programming on the PC, but can see that the logic of it is fairly simple. Any luck with getting your problem solved?
Adam

jremington:
Unfortunately the Pi is 3.3V only, so you would need a 3.3V sonar.

why couldnt he use a level shifter like this one:

adamhutterer:
Hi CaptainNik.
I am wanting to do a similar thing, except play a video file on loop, and then when a sensor is triggered, switch to a different video file. I have no idea how to do the programming on the PC, but can see that the logic of it is fairly simple. Any luck with getting your problem solved?
Adam

skypickle:
why couldnt he use a level shifter like this one:
TXB0104 Bi-Directional Level Shifter [TXB0104] : ID 1875 : $3.50 : Adafruit Industries, Unique & fun DIY electronics and kits

Hello again guys. Yes, i did what i wanted to do with help from a friend of mine and and some guys in another forum. My code plays a video file in loop when triggered. More specific, it starts playing a video file from a path in my pc, when someone approaches the sensor anywhere closer than 100 cm. When the person moves out from that range, it pauses the video and if someone approaches again bellow 100 cm, it starts the video from the beginning. Here’s the code if you believe it can help you or for anyone that is interested in it. :smiley:

import processing.serial.*;
import processing.video.*;
Serial myPort;  // Create object from Serial class
int val = 99;     // Data received from the serial port
String PATH = "D:\\nikos\\NIKOS\\VIDEOS\\Inside.Man.2006.1080p.BluRay.x264.anoXmous_.mp4";
Movie mov;
String inData = "";
int THRESHOLD = 100;
boolean playing = false;

void setup() {
  size(1920, 816);
  frameRate(23.98);
  mov = new Movie(this, PATH);
  mov.speed(1);
  mov.volume(30);
  mov.pause();

  String COM1 = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, COM1, 9600);
  
}

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

void draw() {
  if ( myPort.available() > 0) {  // If data is available,
         try { 
            inData = myPort.readStringUntil('\n');
            if ( inData == null ) {
               throw new Exception("Error reading: "+inData);
            }
            else
              inData = inData.trim();
            val = Integer.parseInt(inData);
            if (val <= THRESHOLD) { //If the sensor detect object at less 100cm
              if (!playing) {
                mov.jump(0);
                mov.loop();
                playing = true;
              }
            }else{
              if (playing) {
                mov.pause();
                playing = false;
              }
            }
       } catch (Exception ex) {
         println("Exception:"+ex.toString()+" Data:"+inData);
       }
       println(">"+inData+" -> "+val+"<");
  } 
  

  image(mov, 0, 0, width, height);
}
1 Like

If you need also the Arduino code i used, i can post it too.

1 Like

@CaptainNik
please upload the Arduino code! thank you

@CaptainNik could you please upload the arduino code please! it would be really helpful, im working on a similar project! thanks!