Override PIR hardware HIGH timeout with Processing code

edited September 2014 in Arduino

Hi all

I am working with a Processing + Arduino + PIR + Relay setup to trigger a video and lighting display sequence. A basic schematic is on the image below and my code follows. Although the code is 99% working, I have to keep on activating the motion sensor to view the complete movie sequence.

Is there a code line to keep the PIR HIGH for the duration of the ten minute video?

Many thanks Byron

Morgan - Schematic 20140928 A

// MY SETUP //
/* Apple OSX Version 10.9.5, Java 7.67, Processing 2.2.1, Arduino Uno, Relay board 8CH 5V, PIR sensor */

// LIBRARIES TO IMPORT //
import processing.serial.*;
import cc.arduino.*;
import processing.video.*;

// GLOBAL VARIABLES //
Arduino arduino; // Variable for Arduino
Movie myMovie; // Variable for movie
int pirPin = 2; // Variable for PIR
int pinRelay1 = 3; // Variable for Godly Column relay
int pinRelay2 = 4; // Variable for Blue Gradient relay
int pinRelay3 = 5; // Variable for Magic Magnet relay
int pinRelay4 = 6; // Variable for Hula Hoops relay
int pinRelay5 = 7; // Variable for Silicone Landscape relay
int pinRelay6 = 8; // Variable for Ceiling Lamp relay
int pinRelay7 = 9; // Variable for Laughing Lights relay
int pinRelay8 = 10; // Variable for UV Light relay
int ledPin = 13; // Variable to show PIR motion detected 
int calibrationTime = 5; // Variable for PIR to calibrate
int time = millis(); // Variable for time
int seconds = 0; // Variable for seconds
int lastmiltime = 0;

// INITIAL ENVIRONMENT PROPERTIES //
void setup() {
  arduino = new Arduino(this, "/dev/tty.usbmodemfd111", 57600); // Declares Arduino serial sport
  print(" Calibrating "); // Prints to console area
  for(int i = 0; i < calibrationTime; i++){ // Declares sensor time to calibrate
      print("."); // Prints to console area
      delay(1000); // Delay everything for 1 second
  }
  print(" calibrated "); // Prints to console area
  arduino.pinMode(pirPin, arduino.INPUT); // Declares PIR sensor as Arduino input
  arduino.pinMode(ledPin, arduino.OUTPUT); // Declares LED as an Arduino output
  arduino.pinMode(pinRelay1, arduino.OUTPUT); // Declares Relay1 as an Arduino output
  arduino.pinMode(pinRelay2, arduino.OUTPUT); // Declares Relay2 as an Arduino output
  arduino.pinMode(pinRelay3, arduino.OUTPUT); // Declares Relay3 as an Arduino output
  arduino.pinMode(pinRelay4, arduino.OUTPUT); // Declares Relay4 as an Arduino output
  arduino.pinMode(pinRelay5, arduino.OUTPUT); // Declares Relay5 as an Arduino output
  arduino.pinMode(pinRelay6, arduino.OUTPUT); // Declares Relay6 as an Arduino output
  arduino.pinMode(pinRelay7, arduino.OUTPUT); // Declares Relay7 as an Arduino output
  arduino.pinMode(pinRelay8, arduino.OUTPUT); // Declares Relay8 as an Arduino output
  size(320, 180); // Declares size of sketch
  myMovie = new Movie(this, "timer.mov"); // Declares movie file name
}

//CODE TO CONTINUOUSLY EXECUTE //
void draw() {
  if(arduino.digitalRead(pirPin) == arduino.HIGH) { // If motion is detected
    if (millis()-lastmiltime>1000) {
      seconds++;
      lastmiltime = millis();
      }
    if (seconds > time + 1) {
      myMovie.play(); // Play movie once
      image(myMovie, 0, 0, 320, 180); // Position and scale movie
      }
    if (seconds > time + 1) {
      arduino.digitalWrite(pinRelay1, arduino.HIGH); // Turn Relay1 on
      }    
    if (seconds > time + 10) {
      arduino.digitalWrite(pinRelay1, arduino.LOW); // Turn Relay1 off
      }       
    if (seconds > time + 3) {
      arduino.digitalWrite(pinRelay2, arduino.HIGH); // Turn Relay2 on
       }
    if (seconds > time + 4) {
      arduino.digitalWrite(pinRelay2, arduino.LOW); // Turn Relay2 off
       }  
    if (seconds > time + 5) {
      arduino.digitalWrite(pinRelay3, arduino.HIGH); // Turn Relay3 on
       }
    if (seconds > time + 6) {
      arduino.digitalWrite(pinRelay3, arduino.LOW); // Turn Relay3 off
       }  
    if (seconds > time + 7) {
      arduino.digitalWrite(pinRelay4, arduino.HIGH); // Turn Relay4 on
       }
    if (seconds > time + 8) {
      arduino.digitalWrite(pinRelay4, arduino.LOW); // Turn Relay4 off
       }
    if (seconds > time + 9) {
       arduino.digitalWrite(pinRelay5, arduino.HIGH); // Turn Relay5 on
       }
    if (seconds > time + 10) {
       arduino.digitalWrite(pinRelay5, arduino.LOW); // Turn Relay5 off
       }
    if (seconds > time + 11) {
       arduino.digitalWrite(pinRelay6, arduino.HIGH); // Turn Relay6 on
       }
    if (seconds > time + 12) {
       arduino.digitalWrite(pinRelay6, arduino.LOW); // Turn Relay6 off
       }
     if (seconds > time + 13) {
      arduino.digitalWrite(pinRelay7, arduino.HIGH); // Turn Relay7 on
       }
     if (seconds > time + 14) {
      arduino.digitalWrite(pinRelay7, arduino.LOW); // Turn Relay7 off
      }
     if (seconds > time + 15) {
      arduino.digitalWrite(pinRelay8, arduino.HIGH); // Turn Relay8 on
      }
     if (seconds > time + 16) {
       arduino.digitalWrite(pinRelay8, arduino.LOW); // Turn Relay8 off
       }
  }
  if (arduino.digitalRead(pirPin) == arduino.LOW) {
    print("."); // Prints to console area
    }
}

