Project overview: I am connecting an Arduino to processing because I want to speed up/speed down a video through LOW/HIGH on the motion sensor. When the motion sensor detects movement, I need the speed of the video to slow down. So I was thinking it would be an if/else statement which would say if motion detected then play the video at .5 speed. else, play at 3x speed. How can I do this?
I am having difficulties with creating an if/else statement.
I have successfully connected Arduino ide to processing and before inputting a if/else statement, my console was giving me results “motion detected!” and “motion stopped!”.
With the code below I am getting NullPointerException!
I am an art student. This is my first venture coding. I have been studying these programs for two days straight but cannot put it together. Please be direct! It would be most helpful. Thank you!
Processing
import processing.video.*;
import processing.serial.*;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
Movie movie;
void setup()
{
// I know that the last port in the serial list on my mac
// is Serial.list()[3].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[3]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
//movie
size(360, 360);
movie = new Movie(this, "datacounter.mp4");
delay(100);
//different movie functions
movie.play();
movie.jump(0);
//movie.pause();
movie.speed(0.5);
//movie.stop();
movie.loop();
}
// Read new frames from the movie.
void movieEvent(Movie movie) {
movie.read();
}
**void draw()**
**{**
** if ( myPort.available() > 0) // was 0 **
** { // If data is available,**
** val = myPort.readStringUntil('\n'); // read it and store it in val**
** } **
** println(val);**
** **
**if (val.equals("Motion")) {**
** movie.speed(3);**
** } else {**
** movie.speed(0.5);**
** }**
//video
image(movie, 0, 0);
//experiment ^
}
Arduino Code
#define pirSensor 2
int ledPin = 12;
int val;
void setup() {
// put your setup code here, to run once:
pinMode(pirSensor, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
val = digitalRead(pirSensor);
if (val == HIGH) {
//sensor motion
Serial.println("motion");
//turn led on if motion hoe
digitalWrite(ledPin, HIGH);
}
else {
//sensor inactive
Serial.println("nothin");
//turn the light off hoe
digitalWrite(ledPin, LOW);
}
}