Saturday, October 22, 2011

PIR sensor and RGB LED indicators

Using the same RGB LEDs from the last project, i hooked up a PIR sensor sample from Parallax. It takes about 30sec to configure its self each time it powers on, and blinks red from the first RGB LED while its configuring. Then it lays in wait for movement, and then vigorously blinks the RGB LEDs until the movement has stopped. The PIR sensor outputs Either LOW(0v) or HIGH(5v) to the digital Arduino pin it is connected to depending on the status of the sensor. If motion is present, the PIR goes HIGH, and goes to LOW every once in a while even if motion is present. Then once no more motion is detected, the PIR outputs LOW.

/*
* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
*
* Switches a LED according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
*
* @author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
* @date: 3. September 2006
*
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
*
* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
* (http://www.parallax.com/detail.asp?product_id=555-28027)
*
* The sensor's output pin goes to HIGH if motion is present.
* However, even if motion is present it goes to LOW from time to time,
* which might give the impression no motion is present.
* This program deals with this issue by ignoring LOW-phases shorter than a given time,
* assuming continuous motion is present during these phases.
*
*/

/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

boolean lockLow = true;
boolean takeLowTime;

int pirPin = 2; //the digital pin connected to the PIR sensor's output

int colorz;
int LEDx;
int delayx;
int pinx;
int pinz;
int piny;
int blnd;

/////////////////////////////
//SETUP
void setup()
{
Serial.begin(9600);
pinMode(pirPin, INPUT);

pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
//LED2
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(8, OUTPUT);
//LED3
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);

//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}

////////////////////////////
//LOOP
void loop()
{

if(digitalRead(pirPin) == HIGH)
{
int ps = 50;
light(1,1,ps); //LED1-Red
light(2,2,ps); //LED2-green
light(3,3,ps); //LED3-blue

blend(1,1,ps); //LED1-purple RG
blend(2,2,ps); //LED2-light blue GB
blend(3,3,ps); //LED3-yellow RB
blend(1,4,ps); //LED1-White RGB //the led visualizes the sensors output pin state

if(lockLow)
{
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}

if(digitalRead(pirPin) == LOW)
{
LEDLOW(); //the led visualizes the sensors output pin state

if(takeLowTime)
{
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause)
{
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}

void LEDLOW()
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
}

void light(int LEDx,int colorz,int delayx)
{
if(LEDx == 1) //if LEDx is 1(which is the first LED(digital pins 13-11))
{
if(colorz == 1) //if colorz is 1 then turn on pin 13 with delay in on/off,
{ // which is red of first RGB LED
blinkz(13, delayx);
}

if(colorz == 2) //if colorz is 2 then turn on pin 12 with delay in on/off,
{ //which is green of first RGB LED
blinkz(12, delayx);
}

if(colorz == 3) //if colorz is 3 then turn on pin 11 with delay in on/off,
{ //which is blue of first RGB LED
blinkz(11, delayx);
} //and so on for the rest of the if statements
}

if(LEDx == 2)
{
if(colorz == 1)
{
blinkz(3, delayx); //uses digital pin 3
}

if(colorz == 2)
{
blinkz(4, delayx); //uses digital pin 4
}

if(colorz == 3)
{
blinkz(8, delayx); //uses digital pin 8
}
}

if(LEDx == 3)
{
if(colorz == 1)
{
blinkz(7, delayx);
}

if(colorz == 2)
{
blinkz(6, delayx);
}

if(colorz == 3)
{
blinkz(5, delayx);
}
}
}

void blinkz(int pinx, int delayx) //blinks one LED at a time on/off with an established time delay(delayx)
{
digitalWrite(pinx, HIGH);
delay(delayx);
digitalWrite(pinx, LOW);
delay(delayx);
}

void blend(int LEDx, int colorz, int delayx)
{
if(LEDx == 1)
{
if(colorz == 1)
{
//red+green, purple
mixblend(13,12,delayx); //two pins at once to mix colors, or random two pins of any LED at a time.
}

if(colorz == 2)
{
//Green+blue, light blue
mixblend(12,11,delayx);
}

if(colorz == 3)
{
//red+blue,
mixblend(13,11,delayx);
}

if(colorz == 4)
{
whiteblend(13,12,11,delayx); //makes white(if you do three pins from same RGB LED, or random three pins of any LED at a time.
}
}

if(LEDx == 2)
{
if(colorz == 1)
{
mixblend(3,4,delayx);
}

if(colorz == 2)
{
mixblend(4,8,delayx);
}

if(colorz == 3)
{
mixblend(3,8,delayx);
}

if(colorz == 4)
{
whiteblend(3,4,8,delayx);
}
}

if(LEDx == 3)
{
if(colorz == 1)
{
mixblend(7,6,delayx);
}

if(colorz == 2)
{
mixblend(6,5,delayx);
}

if(colorz == 3)
{
mixblend(7,5,delayx);
}

if(colorz == 4)
{
whiteblend(7,6,5,delayx);
}
}
}

//pinx, pinz, are any two LED pins
//you want to light at one time
void mixblend(int pinx, int pinz, int delayx) //tells which two LEDs to blend
{
digitalWrite(pinx, HIGH);
digitalWrite(pinz, HIGH);
delay(delayx);
digitalWrite(pinx, LOW);
digitalWrite(pinz, LOW);
delay(delayx);
}

//pinx, pinz, piny are any three LED pins
//you want to light at one time, do three from same LED for white
void whiteblend(int pinx, int pinz, int piny, int delayx)
{
digitalWrite(pinx, HIGH);
digitalWrite(pinz, HIGH);
digitalWrite(piny, HIGH);
delay(delayx);
digitalWrite(pinx, LOW);
digitalWrite(pinz, LOW);
digitalWrite(piny, LOW);
delay(delayx);
}

*use the "Auto Format"(Cntrl + T) option under the tool menu in the Arduino IDE to fix the formatting. As you can see i didn't write this code, only added the RGB LED stuff to it...but it is fairly simple. Thr following is the original code.


/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;      

//the time when the sensor outputs a low impulse
long unsigned int lowIn;        

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

boolean lockLow = true;
boolean takeLowTime;

int pirPin = 7;    //the digital pin connected to the PIR sensor's output
int ledPin = 8;


/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }

////////////////////////////
//LOOP
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;          
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec");
         delay(50);
         }        
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){      
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause,
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                      
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
  }

 Pics:
 The Arduino is waiting for the PIR sensor to configure, and blinking red while it waits.
Done configuring PIR sensor, and waiting for something to move!
I moved the camera too much, and so the PIR sensor has sensed motion is blinking all the LEDs!

1 comment: