# Playing video with PIR motion sensor

**URL:** <https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [February 8, 2019, 2:00pm UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214 "2019-02-08T14:00:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kud7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kud7/32/673_2.png) [@kud7](https://discourse.processing.org/u/kud7)\
**Post date:** [February 8, 2019, 2:00pm UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214/1 "2019-02-08T14:00:16Z")

</div>

Processing sketch working great, which i can play video from hdd and PIR motion sensor detect my motion but actually i want to trigger video or Processing sketch from with PIR motion sensor. I also tested motion sensor with Arduino led pins, it’s flashing when PIR sensor detect motion.

How can I connect this two sketch each other and i can play any video file with motion sensor in Processing.  
Thank you!

ARDUINO

````auto
/*
   PIR sensor tester
*/
 
int ledPin = 13; // choose the pin for the LED
int inputPin = 3; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
 
void setup() {
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(inputPin, INPUT); // declare sensor as input
 
  Serial.begin(9600);
}
 
void loop() {
  val = digitalRead(inputPin); // read input value
  if (val == HIGH) { // check if the input is HIGH
    digitalWrite(ledPin, HIGH); // turn LED ON
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    if (pirState == HIGH) {
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}```

````

PROCESSING

````auto
/**
 * Efficient Serial Reading (v1.01)
 * GoToLoop (2016-Jan-19)
 *
 * forum.Processing.org/two/discussion/14534/
 * myport-available-always-0#Item_1
 *
 * forum.Processing.org/two/discussion/16618/
 * processing-with-arduino-void-serialevent#Item_5
 */
import processing.video.*;
import processing.serial.Serial;
 
static final int PORT_INDEX = 0, BAUDS = 9600;
String myString;
 
Movie video;
 
void setup() {
  size(640,480);
  video = new Movie(this,"render.mp4");
  video.loop();
  //noLoop();
  final String[] ports = Serial.list();
  printArray(ports);
  new Serial(this, ports[PORT_INDEX], BAUDS).bufferUntil(ENTER);
}
 
void draw() {
  background(0);
  image(video,0,0);
 
  println(myString);
}
 
void serialEvent(final Serial s) {
  myString = s.readString().trim();
  redraw = true;
  if (myString.equals("Motion detected!")) {
    video.stop();
  } 
}
 
void movieEvent(Movie video) {
  video.read();
}```
````

---

<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:** [February 8, 2019, 3:08pm UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214/2 "2019-02-08T15:08:31Z")

</div>

> [@kud7](#):
>
> val==“Motion detected!”

> [@kud7](#):
>
> \*\* Serial.print("motion detected at ");\*\*
> 
> \*\* Serial.print(millis()/1000);\*\*
> 
> \*\* Serial.println(" sec");\*\*

-a- strings can not be compared with ==

> **[equals() / Reference](https://processing.org/reference/String_equals_.html)**
>
> Compares two strings to see if they are the same. This method is necessary because it's not possible to compare strings using the equality operator (==). Returns true if the strings are the sam…

-b- still they must be exactly same, esp about ending bytes… \l \n

-c- possibly something like substring compare might help

> **[indexOf() / Reference](https://processing.org/reference/String_indexOf_.html)**
>
> Tests to see if a substring is embedded in a String, and returns the index position of the first occurrence of the substring defined in the str parameter. If the str substring is not fou…

if ( val.indexOf(“detected”) \> 0 )

but first pls  
post in above your two codes again by using

```auto
</>

```

the code tag  
now is not testable and difficult readable

and also pls show use the console print  
with the incoming from arduino

---

<div class="post-metadata">

**Author:** ![kud7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kud7/32/673_2.png) [@kud7](https://discourse.processing.org/u/kud7)\
**Post date:** [February 10, 2019, 12:33am UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214/3 "2019-02-10T00:33:14Z")

</div>

Thanks kll, i just edit my post.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 15, 2019, 6:00pm UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214/4 "2019-02-15T18:00:46Z")

</div>

@kud7 – were you able to resolve your issue once you got string comparison working?

---

<div class="post-metadata">

**Author:** ![kud7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kud7/32/673_2.png) [@kud7](https://discourse.processing.org/u/kud7)\
**Post date:** [February 16, 2019, 4:31am UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214/6 "2019-02-16T04:31:40Z")

</div>

Yes Finally figured it out. It’s all working great except delay time. Processing takes about 5 second to start up the video. Now i am trying to minimize delay time. Any idea what might be cause that issue?

---

<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:** [February 16, 2019, 4:44am UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214/7 "2019-02-16T04:44:09Z")

</div>

This might help, [https://forum.processing.org/two/discussion/8109/prepare-a-second-video-to-play-without-slowing-down-the-first-video#latest](https://forum.processing.org/two/discussion/8109/prepare-a-second-video-to-play-without-slowing-down-the-first-video#latest)

Kf

---

<div class="post-metadata">

**Author:** ![kud7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kud7/32/673_2.png) [@kud7](https://discourse.processing.org/u/kud7)\
**Post date:** [February 16, 2019, 4:47am UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214/8 "2019-02-16T04:47:46Z")

</div>

Thanks mate 🙂

---

<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:** [February 16, 2019, 4:52am UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214/9 "2019-02-16T04:52:30Z")

</div>

Actually I will reconsider not using stop(). I havent used the video lib lately. Is there a pause() function? The idea is that you load your resources in setup and they are loaded only once in your program. I am assuming you are only loading a single file.

If you are playing a single video, and if pause is not available, then what you could do is not show the image in draw() when you want to simulate a pause state/“no motion detected” case.

Also, avoid println() in draw(). It is good for debugging but nothing more. This could be useful as well: [https://forum.processing.org/two/discussion/25094/set-distance-value-with-ultrasonic-sensor#latest](https://forum.processing.org/two/discussion/25094/set-distance-value-with-ultrasonic-sensor#latest)

Kf

---

<div class="post-metadata">

**Author:** ![kud7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kud7/32/673_2.png) [@kud7](https://discourse.processing.org/u/kud7)\
**Post date:** [February 17, 2019, 11:55pm UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214/11 "2019-02-17T23:55:12Z")

</div>

Yes only one video file, Unfortunately i haven’t had chance to play with the sketch. As you guess I am quite new with Processing and sorry if my questions doesn’t make sense.  
Is there any tutorial or article about creating class in processing. I want to play Processing sketch instead of playing video file.

---

<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:** [February 19, 2019, 5:12am UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214/12 "2019-02-19T05:12:01Z")

</div>

Check this site: [https://happycoding.io/tutorials/](https://happycoding.io/tutorials/) It has a great intro to Processing, Java and other technologies. You can also check the Processing foundation’s website as they have [tutorials](https://processing.org/tutorials/) and [examples](https://processing.org/examples/). My favorite recommendation is to open the Processing IDE and dive right into the examples and find something that you like and try to understand it.

Kf

---

<div class="post-metadata">

**Author:** ![kud7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kud7/32/673_2.png) [@kud7](https://discourse.processing.org/u/kud7)\
**Post date:** [February 19, 2019, 5:50am UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214/13 "2019-02-19T05:50:36Z")

</div>

You are amazing kfrajer, thank you! I will check it out.

---

<div class="post-metadata">

**Author:** ![arduinonoob](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@arduinonoob](https://discourse.processing.org/u/arduinonoob)\
**Post date:** [February 10, 2020, 3:25am UTC](https://discourse.processing.org/t/playing-video-with-pir-motion-sensor/8214/14 "2020-02-10T03:25:18Z")

</div>

@kud7 I am working on something similar but your code is a little confusing. Do you have an updated version you could post? Much appreciated if you are able to!
