I have a problem to find a solution for my project.
I have 2 ultrasonic sensors (Sr04) and I want to detect the direction of movement (in or out).
The sensors are installed horizontal on the wall by the front door.
If sensor 1 detect earlier then sensor 2 then I want to count + 1 and for -1 its first sensor 2...
I can't figger out how to translate this to C code.
Forget the C code for a moment, and think about the exact logic of what needs to be done. Think about how you can record the fact that a sensor has been tripped, perhaps with a flag, boolean variables say innerSensorTripped and outerSensorTripped which can be true or false.
Chart how you could use such flags to see in which direction the subject is moving.
Apart from that, have you got a sensor running correctly, measuring distance to the target?
This is the code that I already have running, this works fine, I suggest when sensor 1 change first en sensor 2 next then count + 1.
Changing sensor 2 first en sensor 2 next then count -1.
That's the way I think it should work...
#include "DHT.h"
///////////////////////////////Sensor 1////////////////////////////////////
#define echoPin1 2 // Echo Pin
#define trigPin1 3 // Trigger Pin
#define LEDPin1 19// Groen
int maximumRange1 = 100; // Maximum range needed
int minimumRange1 = 90; // Minimum range needed
long duration1, distance1; // Duration used to calculate distance
int incrementState1 = 0; //variable that will read the increment button (either HIGH or LOW)
int decrementState1 = 0; //variable that will read the decrement button (either HIGH or LOW)
int lastIncrementState1 = 0;
int lastDecrementState1 = 0;
int currentState1 = 0;
int previousState1 = 0;
///////////////////////////////Sensor 2////////////////////////////////////
#define echoPin2 11 // Echo Pin
#define trigPin2 12 // Trigger Pin
#define LEDPin2 8 // Rood
int maximumRange2 = 100; // Maximum range needed
int minimumRange2 = 90; // Minimum range needed
long duration2, distance2; // Duration used to calculate distance
int incrementState2 = 0; //variable that will read the increment button (either HIGH or LOW)
int decrementState2 = 0; //variable that will read the decrement button (either HIGH or LOW)
int lastIncrementState2 = 0;
int lastDecrementState2 = 0;
int currentState2 = 0;
int previousState2 = 0;
int counter = 0; //variable that will store the count
void setup() {
Serial.begin(9600);
Serial.println("Zwemmer teller:");
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(LEDPin1, OUTPUT); // Use LED indicator (if required)
pinMode(LEDPin2, OUTPUT); // Use LED indicator (if required)
}
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
//lcd.setCursor(0, 1);
//lcd.print(counter); //print it on serial monitor
//Serial.println(counter);
///////////////////////////////Sensor 1////////////////////////////////////
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance1 = duration1 / 58.2;
if (distance1 <= minimumRange1) {
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println("-1");
digitalWrite(LEDPin1, HIGH);
currentState1 = 1;
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance1);
digitalWrite(LEDPin1, LOW);
currentState1 = 0;
}
if (currentState1 != previousState1) {
if (currentState1 == 1) {
counter = counter + 1;
}
}
Serial.println(counter);
previousState1 = currentState1;
delay(175);
///////////////////////////////Sensor 2////////////////////////////////////
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance2 = duration2 / 58.2;
if (distance2 <= minimumRange2) {
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println("-1");
digitalWrite(LEDPin2, HIGH);
currentState2 = 1;
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance2);
digitalWrite(LEDPin2, LOW);
currentState2 = 0;
}
if (currentState2 != previousState2) {
if (currentState1 == 1 + currentState2 == 1) {
counter = counter + 1;
}
Serial.println(counter);
}
previousState2 = currentState2;
//Delay 50ms before next reading.
delay(175);
}
If sensor 1 detect earlier then sensor 2 then I want to count + 1 and for -1 its first sensor 2...
If sensor 1 detects a distance of 30 meters and sensor 2 then, milliseconds later, detects a distance of 5 centimeters, what do you want to do? Perhaps now you can see why ultrasonic sensors are not the correct kind of sensors to be using for this application.