Coding problem when using IF statement with Image attached

I had face the problem when using IF statement in coding. I want to build a people counter using three ultrasonic sensors.

I'll explain the scenario first for better understanding of what answer I'm searching for:


Example 0
When sensorA sensed a person, it will become TRUE. It will keep TRUE until the person leave the sensor.
When the person leave the sensorA, it will increase the value of sensorA by 1, and become FALSE.


Example 1
[1, 1, 1] + [true, false, false]+[peopleCounted+1]
So when a person pass by the entrance normally, it will activate all the sensors and increase the value of each sensor. The peopleCounted will increase by 1 too.


Example 2
[1, 1, 1] + [true, true, false]
When one person is going in, but is blocked at the middle, no peopleCounted is added, but still remain [1, 1, 1], bool = [true, true, false].
If the person at sensor A move away, and another person is going in, peopleCounted+1, SensorValue = [2, 1, 1], bool = [true, false, false].


Example 3
[1, 1, 2]
If personI was decide to go in, but turn back in the middle, while personII stand still, no peopleCounted is added.

From the scenarios above, I think most people can get what I'm coding for... Its just a simulation of how people using the entrance. :sweat_smile:

Here's my code:

  if (sensorValue1 >= 1 && sensed1 == true)
  { 
    if (sensorValue2 >= 1 && sensed2 == true && (sensed1 == true || false))
    {
      if (sensorValue3 >= 1 && (sensed2 != true && sensed3 == true))
      {
        peopleCounted++;
        sensorValue1--;
        sensorValue2--;
      }
      else if (sensorValue3 >= 1 && (sensed2 == true && sensed3 == true))
      {
        if (sensed2 == false && sensorValue3 >= 2 && sensed3 == true )
        {
          if (sensed1 != true)
          {
            peopleCounted++;
            sensorValue1--;
            sensorValue2--;
          }
        }
      }
    }    
  }
  
  if (sensorValue3 >= 1 && sensed3 == true)
  { 
    if (sensorValue2 >= 1 && sensed2 == true)
    { 
      if (sensorValue1 >= 1 && (sensed2 != true && sensed1 == true))
      {
        peopleCounted--;
        sensorValue3--;
        sensorValue2--;
      }
      else if (sensorValue1 >= 1 && (sensed1 == true && sensed2 == true))
      {
        if (sensed2 == false && sensorValue1 >= 2 && sensed1 == true)
        {
          if (sensed3 != true)
          {
            peopleCounted--;
            sensorValue3--;
            sensorValue2--;
          }
        }
      }
    }
  }

My final approach is to increase/decrease the value of peopleCounted. But it doesn't works as what I wish to. The boolean and sensorValue is working well.

Am I using the IF statement in the wrong way, or having mistake in coding, or is there any other approach available?

*Edited: By the way I had insert the image but it doens't show in this topic...

The first thing to explain is what you are actually getting in sensor information that tells the program "a person" is sensed. And is that 100%, always working.

Paul

Hello winterpie,
I tried to understand your examples but they hurt my head, sorry. No matter, here are some comments about your code:

  if (sensorValue1 >= 1 && sensed1 == true)

You don't need to explicitly state == true, you can miss off the == true and it still works, so these two do the same thing:

  if (sensorValue1 >= 1 && sensed1 == true)
  if (sensorValue1 && sensed1)

In this:

if (sensorValue2 >= 1 && sensed2 == true && (sensed1 == true || false))

What do you think

sensed1 == true || false

is doing?
If you mean if sensed1 is true or if sensed 1 is false then, first that's pointless as it is always either true or false, there is nothing else it can be. Second that won't work like that as the false does not apply to anything, you need to put

sensed1 == true || sensed1 == false

Apart from that posting a code snippet as you have done is not helpful as we can't see the context of the rest of the code. Also, without a schematic we can't see how it's supposed to work.

Thanks PerryBebbington, but I need to check the status of 3 sensors everytime to check the movement of the people.

