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