Need help for this funtion

i made a bidirectional people count...and set count =0 and when sensors are triggered its does count++ and count--
i dont want the count to go less than zero...its goes like -1,-2..

if (count < 0)
{
  count = 0;
}

But if your people detector is working correctly then count will neve be less than zero, will it :grinning:

yeah its working correctly...
here is the code

#define sensorPin1 7
#define sensorPin2 8
#define relay 3

int sensorState1 = 0;
int sensorState2 = 0;
int count=0;

void setup()
{
  Serial.begin(9600);
  Serial.println("FUN Started");
  Serial.print(count);
  pinMode (sensorPin1,INPUT);
  pinMode (sensorPin2, INPUT);
  pinMode(relay, OUTPUT);
  
 
}

void loop()
  { int timex=2000;
  
  sensorState1 = digitalRead(sensorPin1);
  sensorState2 = digitalRead(sensorPin2);

  if(sensorState1 == LOW){
    count++; 
    Serial.println(count);
    delay(timex);
  }

  if(sensorState2 == LOW){
    count--; 
    Serial.println(count);
    delay(timex);
  }
  

   if(count<=0)
  {
    digitalWrite(relay, LOW); 
   
  }
  else if (count>0 && count<10){
    digitalWrite(relay, HIGH);
//    
    
    
  }
  else {
    digitalWrite(relay, HIGH);
     
  }
}```

Then count will never be less than zero unless someone lingers in sensor range for more than 2 seconds

You should be detecting when a sensor becomes LOW rather than when it is LOW

1 Like

You should be detecting when a sensor becomes LOW rather than when it is LOW
can you tell me how to code this way..sorry im new to arduino and trying my best to code..

can you help me how to do that way...?

Take a look at the StateChangeDetection example in the IDE

Basically you read the input and compare it what it was the previous time you read it. If it has changed then the input has either become HIGH or become LOW and its current state will tell you which it is

1 Like

thank you will look into it.

hello... dude I tried to code
check this ...its not working..counts are not even getting triggered with this code

#define sensorPin2 8
#define relay 3

int sensorState1 = 0;
int sensorState2 = 0;
int lastsensorState1 = 0;
int lastsensorState2 = 0;
int count=0;

void setup()
{
  Serial.begin(9600);
  Serial.println("FUN Started");
  Serial.print(count);
  pinMode (sensorPin1,INPUT);
  pinMode (sensorPin2, INPUT);
  pinMode(relay, OUTPUT);
  
 
}

void loop()
  { int timex=1000;
  
  sensorState1 = digitalRead(sensorPin1);
  sensorState2 = digitalRead(sensorPin2);

   if (count < 0)
    {
      count = 0;
    }

  if (sensorState1 != lastsensorState1){
  if(sensorState1 == LOW){
    count++; 
    Serial.println(count);
    delay(timex);
  }}
 if (sensorState2 != lastsensorState2){
  if(sensorState2 == LOW){
    count--; 
    Serial.println(count);
    delay(timex);
  }}
  

   if(count<=0)
  {
    digitalWrite(relay, LOW); 
   
  }
  else if (count>0 && count<10){
    digitalWrite(relay, HIGH);
//    Serial.println(1);
    
    
  }
  else {
    digitalWrite(relay, HIGH);
     
  }
}```

**but if i change last state to 1 then it works but it triggers every 2 secs..**
**code as follows** 

```#define sensorPin1 7
#define sensorPin2 8
#define relay 3

int sensorState1 = 0;
int sensorState2 = 0;
int lastsensorState1 = 1;
int lastsensorState2 = 1;
int count=0;

void setup()
{
  Serial.begin(9600);
  Serial.println("FUN Started");
  Serial.print(count);
  pinMode (sensorPin1,INPUT);
  pinMode (sensorPin2, INPUT);
  pinMode(relay, OUTPUT);
  
 
}

void loop()
  { int timex=1000;
  
  sensorState1 = digitalRead(sensorPin1);
  sensorState2 = digitalRead(sensorPin2);

   if (count < 0)
    {
      count = 0;
    }

  if (sensorState1 != lastsensorState1){
  if(sensorState1 == LOW){
    count++; 
    Serial.println(count);
    delay(timex);
  }}
 if (sensorState2 != lastsensorState2){
  if(sensorState2 == LOW){
    count--; 
    Serial.println(count);
    delay(timex);
  }}
  

   if(count<=0)
  {
    digitalWrite(relay, LOW); 
   
  }
  else if (count>0 && count<10){
    digitalWrite(relay, HIGH);
//    Serial.println(1);
    
    
  }
  else {
    digitalWrite(relay, HIGH);
     
  }
}```

You are not updating the last sensor states with the current sensor states before reading the states again so that you can determine whether they have changed

i did use that funtion ...if i use that..it just triggers once and never again triggers

Where in your code do you save the current states to the variable states variables ready to check whether they have changed when you read them again ?