This is the first time I have tried to build anything without guidance. I am trying to count objects going "in" or "out" depending on the order they cross two Ultrasonic distance sensors.
But it does not work, can anyone tell me what I am doing wrong?
// Ultrasonic - Version: Latest
#include <Ultrasonic.h>
/*
*/
Ultrasonic ultrasonic1(10, 11);
Ultrasonic ultrasonic2(12, 13);
int calibratedHeightOffset = 30;
int calibratedHeightToRead;
int occupants;
boolean u1First;
boolean u2First;
String directionOfMovement;
void setup() {
Serial.begin(9600);
calibratedHeightToRead = calibrateHeight();
occupants = 0;
Serial.print("Number of occupants in room from start is: ");
Serial.print(occupants);
}
void loop() {
occupancyDetector();
}
int calibrateHeight(){
int u1 = u1Read();
delay(37);
int u2 = u2Read();
Serial.println((u1+u2)/2-calibratedHeightOffset);
return (u1+u2)/2-calibratedHeightOffset;
}
int u1Read(){
int a = ultrasonic1.distanceRead();
delay(35);
int b = ultrasonic1.distanceRead();
delay(35);
int c = ultrasonic1.distanceRead();
delay(35);
return averageReading(a,b,c);
}
int u2Read(){
int a = ultrasonic2.distanceRead();
delay(35);
int b = ultrasonic2.distanceRead();
delay(35);
int c = ultrasonic2.distanceRead();
delay(35);
return averageReading(a,b,c);
}
boolean u1IsTriggered(){
return (u1Read() < calibratedHeightToRead)?true:false;
}
boolean u2IsTriggered(){
return (u2Read() < calibratedHeightToRead)?true:false;
}
int averageReading(int a, int b, int c){
int firstMax = (a >= b)?a:b;
int secondMax = (b >= c)?b:c;
return (firstMax < secondMax)?firstMax:secondMax;
}
String occupancyDetector(){
String numberOfOccupants = "Number of occupants in room is: ";
if(u1IsTriggered() && u2IsTriggered()){
if(u1First){
directionOfMovement = "in";
}
else if(u2First){
directionOfMovement = "out";
}
}
else if(u1IsTriggered() || u2IsTriggered()){
if(u1IsTriggered()){
if(directionOfMovement.equals("out")){
directionOfMovement = "";
u2First = false;
occupants--;
return numberOfOccupants + occupants;
}
else{
u1First = true;
}
}
else if(u2IsTriggered()){
if(directionOfMovement.equals("in")){
directionOfMovement = "";
u1First = false;
occupants++;
return numberOfOccupants + occupants;
}
else{
u2First = true;
}
}
}
else{
//Nothing triggeded (Onödig ?)
}
}
would be like something is leaving the room "out" and also decrease occupants by 1
I have tested the values when adding code/functions and all seamed to work, but when I tried to add the last big function "String occupancyDetector()" my thought was that it would print the value of occupants to the serial monitor and have it change when I move my hand through the sensors. But I get no readings at all.
Your theory/method can work if there is never the possibility of multiple persons entering, exiting, or entering and exiting at the same time.
If multiples are likely, you would probably have to have some way to uniquely identify each person with 2 sensors to know which way they were going. And also allow for someone tripping one sensor and turning around. There are a bunch of scenarios that would be difficult to account for which will make your goal difficult to obtain.
When I have issues, I'll usually fall back on the Serial.prinln("A"); trick. Placing one with a different letter at each step in the code where I "think" the problem is. This can give you a good idea where your train is coming off the rail. And maybe you've already done that.
burton666:
I get the part from setup printed to serial monitor, but I do not get any updates/reaction by moving my hand in front of the sensors.
That's almost certainly (bit of sarcasm there) due to the fact that the only occurrences of Serial.print() in the sketch are in setup() twice, and calibrateHeight() once. So you're not actually asking it to print anything.
burton666:
I have two ulstrasonic sensors which is placed close together. My thought was that if:
That may be a big problem in itself. Ultrasound sensors have a cone of about 30°, so they will have an enormous overlap in detection area, possibly to the extent that you can't reliably detect direction. The inherent slowness of making a measurement may also pose a problem - with the person crossing the non-overlapping part before detection.
Break beam sensors (using IR) may be a much better solution for this.
Ok, missed the obvious part of adding the Serial.println to the loop. But after fixing this I just get a lot of "junk characters" spamming the console.