Playing video with PIR motion sensor


#1

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

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

/**
 * 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();
}```

#2

-a- strings can not be compared with ==
https://processing.org/reference/String_equals_.html

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

-c- possibly something like substring compare might help
https://processing.org/reference/String_indexOf_.html
if ( val.indexOf(“detected”) > 0 )

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

</>

the code tag
now is not testable and difficult readable

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


#3

Thanks kll, i just edit my post.


#4

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


#5

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)


#6

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?


#7

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

Kf


#8

Thanks mate :slight_smile:


#9

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

Kf