// CALLED EVERY TIME A NEW FRAME IS AVAILABLE //
void movieEvent(Movie m) {                                      
  m.read(); // Not sure if this is needed
}
Tagged:

Answers

  • edited October 2014

    Many thanks to Robin Gilham based in Cape Town for helping me with the question above. To resolve the PIR hardware calibration, 'switch' control code is required. The code has been updated below and the key Processing links are http://processing.org/reference/switch.html and http://processing.org/reference/increment.html

    // LIBRARIES TO IMPORT //
    import processing.serial.*;
    import processing.video.*;
    import cc.arduino.*;
    
    // GLOBAL VARIABLES //
    Arduino arduino;
    Movie myMovie;
    int pirPin = 2;
    int relayPin1 = 3;
    int relayPin2 = 4;
    int relayPin3 = 5;
    int relayPin4 = 6;
    int relayPin5 = 7;
    int relayPin6 = 8;
    int relayPin7 = 9;
    int relayPin8 = 10;
    int relayPin9 = 11;
    int buttonPin = 12;
    int ledPin = 13;
    int time = millis();
    int num = 0;
    int calibrationTime =30;
    boolean sketchFullScreen() {
      return true;
    }
    
    void setup() {
      size(displayWidth, displayHeight);
      background(0);
      myMovie = new Movie(this, "Timer_5_minute.mov");
      arduino = new Arduino(this, "/dev/tty.usbmodemfd111", 57600);
      print(" Calibrating ");
      for(int i = 0; i < calibrationTime; i++){
        print(".");
        delay(1000);}
      print(" Calibrated ");
      arduino.pinMode(pirPin, arduino.INPUT);
      arduino.pinMode(ledPin, arduino.OUTPUT);
      arduino.pinMode(relayPin1, arduino.OUTPUT);
      arduino.pinMode(relayPin2, arduino.OUTPUT);
      arduino.pinMode(relayPin3, arduino.OUTPUT);
      arduino.pinMode(relayPin4, arduino.OUTPUT);
      arduino.pinMode(relayPin5, arduino.OUTPUT);
      arduino.pinMode(relayPin6, arduino.OUTPUT);
      arduino.pinMode(relayPin7, arduino.OUTPUT);
      arduino.pinMode(relayPin8, arduino.OUTPUT);
      arduino.pinMode(relayPin9, arduino.OUTPUT);
    }
    
    void draw() {
      switch(num) {
        case 0:
         if (arduino.digitalRead(pirPin) == arduino.HIGH) {
           print ("..... Playing .....");
           time = millis();
           num++;
         }
         if (arduino.digitalRead(pirPin) == arduino.LOW) {
           print(".");
           delay(1000);
         }
        break;
        case 1:
          if (millis() - time > 1) {
            time = millis();
            num++;
          }
        break;
        case 2:
          if (millis() - time > 2) {
            myMovie.play();
            image(myMovie, 0, 0, 320, 180);
            frame.setLocation(0,0);
          }
          if (millis() - time > 30000) {
            arduino.digitalWrite(relayPin1, arduino.HIGH);
          }
          if (millis() - time > 40000) {
            arduino.digitalWrite(relayPin1, arduino.LOW);
          }
          if (millis() - time > 60000) {
            arduino.digitalWrite(relayPin2, arduino.HIGH);
          }
          if (millis() - time > 70000) {
            arduino.digitalWrite(relayPin2, arduino.LOW);
          }
          if (millis() - time > 90000) {
            arduino.digitalWrite(relayPin3, arduino.HIGH);
          }
          if (millis() - time > 100000) {
            arduino.digitalWrite(relayPin3, arduino.LOW);
          }
          if (millis() - time > 120000) {
            arduino.digitalWrite(relayPin4, arduino.HIGH);
          }
          if (millis() - time > 130000) {
            arduino.digitalWrite(relayPin4, arduino.LOW);
          }
          if (millis() - time > 150000) {
            arduino.digitalWrite(relayPin5, arduino.HIGH);
          }
          if (millis() - time > 160000) {
            arduino.digitalWrite(relayPin5, arduino.LOW);
          }
          if (millis() - time > 180000) {
            arduino.digitalWrite(relayPin6, arduino.HIGH);
          }
          if (millis() - time > 190000) {
            arduino.digitalWrite(relayPin6, arduino.LOW);
          }
          if (millis() - time > 210000) {
            arduino.digitalWrite(relayPin7, arduino.HIGH);
          }
          if (millis() - time > 220000) {
            arduino.digitalWrite(relayPin7, arduino.LOW);
          }
          if (millis() - time > 240000) {
            arduino.digitalWrite(relayPin8, arduino.HIGH);
          }
          if (millis() - time > 130000) {
            arduino.digitalWrite(relayPin8, arduino.LOW);
          }
          if (millis() - time > 300000) {
            time = millis();
            num++;
          }
          break;
        case 3: 
          print(" ... Waiting ");
          myMovie.stop();
          time = millis();
          num = 0;
          break;
          default:
            print (" ERROR no state ");
            num = 0;
      }
    }
    
    // CALLED EVERY TIME A NEW FRAME IS AVAILABLE //
    void movieEvent(Movie m) {                                      
      m.read();
    }
    
    public void init() {
      frame.removeNotify();
      frame.setUndecorated(true); 
      frame.addNotify();
      super.init();
    }
    
Sign In or Register to comment.