I had put some description inside my code and rearrange some arrangement.

You can view the sensorValue as how many time it sensed a person and boolean as where is the person standing at the moment.

The code is too long, so I seperate it into 2 parts.

Part1

#include <loopTimer.h>

#define BAUD_RATE 115200

const int trigPin1 = 2;
const int echoPin1 = 3;
const int trigPin2 = 4;
const int echoPin2 = 5;
const int trigPin3 = 8;
const int echoPin3 = 9;

millisDelay sensorDelay1;
millisDelay sensorDelay2;
millisDelay sensorDelay3;
millisDelay oriDelay; //use to get the height from the ground to sensor
millisDelay timer1; //timer to clear sensorValue1 if it is not been used
millisDelay timer2; //timer to clear sensorValue2 if it is not been used
millisDelay timer3; //timer to clear sensorValue3 if it is not been used
int timerValue = 5000; //value for timer1, timer2 timer3
int sensedDelay = 250; 

int trigDelay = 10;
int echoDelay = 25;
float pulseTime1, pulseTime2, pulseTime3; 
float pulseDistance1, pulseDistance2, pulseDistance3; 
float pD1, pD2, pD3; //another pulseDistance, used to count people
float oriHeight1, oriHeight2, oriHeight3;
float sensedHeight1, sensedHeight2, sensedHeight3;
float humanHeight = 145; //the person that higher than 145 will be counted.
bool sensed1 = false; //people sensed at sensor 1
bool sensed2 = false; //people sensed at sensor 2
bool sensed3 = false; //people sensed at sensor 3
int sensorValue1, sensorValue2, sensorValue3; 
int peopleCounted;

bool startUpDone = false;

millisDelay printerCounter;

//________________________________________________________________

void startUp()  //use to get oriHeight1, oriHeight2, oriHeight3
{   
  long duration1, distance1;
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(trigDelay);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(trigDelay);
  digitalWrite(trigPin1, LOW);
  pulseTime1 = pulseIn(echoPin1, HIGH);
  delay(echoDelay);
  pulseDistance1 = (pulseTime1 * 0.0343) / (2.);
  
  long duration2, distance2;
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(trigDelay);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(trigDelay);
  digitalWrite(trigPin2, LOW);
  pulseTime2 = pulseIn(echoPin2, HIGH);
  delay(echoDelay);
  pulseDistance2 = (pulseTime2 * 0.0343) / (2.);
  
  long duration3, distance3;
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(trigDelay);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(trigDelay);
  digitalWrite(trigPin3, LOW);
  pulseTime3 = pulseIn(echoPin3, HIGH);
  delay(echoDelay);
  pulseDistance3 = (pulseTime3 * 0.0343) / (2.);

   if(oriDelay.justFinished())
   {
    oriHeight1 = pulseDistance1;
    oriHeight2 = pulseDistance2;
    oriHeight3 = pulseDistance3;

    startUpDone = true;
   }
   
}

//________________________________________________________________

Part2

