Multiple Ultrasonic Sensors

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
vkapadia
Posts: 13
Joined: 09 Jun 2017 00:38

Multiple Ultrasonic Sensors

Post by vkapadia » 13 Jun 2017 21:43

Hi, I'm new to Z-Wave, Z-Uno, and Arduino in general. I'm still waiting for Z-Uno to be available in the US, but once it does, I want to make a sensor that knows when me or my wife is home. After thinking through a bunch of ways to do this, none of which are reliable enough and automatic (my wife wouldn't want any system that requires her to do anything special like tap a tag or push a button), I thought of checking if our cars are in the garage. I think a false positive/negative would be rare enough (we rarely leave the house with both cars in it, or have both cars gone while we are still home).

To do so, someone suggested using a Z-Uno with an ultrasonic sensor, since there is no commercially available distance sensor yet. I want to combine two sensors though, so that I only need to purchase one Z-Uno board (this hobby could get expensive lol...) so I was wondering if this would work. I was pointed to http://z-uno.z-wave.me/examples/hc-sr04 ... ce-sensor/ as an example. So what I would do is mirror the setup with a second ultrasonic sensor, wired to pins 7 and 8.

Here is the code I modified from that link, would this work? I removed all the comments from the original code so the comments here are notes and questions that I have.

Code: Select all

ZUNO_SETUP_CHANNELS(
    ZUNO_SENSOR_MULTILEVEL(
        ZUNO_SENSOR_MULTILEVEL_TYPE_GENERAL_PURPOSE_VALUE,
        0, // not sure what i'd use for this value, i think 0 should work
        SENSOR_MULTILEVEL_SIZE_TWO_BYTES, // don't think i need this much, one byte should work, right?
        0, // don't need any decimal precision
        getter
    )
);

int compare_distance = 3000; // this is the variable I'll change to compare distance to
int overlap_distance = 100; // this value is used as an overlap zone between car detected and not detected, used to prevent the edge case

int readPinMe = 9;
int triggerPinMe = 10;
byte homeStateMe = 0;

int readPinWife = 7;
int triggerPinWife = 8;
byte homeStateWife = 0;

void setup() {
  Serial.begin();
  pinMode(readPinMe, INPUT);
  pinMode(triggerPinMe, OUTPUT);
  digitalWrite(triggerPinMe, LOW);
  pinMode(readPinWife, INPUT);
  pinMode(triggerPinWife, OUTPUT);
  digitalWrite(triggerPinWife, LOW);
}

void loop() {
  int tmp;
  
  // Me

  digitalWrite(triggerPinMe, LOW);
  delayMicroseconds(10);
  digitalWrite(triggerPinMe, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPinMe, LOW);

  tmp = pulseIn(readPin, HIGH, 100000);
  if (tmp != 0) {    
    if (tmp < (compare_distance - overlap_distance) && homeStateMe == 0) {
      homeStateMe = 1;
    } else if (tmp > (compare_distance + overlap_distance) && homeStateMe == 1) {
      homeStateMe = 0;
    }    
  }

  // Wife

  digitalWrite(triggerPinWife, LOW);
  delayMicroseconds(10);
  digitalWrite(triggerPinWife, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPinWife, LOW);

  tmp = pulseIn(readPin, HIGH, 100000);
  if (tmp != 0) {    
    if (tmp < (compare_distance - overlap_distance) && homeStateWife == 0) {
      homeStateWife = 2;
    } else if (tmp > (compare_distance + overlap_distance) && homeStateWife == 2) {
      homeStateWife = 0;
    }    
  }
  
  zunoSendReport(1);
  delay(1000);
}

word getter() {
  return homeStateMe + homeStateWife;
  // this makes 4 states:
  //   0 = nobody home
  //   1 = me home
  //   2 = wife home
  //   3 = both home
}
Any thoughts?

Thanks,
-Vinay

User avatar
PoltoS
Posts: 3416
Joined: 26 Jan 2011 19:36

Re: Multiple Ultrasonic Sensors

Post by PoltoS » 13 Jun 2017 22:23

Code: Select all

0, // not sure what i'd use for this value, i think 0 should work
for General Purpose that you have chosen 0 is scaleless number, 1 is %

Code: Select all

        SENSOR_MULTILEVEL_SIZE_TWO_BYTES, // don't think i need this much, one byte should work, right?
        0, // don't need any decimal precision
1 byte is enough if presision is 0. This wil give 0-255 values

Looks ok

vkapadia
Posts: 13
Joined: 09 Jun 2017 00:38

Re: Multiple Ultrasonic Sensors

Post by vkapadia » 13 Jun 2017 22:30

Ok actually, there seems to be a better way (I'm just reading through the getting started guide, so just discovered this way).

How's this?

Code: Select all

ZUNO_SETUP_CHANNELS(
    ZUNO_SENSOR_BINARY_GENERAL_PURPOSE(getterMe),
    ZUNO_SENSOR_BINARY_GENERAL_PURPOSE(getterWife)
);

int compare_distance = 3000; // this is the variable I'll change to compare distance to
int overlap_distance = 100; // this value is used as an overlap zone between car detected and not detected, used to prevent the edge case

int readPinMe = 9;
int triggerPinMe = 10;
boolean homeStateMe = false;

int readPinWife = 7;
int triggerPinWife = 8;
boolean homeStateWife = false;

void setup() {
  Serial.begin();
  pinMode(readPinMe, INPUT);
  pinMode(triggerPinMe, OUTPUT);
  digitalWrite(triggerPinMe, LOW);
  pinMode(readPinWife, INPUT);
  pinMode(triggerPinWife, OUTPUT);
  digitalWrite(triggerPinWife, LOW);
}

void triggerPulse(int pin) {
  digitalWrite(pin, LOW);
  delayMicroseconds(10);
  digitalWrite(pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pin, LOW);
}

void loop() {  
  // Me
  triggerPulse(triggerPinMe);

  int tmp = pulseIn(readPin, HIGH, 100000);
  if (tmp != 0) {    
    if (tmp < (compare_distance - overlap_distance) && !homeStateMe) {
      homeStateMe = true;
    } else if (tmp > (compare_distance + overlap_distance) && homeStateMe) {
      homeStateMe = false;
    }    
  }
  
  zunoSendReport(1);

  // Wife
  triggerPulse(triggerPinWife);

  tmp = pulseIn(readPin, HIGH, 100000);
  if (tmp != 0) {    
    if (tmp < (compare_distance - overlap_distance) && !homeStateWife) {
      homeStateWife = true;
    } else if (tmp > (compare_distance + overlap_distance) && homeStateWife) {
      homeStateWife = false;
    }    
  }
  
  zunoSendReport(2);
  
  delay(1000);
}

word getterMe() {
  return homeStateMe;
}

word getterWife() {
  return homeStateWife;
}
(Edited to add two calls to zunoSendReport, one for each channel.)

User avatar
PoltoS
Posts: 3416
Joined: 26 Jan 2011 19:36

Re: Multiple Ultrasonic Sensors

Post by PoltoS » 13 Jun 2017 23:09

A much better one indeed!

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest