/********************************************************
*  Code to relay the data from four PIR sensors to the PC
*  using a simple low-pass filter (weighted average).
*  By: Jonathan Mash
*  Date: Feb. 2009
*********************************************************/
 
#define PIR_HIST 5
int PIR_idx;
int PIR[4][PIR_HIST];
 
void setup()
{
  Serial.begin(9600);
 
  PIR_idx = 0;
}
 
 
void loop()
{
  Record();
 
  Serial.println(PIR[0][PIR_idx]);
  Serial.println(PIR[1][PIR_idx]);
  Serial.println(PIR[2][PIR_idx]);
  Serial.println(PIR[3][PIR_idx]);
 
  PIR_idx++;
  if(PIR_idx == PIR_HIST)
    PIR_idx = 0;
  delay(50);
}
 
void Record()
{
  int i = PIR_idx;
 
  PIR[0][PIR_idx] = (int)floor(analogRead(0)*0.75);
  PIR[1][PIR_idx] = (int)floor(analogRead(1)*0.75);
  PIR[2][PIR_idx] = (int)floor(analogRead(2)*0.75);
  PIR[3][PIR_idx] = (int)floor(analogRead(3)*0.75);
 
  if(i == 0)
    i = PIR_HIST-1;
  else
    i = i-1;
  PIR[0][PIR_idx] += (int)floor(PIR[0][i]*0.125);
  PIR[1][PIR_idx] += (int)floor(PIR[1][i]*0.125);
  PIR[2][PIR_idx] += (int)floor(PIR[2][i]*0.125);
  PIR[3][PIR_idx] += (int)floor(PIR[3][i]*0.125);
 
  if(i == 0)
    i = PIR_HIST-1;
  else
    i = i-1;
  PIR[0][PIR_idx] += (int)floor(PIR[0][i]*0.1);
  PIR[1][PIR_idx] += (int)floor(PIR[1][i]*0.1);
  PIR[2][PIR_idx] += (int)floor(PIR[2][i]*0.1);
  PIR[3][PIR_idx] += (int)floor(PIR[3][i]*0.1);
 
  if(i == 0)
    i = PIR_HIST-1;
  else
    i = i-1;
  PIR[0][PIR_idx] += (int)floor(PIR[0][i]*0.025); 
  PIR[1][PIR_idx] += (int)floor(PIR[1][i]*0.025); 
  PIR[2][PIR_idx] += (int)floor(PIR[2][i]*0.025); 
  PIR[3][PIR_idx] += (int)floor(PIR[3][i]*0.025);  
}