void sensorS()
{
  long duration1, distance1;
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(trigDelay);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(trigDelay);
  digitalWrite(trigPin1, LOW);
  pulseTime1 = pulseIn(echoPin1, HIGH);
  delay(echoDelay);
  pD1 = (pulseTime1 * 0.0343) / (2.);
  sensedHeight1 = oriHeight1 - pD1;
  
  long duration2, distance2;
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(trigDelay);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(trigDelay);
  digitalWrite(trigPin2, LOW);
  pulseTime2 = pulseIn(echoPin2, HIGH);
  delay(echoDelay);
  pD2 = (pulseTime2 * 0.0343) / (2.);
  sensedHeight2 = oriHeight2 - pD2;
  
  long duration3, distance3;
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(trigDelay);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(trigDelay);
  digitalWrite(trigPin3, LOW);
  pulseTime3 = pulseIn(echoPin3, HIGH);
  delay(echoDelay);
  pD3 = (pulseTime3 * 0.0343) / (2.);
  sensedHeight3 = oriHeight3 - pD3;

  if(sensedHeight1 >= humanHeight)  //people is standing under the sensor, or is passing by
  {
    sensed1 = true; //will turn into false if no being used or after used or the person is passby sensor1 
  }
  if(sensedHeight2 >= humanHeight)
  {
    sensed2 = true;
  }
  if(sensedHeight3 >= humanHeight)
  {
    sensed3 = true;
  }

            
  if(sensed1 == true && sensedHeight1 < humanHeight)  //if the person is sensed, then go away
  {
    sensorValue1++;
    timer1.start(timerValue); //to clear sensorValue if not being used
    delay(sensedDelay);
    sensed1 = false;
  }
  if(sensed2 == true && sensedHeight2 < humanHeight)
  {
    sensorValue2++;
    delay(sensedDelay);
    timer2.start(timerValue);  
    sensed2 = false;
  }
  if(sensed3 == true && sensedHeight3 < humanHeight)
  {
    sensorValue3++;
    timer3.start(timerValue);
    delay(sensedDelay);
    sensed3 = false;
  }


  if (timer1.justFinished() && sensed1 == false && sensorValue2 == 0)  //to clear sensorValue
  {
    sensorValue1 = 0;          
  }
  if (timer2.justFinished() && sensed2 == false) 
  //the most important sensorValue to indicate whether the person is actually going in, or turn back in the middle, or going out
  {
    sensorValue2 = 0; 
  }
  if (timer3.justFinished() && sensed3 == false && sensorValue2 == 0)
  {
    sensorValue3 = 0; 
  }

    //THE GOING IN PROCESS//
    if (sensorValue1 >= 1 && sensed1 == true)  
    //if the person1 is going in, sensorValue is = [1,0,0], boolean = [true, false, false]
    { 
    if (sensorValue2 >= 1 && sensed2 == true && (sensed1 == true || sensed1 == false)) 
    //then the person1 keep going, maybe there is another person2 going in too, or maybe not, sensorValue is = [1,1,0] or [2,1,0] , boolean = [false, true, false] or [true, true, false]
    {
      if (sensorValue3 >= 1 && (sensed2 != true && sensed3 == true)) 
      //if the person1 finally passed sensor1, sensor2, then reached sensor3, sensorValue is = [1,1,1] or [2,1,1] or [2,2,1], boolean = [false, false, true] or [true, false, true] or [true, true, true]
      {
        peopleCounted++;
        sensorValue1--;
        sensorValue2--;
      }
      else if (sensorValue3 >= 1 && (sensed2 == true && sensed3 == true)) 
      //if the person1 at sensor2 is being block by another person at sensor3, so sensorValue is also = [1,1,1], boolean = [false, true, true]
      {
        if (sensed2 == false && sensorValue3 >= 2 && sensed3 == true ) 
        //if another person let person1 go in, then sensorValue is = [1,1,2], boolean = [false, false, true]
        {
          if (sensed1 != true)
          //to ensure the person is actual going in, not turn back in the middle, if turn back sensorValue = [2,1,1] or [2,1,0], boolean = [true, false, true] or [true, false, false]
          {
            peopleCounted++;
            sensorValue1--;
            sensorValue2--;
          }
        }
      }
    }    
  }

  //THE GOING OUT PROCESS//  it's actual the reverse mode of GOING IN PROCESS
  if (sensorValue3 >= 1 && sensed3 == true)
  //if personA is going out from sensor3, sensorValue is = [0,0,1], boolean = [false, false, true]
  { 
    if (sensorValue2 >= 1 && sensed2 == true && (sensed3 == true || sensed3 == false))
    //and personA reached sensor2, maybe there is another person following, or maybe not, sensorValue is = [0,1,1] or [0,1,2], boolean = [false, true, false] or [false, true, true]
    { 
      if (sensorValue1 >= 1 && (sensed2 != true && sensed1 == true))
      //finally the personA going out sucessfully and reached sensor1, sensorValue is = [1,1,1] or [1,1,2] or [1,2,2] , boolean = [true, false, false] or [true, false, true] or [true, true, true]
      {
        peopleCounted--;
        sensorValue3--;
        sensorValue2--;
      }
      else if (sensorValue1 >= 1 && (sensed1 == true && sensed2 == true))
      //maybe the personA at sensor2 is being block by another personB at sensor1 when going out, so sensorValue is also = [1,1,1] , boolean = [true, true, false]
      {
        if (sensed2 == false && sensorValue1 >= 2 && sensed1 == true)
        //then if personB leave and let personA going out, then sensorValue is = [2,1,1], boolean = [true, false, false]
        {
          if (sensed3 != true)
          //to ensure the personA is actual going out, not turn back in the middle, if turn back sensorValue = [1,1,2] or [0,1,2] boolean = [true, false, true] or [false, false, true]
          {
            peopleCounted--;
            sensorValue3--;
            sensorValue2--;
          }
        }
      }
    }
  }
}

