PIR motion sensor, Arduino, Processing, not working

I need help asap please

Hello, I am trying to get a mp4 video to play in processing whenever motion is detected in Arduino and then I need it to pause when motion ends. I am a student and I have been trying for 10 hours already. Direct answers would be greatly appreciated I am not an expert in code. Any help would be greatly appreciated.

Arduino Code

int ledPin = 13; // choose the pin for the LED

int inputPin = 2; // 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 Code

import processing.video.*;

import processing.serial.Serial;

static final int PORT_INDEX = 0, BAUDS = 9600;

String myString;

Movie video;

void setup() {

size(640,640);

//frameRate(60);

video = new Movie(this,"jimi.mp4");

video.loop();

noLoop();

final String[] ports = Serial.list();

printArray(ports);

new Serial(this, ports[3], BAUDS).bufferUntil(ENTER);

}

void draw() {

background(0);

image(video,0,0);

//frameRate(30);

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](https://video.read/)();

}
1 Like

@kfrajer hey please help!

Without an exact problem, I can only offer sanity checks:

  • Are you able to play your video in a sketch without any serial communication, etc.?
  • Does your Arduino sketch send data to the “Serial Monitor” within the Arduino IDE?

If both of those are working, then the issue lies between receiving and decoding the serial message.
Edit: I noticed you have defined PORT_INDEX in Processing but do not use it.
Make sure to use the serial port matching your Arduino from Serial.list()

I often use a single character when sending commands over a serial interface.
You could try sending just a “Y” or “N” from the Arduino and check for those in Processing.

Also, you will likely get more people looking at your code if it is formated.
Select it and use the “</>” icon when editing

2 Likes

Hello,

This is not correct:

import [processing.video](https://processing.video/).*;

noLoop() reference states :

Stops Processing from continuously executing the code within **draw()

I commented this out.

A serial connection may reboot Arduino (depends on the Arduino; watch for this) and I always add a:
delay(1000);
at the end of setup.

I moved:
//println(myString);
to the serialEvent() so it only updated when there was a serial event.

You should format code before posting:
https://discourse.processing.org/faq#format-your-code

  • The double spacing made this difficult to read.
  • Processing and Arduino IDE have an Auto Format that can be used.

It worked here with my video after I made changes above.

:slight_smile:

1 Like

Yes, from what I see, you need to do:

  1. In Processing, remove the noLoop() reference. You want to keep looping checking for incoming values
  2. I will add a println(myString) in your serial event to check what you receive. More like sanity check while developing.
  3. In your ino code, you need to add a delay between your transitions. This will not affect then end effect of your code but it will help in debugging.

Check this previous post that seems to be related to what you are doing.

Kf

1 Like