Go Down

Topic: Using Ultrasonic Range Finder SRF02 as Motion Detector (Read 3 times) previous topic - next topic

liudr

Code: [Select]
delay(250);                 // wait before next reading:

This line in your sonic ranger code could make fast object slip by without detection though. If your sonic ranger beam covers say 30 cm width at the point where objects pass, you just have to move at 30cm/0.25s or 1.2m/s at the right timing (Murphy said it will happen) to slip pass the sensor without detection. I can walk or skip faster than that. Is that very necessary or can you reduce it to 25ms?

xaral

I think you are right. I wrote 250 cause was the delay suggest in Arduino playgroung for this sensor. I will change it and see what happen...

I already updated the code but I'm still having problems

Code: [Select]
#include <Wire.h>

//definition of pin on Arduino board
const int PIR1 = 2;
const int PIR2 = 3;
const int LED1 = 4;
const int LED2 = 5;

//Definition of variables to use
int val1 = 0;
int val2 = 0;
int count1 = 0;
int count2 = 0;

int calibrationTime = 20;  //calibration time of PIR sensors

void setup()

{
  Wire.begin();          // start the I2C bus

  Serial.begin(9600);    // open the serial port:
 
  //Definition of input/output values
  pinMode(PIR1,INPUT);
  pinMode(PIR2,INPUT);
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);

 
  //give the sensor some time to calibrate
  Serial.println("Sensor Calibration in Progress");
  Serial.println("------------------------------");
 
  for(int i = 0; i < calibrationTime; i++){
    Serial.print(".");
    digitalWrite(LED1, HIGH);
    delay(250);
    digitalWrite(LED2, HIGH);
    delay(250);
    digitalWrite(LED1, LOW);
    delay(250);
    digitalWrite(LED2, LOW);
    delay(250);
  }

  Serial.println("");
  Serial.println("Sensor Calibration Completed");
  Serial.println("Sensor Reading Active");
  delay(100);
}

int reading = 0;
int oldreading = 0;

void loop()

{
passiveIR();
ultrasonic();
presence();

}

void passiveIR()
{
  int val1 = digitalRead(PIR1);
  int val2 = digitalRead(PIR2);
  Serial.print(val1);
  Serial.print(',');
  Serial.print(val2);
  Serial.print(','); 
}

void ultrasonic()

{

  // step 1: instruct sensor to read centimeters
  Wire.beginTransmission(112); // transmit to device #112 (0x70)
                               // the address specified in the datasheet is 224 (0xE0)
                               // but i2c adressing uses the high 7 bits so it's 112
  Wire.write(byte(0x00));      // sets register pointer to the command register (0x00) 
  Wire.write(byte(0x51));      // command sensor to measure in "inches" (0x50)
                               // use 0x51 for centimeters
                               // use 0x52 for ping microseconds
  Wire.endTransmission();      // stop transmitting

  // step 2: wait for readings to happen
  delay(70);                   // datasheet suggests at least 65 milliseconds

  // step 3: instruct sensor to return a particular echo reading
  Wire.beginTransmission(112); // transmit to device #112
  Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
  Wire.endTransmission();      // stop transmitting

  // step 4: request reading from sensor
  Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112

  // step 5: receive reading from sensor
  if(2 <= Wire.available())    // if two bytes were received
  {
    oldreading = reading;
    //Serial.print(oldreading);   
   // Serial.print(',');
    reading = Wire.read();    // receive high byte (overwrites previous reading)
    reading = reading << 8;   // shift high byte to be high 8 bits
    reading |= Wire.read();   // receive low byte as lower 8 bits
   

  }

  delay(25);                 // wait before next reading:

}

void presence()
{
  Serial.print(reading);    // print the reading
  Serial.print(',');
  Serial.print(oldreading);    // print the reading
  Serial.print(',');
  int f = reading - oldreading;
  Serial.print(f);    // print the reading
  Serial.print(',');
 
  if(val1 == HIGH && val2 == HIGH && f < 15){
    Serial.println('1');
    //Serial.print(',');
  }
  if(val1 == LOW || val2 == LOW || f >15){
    Serial.println('0');
    //Serial.print(',');
  }
   for(int i=0; i<50; i++){
  }   
}


I forgot to tell that initially I was thinking to do the treatment of each sensor on processing sketch, but concerning that I will use the final value in a graph and in an interface, I thought that it is better if the value is simplified...

liudr


xaral

#8
May 16, 2012, 12:29 am Last Edit: May 16, 2012, 12:30 am by xaral Reason: 1
Sorry. Seems that I'm almost sleeping  :smiley-sleep:
Although I think the next code is right

Code: [Select]
if(val1 == HIGH && val2 == HIGH && f < 15){
   Serial.println('1');
   //Serial.print(',');
 }
 if(val1 == LOW || val2 == LOW || f >15){
   Serial.println('0');
   //Serial.print(',');


the first condition if(val1 == HIGH && val2 == HIGH && f < 15) isn't being detected. Arduino is just printing 0s

I will get crazy at the end of this... seems simple but there's always a little problem

liudr

What do you get from these when you have no obstacles and when you do?

Code: [Select]
  Serial.print(reading);    // print the reading
  Serial.print(',');
  Serial.print(oldreading);    // print the reading
  Serial.print(',');
  int f = reading - oldreading;
  Serial.print(f);    // print the reading
  Serial.print(',');


I assume if you don't have obstacles then you get zero? Or do you have a back drop that reflects the sound back so there is a fixed distance without obstacles?

Go Up