//________________________________________________________________

void setup() 
{
  Serial.begin(BAUD_RATE);
  for (int i = 10; i > 0; i--)
  {
    Serial.print(i);
    Serial.print(" ");
    Serial.println(" ");
    delay(500);
  }
  pinMode (trigPin1, OUTPUT);
  pinMode (echoPin1, INPUT);
  pinMode (trigPin2, OUTPUT);
  pinMode (echoPin2, INPUT);
  pinMode (trigPin3, OUTPUT);
  pinMode (echoPin3, INPUT);
  
  oriDelay.start(10000); //use 10 sec to measure the oriHeight
  printerCounter.start(2000); //repeat every 2 second to check status on Serial Monitor
}

//________________________________________________________________

void valuePrinter()
{
  if(printerCounter.justFinished())
  {
    printerCounter.repeat();
    Serial.print("OH1: ");Serial.println(oriHeight1);
    Serial.print("OH2: ");Serial.println(oriHeight2);
    Serial.print("OH3: ");Serial.println(oriHeight3);
    Serial.print("PD1: "); Serial.println(pD1);
    Serial.print("PD2: "); Serial.println(pD2);
    Serial.print("PD3: "); Serial.println(pD3);
    Serial.print("SH1: "); Serial.println(sensedHeight1);
    Serial.print("SH2: "); Serial.println(sensedHeight2);
    Serial.print("SH3: "); Serial.println(sensedHeight3);
    Serial.print("SD1: "); Serial.println(sensed1);
    Serial.print("SD2: "); Serial.println(sensed2);
    Serial.print("SD3: "); Serial.println(sensed3);
    Serial.print("SV1: "); Serial.println(sensorValue1);
    Serial.print("SV2: "); Serial.println(sensorValue2);
    Serial.print("SV3: "); Serial.println(sensorValue3);
    Serial.print("PC: "); Serial.println(peopleCounted);
  }
}

//________________________________________________________________

void loop() 
{
  if (startUpDone != true)
  {
    startUp();
  }
  else 
  {
    sensorS();
  }
  valuePrinter(); //use to check status on Serial Monitor
}

Thanks for posting all your code. I confess this one is too complicated for me to get my head around, so I am sorry I cannot help any more. Hopefully someone else will be able offer some help.

Good luck with your project.

Please note that sensorValue1 >= 1 && sensed1 == true is not the same as sensorValue1 && sensed1

AWOL,
If you are right then please explain because I'm missing something. I'm probably going to curse at myself when you tell me but...

sensorValue1 could be less than zero. (It is a signed value)

TheMemberFormerlyKnownAsAWOL:
sensorValue1 could be less than zero. (It is a signed value)

I knew I'd feel daft when you pointed it out!
++Karma; // Thank you :slight_smile: