Cannot do if/else statement with Arduino PIR motion sensor

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

}
1 Like

@kfrajer Hi KF, I’ve seen you help many people. I worked on this project all day and this is where I have gotten. I am not sure how to make an if/else statement. I need to know what I am doing wrong and how exactly it can be fixed. Please help me out.

In this demo I have isolated the Processing code to changed the speed of the video either between x3.0 or x0.5. I do this by switching the state from either of two values 0 or 1 aka low or high resp. I map these values to either “motion” or “Nothin” as described in your arduino code.

Notice I am not detecting and applying changes in every frame but only when a change of state happens. The change is triggered on every mouse click. In your case, you need to adapt the code below to work with your arduino code and make sure you are not sending the “Nothin” or “Motion” in every loop cycle but only when a change is detected.

In other words, the Processing code works as you requested and now you need to adapt it with your arduino code.

Kf

import processing.video.*;

final String STR_LOW_STATE="Motion";
final String STR_HIGH_STATE="Nothing";
final int LOW_STATE=0;
final int HIGH_STATE=1;
final float LOW_STATE_SPEED=0.5;
final float HIGH_STATE_SPEED=3.0;

String val; // Data received from the serial port

Movie movie;

int state=0;

void setup()
{

  //movie
  size(360, 360);
  movie = new Movie(this, "train.mp4");
  setState(STR_LOW_STATE);  //Init string

  //different movie functions
  movie.play();
  movie.jump(0);  
  movie.loop();
}


void draw(){
  image(movie, 0, 0);
}

void mousePressed() {
  state=(state+1)%2;  //Changes state. Either 0 or 1
  setState( state==LOW_STATE ? STR_LOW_STATE : STR_HIGH_STATE );
}

void movieEvent(Movie movie) {  
  movie.read();
}

void setState(String newState) {
  val = newState;
  if (val.equals(STR_LOW_STATE)) {
    movie.speed(LOW_STATE_SPEED);
  } else {
    movie.speed(HIGH_STATE_SPEED);
  }
}
2 Likes

I finally got it to work!

thank you for all your help. may god bless you!

1 Like