So i was hired to build a prototype, Basically it reads 2 sensors, one mounted high on a door jam and one mounted lower, if both are tripped when somebody walks through it, it wont be set off, but it only the bottom one is tripped it will sound a alarm, They are Sharp IR sensors.
I have working code, and its working, but due to the face that peoples legs move when they walk, it tends to set off the alarm when someone walks through it.
I want to add a delay between the two reads, my loop that reads the sensors is as follows
(_distance1 < alertdistance1 && distance2 > alertdistance1)
i want it to take the reading of distance one, if it is less then the alert distance i want a 1 to the thrown into memory (which would be flushed back to 0 after a second) and the same to be done with the distance 2 sensor. if after half a second after the bottom sensor is tripped the top one is not, then i want the alarm to sound..
ive tried a few different methods but everything ive tried makes the alarm go off immediately after turning the device on
here is my full code
const int sensor1Pin = 0;
const int sensor2Pin = 1;
const int alertPin = 2;
const int mprime = 16667;
const int bprime = 15;
const int k = 10;
const int maxdistance1 = 200;
const int alertdistance1 = 80;
const int alertdistance2 = 80;
int sensor1Value = 0;
int distance1 = 0;
int sensor2Value = 0;
int distance2 = 0;
void setup() {
pinMode(alertPin, OUTPUT);
}
void loop() {
sensor1Value = readSensor1();
sensor2Value = readSensor2();
distance1 = (mprime / (sensor1Value + bprime)) - k;
distance2 = (mprime / (sensor2Value + bprime)) - k;
soundAlert(distance1);
}
int readSensor1() {
int _i;
int _sensor1Value = 0;
for(_i = 0; _i < 5; _i ++) {
_sensor1Value += analogRead(sensor1Pin);
delay(10);
}
_sensor1Value = _sensor1Value / 5;
return _sensor1Value;
}
int readSensor2() {
int _i;
int _sensor2Value = 0;
for(_i = 0; _i < 5; _i ++) {
_sensor2Value += analogRead(sensor2Pin);
delay(10);
}
_sensor2Value = _sensor2Value / 5;
return _sensor2Value;
}
void soundAlert(int _distance1) {
if (_distance1 < alertdistance1 && distance2 > alertdistance1)
digitalWrite(alertPin, HIGH);
}
you should make 2dimensional measurements where the first dimension is the signal and the second dimension is time.
For the latter you can use millis() or micros()
in pseudo code you could do
if one sensor triggers set an alarmclock (e.g. 100 ms)
if the second sensor triggers within 100 ms reset the alarmclock
if the alarmclock reaches zero => ALARM!
The system will have at least three states: IDLE <===> PRE-ALARM ===> ALARM ===> IDLE
I do not think you have formulated precisly enough what the triggering condition is. When you do that the code is half done. Here is a suggestion (which I have not thought thoroughly through - that is your pleasure)
starting psoition, both sensors are off.
Bottom sensor on, top sensor off. Note the time (record value of millis())
if top sensor turns on, reset the time (record value of millis())
if bottom sensor turns off, reset the timer
if the bottom sensor is on and millis() now is 2345 greater (or whatever the threshold is) than the last recorde time - beeep
6 loop to 3 until both sensors are OFF and timer is much greater than millis, in which case weøre back at 1.
I'm fairly certain this code is absolutely not what you want.
{
if (sensorTripData1 = 1)
delay(200);
sensorTripData1 == 0;
}
{
if (sensorTripData2 = 1)
delay(200);
sensorTripData2 == 0;
}
Braces don't group commands together. In fact, if you remove the braces, nothing about that code changes. Did you mean to set the variables when the if-statement was true?
Speaking of if-statements and setting variables...
if (sensorTripData2 = 1)
This is an assignment operator, not a comparison operator. This is where you use "==" to compare the variable "sensorTripData2" to the value of "1".
sensorTripData2 == 0;
This is a comparison operator. It does NOT make any change to the value of sensorTripData2. In fact, it is a meaningless statement. This is where you use "=".
@MSquare, i would love to do it that way, But i have no idea how to.. The code im using is a modified example code for use with the Sharp IR sensors. and my programming skills are not good enough. Ive been building hardware for years, but programming is new to me.
One line "translated". The rest follow the same pattern
// 2. Bottom sensor on, top sensor off. Note the time (record value of millis())
if ( Bottomvalue > tripvalue && TopSensorvlaue < tripvalue ) TimerValue = millis() ;
Well, I made a typo, so it should be: (And I have not checked if "distance2" is the correct sensor or suchlike. I'm just helping on principles and algorithms)
//if the bottom sensor is on and millis() now is 2345 greater (or whatever the threshold is) than the last recorded time - beeep
if ( millis()-TimerValue > 2345 && distance2 > tripdeadline ) AlertRoutine() ;
BTW, make sure TimerValue is declared as unsigned long.
Okay i have the code segments you provided put into my code, but before i can test it i need to figure out what the AlertRoutine(); part of it is for, From my understanding, and i could be wrong, it would call to the "digitalWrite(alertPin,HIGH);" code to set off the alarm. But how would i do that?
Msquare:
To make amends, here is line 5
//if the bottom sensor is on and millis() now is 2345 greater (or whatever the threshold is) than the last recorded time - beeep
ehr? I can not make sense of your question the question. ByTheWay, now would be good time to post the edited code to see how far it is, in particular after all improvments suggested by James C4S.
well i edited it, but now it runs the alert routine, its not waiting until the sensor is tripped before starting the timer, also i need this to reset the timer after the distance2 sensor is tripped
const int sensor1Pin = 0;
const int sensor2Pin = 1;
const int alertPin = 6;
const int sirenPin = 10;
const int mprime = 16667;
const int bprime = 15;
const int k = 10;
const int maxdistance1 = 200;
const int alertdistance1 = 80;
const int alertdistance2 = 80;
unsigned long TimerValue;
int sensor1Value = 0;
int distance1 = 0;
int sensor2Value = 0;
int distance2 = 0;
void setup() {
pinMode(alertPin, OUTPUT);
pinMode(sirenPin, OUTPUT);
}
void loop() {
sensor1Value = readSensor1();
sensor2Value = readSensor2();
distance1 = (mprime / (sensor1Value + bprime)) - k;
distance2 = (mprime / (sensor2Value + bprime)) - k;
soundAlert(distance1);
}
int readSensor1() {
int _i;
int _sensor1Value = 0;
for(_i = 0; _i < 5; _i ++) {
_sensor1Value += analogRead(sensor1Pin);
}
_sensor1Value = _sensor1Value / 5;
return _sensor1Value;
}
int readSensor2() {
int _i;
int _sensor2Value = 0;
for(_i = 0; _i < 5; _i ++) {
_sensor2Value += analogRead(sensor2Pin);
}
_sensor2Value = _sensor2Value / 5;
return _sensor2Value;
}
void soundAlert(int _distance1) {
if (_distance1 < alertdistance1 )
TimerValue = millis() ;
if ( millis()-TimerValue > 2345 && distance2 > alertdistance1 )
{
digitalWrite(alertPin, HIGH);
delay(500);
digitalWrite(alertPin, LOW);
delay(500);
digitalWrite(alertPin, HIGH);
delay(500);
digitalWrite(alertPin, LOW);
delay(500);
digitalWrite(alertPin, HIGH);
delay(500);
digitalWrite(alertPin, LOW);
delay(500);
digitalWrite(alertPin, HIGH);
delay(500);
digitalWrite(alertPin, LOW);
delay(500);
digitalWrite(alertPin, HIGH);
delay(500);
digitalWrite(alertPin, LOW);
delay(500);
digitalWrite(alertPin, HIGH);
delay(500);
digitalWrite(alertPin, LOW);
delay(500);
digitalWrite(sirenPin, HIGH);
delay(200);
digitalWrite(sirenPin, LOW);
}
}
its to detect the "height" of a passing human basically instead of sensing when the door opens, it senses the height of a passing object, using 2 Sharp IR sensors. These would be mounted on a door jamb at determined heights, so the first sensor is around 3 feet from the ground (or lower if wanted) and the top sensor would be around 5 feet. How this system works is by detecting which sensors are tripped by a passing object, taking that information, comparing the voltage values from each sensor, throwing that value into an algorithm, and throwing out a distance in centimeters, say for instance that you have a door that is 3 feet wide, that would be 91 centimeters. So you would program the unit for about 91, I would put 85 to 87 just because the sensors have some width to them. So each sensor knows that all objects should be that far away, but if an object comes in between that reading the reading drops, when that drops it sets off a buzzer.