We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi. A friend and I are in the middle of a video installation tests. I'm using an Arduino PIR Sensor and an Arduino Uno board.
I need to stop a video when the PIR is "On" (Detects motion) and to play that video if the PIR is "Off".
Checking the Processing and Arduino websites, examples and references I couldn't figured out what I'm doing wrong. Please let me know if something is missing or left. Thanks in advance for any help!
This is the Arduino Code (copied from Arduino and Processing communication tutorials found on the web):
/*
PIR sensor tester
*/
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;
}
}
}
And here is the Processing code:
import processing.serial.*;
import processing.video.*;
Serial myPort;
String val;
Movie video;
void setup() {
size(640,480);
video = new Movie(this,"sujeto1.mp4");
video.loop();
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
}
void serialEvent (Serial myPort) {
if (myPort.available() > 0) {
val=myPort.readStringUntil('\n');
}
if (val=="Motion detected!") {
video.stop();
} else {
video.loop();
}
println(val);
}
void draw() {
background(0);
image(video,0,0);
}
void movieEvent(Movie video) {
video.read();
}
Answers
https://Processing.org/reference/String_equals_.html
https://Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_5
Thanks for the answer @GoToLoop ! I visited both references but I still don't get it. With the equals() issue I understood that if you want to compare too strings is necessary to declare them before(?). But, how could I make Processing to read the String "Motion detected!" inside an "if" block? Or to read the value stored in "myString"?
I'm kinda new in Processing and newer in Arduino so I don't know if this code is closer to the right one.
Thanks!!!
Try this modified code for your Processing's serialEvent(). This is not the solution but you can see how the serial port is behaving (or if you are receiving any data). Notice you were missing a line in your function... the one that was reading the data from the serial stream.
Kf
Thanks @kfrajer for your answer. I could not integrate your code very well, but I'm receiving the data pretty well. The operation of the PIR is 10/10 and the communication between the serial port and processing is well too.
The thing that I don't know is how to go from the string that is received in Processing to make the video to play from that string. I explain myself well? (English is not my native language as you could notice :D)
Hi. Mixing the answers I got to this code:
The thing now is that the video is "playing" in loop but not "showing" in loop. When the PIR is "On" the video stops when the value "Motion detected!" appears (all ok with that) but when the other value appears (Motion ended!") the video doesn't continue in loop but it's still reading as loop, so everytime the value appears it seems like I advanced the video :-?? Could I be missing something in void draw() ? or the void movieEvent() :-?
Problem is noLoop()! The original example code didn't need to have draw() called back at 60 FPS.
However, in order to display each arrived video frame, draw() needs to happen. #-o
You can move
redraw = true;
into movieEvent(). *-:)Or even delete noLoop() &
redraw = true;
from the sketch entirely. 8-XMore about noLoop() & redraw(): :-B
Try these posts:
https://forum.processing.org/two/discussion/22640/can-anyone-help-me-find-what-s-wrong-with-my-code-for-a-video-player-based-off-sensor-reading#latest
https://forum.processing.org/two/discussion/25094/set-distance-value-with-ultrasonic-sensor#latest
Kf
Thank you @GoToLoop and @kfrajer for your comments, knowledge and advices. The code works fine now and I can continue with the other parts of my project.
:D :D :D :D