Count once with trigger

hi...can anyone help me to make count only once per trigger.
in the code below what happens is..if i place a hand in front of sensor and keep it countinously trigger ..count just adds on ++
but instead i want count to happen only once per trigger...when if it triggers for mins still it should count 1+
this part needs a change

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

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

full code

#define sensorPin1 7
#define sensorPin2 8
#define sensorPin3 9
#define sensorPin4 10
#define relay 3

int sensorState1 = 0;
int sensorState2 = 0;
int sensorState3 = 0;
int sensorState4 = 0;
int count=0;

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

void loop()
  { int timex=1000;
  
  sensorState1 = digitalRead(sensorPin1);
  sensorState2 = digitalRead(sensorPin2);
  sensorState3 = digitalRead(sensorPin3);
  sensorState4 = digitalRead(sensorPin4);

  

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

  if(sensorState2 == LOW || sensorState3 == LOW ){
    count--; 
    Serial.println(count);
    delay(timex);
  }
  
 if (count < 0)
    {
      count = 0;
    }
   if(count<=0)
  {
    digitalWrite(relay, LOW); 
   
  }
  else if (count>0 && count<10){
    digitalWrite(relay, HIGH);

}

You need to count when the trigger event(s) become true rather than when they are true

See the StateChangeDetection example in the IDE

1 Like

is it something like this
but this didnot work as im using ir sensors to count people in and people out

  if (sensorState4 != lastsensorState4 ) { //counts people IN
    if (sensorState4 == LOW){
    count++;
    Serial.println(count);
    delay(timex);
  }}

Posting snippets that don't show how you handle lastSensorState doesn't help anyone.


lastsensorState4 = 0
sensorstate4 = digitalRead(sensorPin4)


if (sensorState4 != lastsensorState4 ) { //counts people IN
    if (sensorState4 == LOW){
    count++;
    Serial.println(count);
    delay(timex);
  }

Do you set lastsensorState4 equal to sensorState4 somewhere in the code that you have still not posted ?

Posting code that has no hope of compiling also helps no-one.

I hate it when that happens.

no but is it required to do ? as ir sensor has low and high . to trigger?

im just trying to make it look simple or else i can keep posting the whole code

It doesn't matter if it is simple or complex - without context, what you posted is meaningless.

If you don't save the current state then how will you know that it has changed when you test it next time ? Remember. you need to detect when the state becomes the value that you are interested in

sure understood thanks

have tried only for sensor 1 but its not wotking. I even tried with boolean state but it does not work. what am i missing here


#define sensorPin1 7
#define sensorPin2 8
#define sensorPin3 9
#define sensorPin4 10
#define relay 3

int sensorState1 = 0;
int sensorState2 = 0;
int sensorState3 = 0;
int sensorState4 = 0;
int count=0;
int lastsensorState1 = 0;
void setup()
{
  Serial.begin(9600);
  Serial.println("FUN Started");
  Serial.print(count);
  pinMode (sensorPin1,INPUT);
  pinMode (sensorPin2, INPUT);
  pinMode (sensorPin3,INPUT);
  pinMode (sensorPin4,INPUT);
  pinMode(relay, OUTPUT);
  
 
}

void loop()
  { int timex=1000;
  
  sensorState1 = digitalRead(sensorPin1);
  sensorState2 = digitalRead(sensorPin2);
  sensorState3 = digitalRead(sensorPin3);
  sensorState4 = digitalRead(sensorPin4);

  

  if(sensorState1 != lastsensorState1 ){   //just changed the sensor1 to check is it working
    if (sensorState1 ==LOW ){
    count++; 
    Serial.println(count);
    delay(100);
  }
  lastsensorState1 = sensorState1;
  }
  
  if(sensorState4 == LOW ){
    count++; 
    Serial.println(count);
    delay(timex);
  }

  if(sensorState2 == LOW || sensorState3 == LOW ){
    count--; 
    Serial.println(count);
    delay(timex);
  }
  
 if (count < 0)
    {
      count = 0;
    }
   if(count<=0)
  {
    digitalWrite(relay, LOW); 
   
  }
  else if (count>0 && count<10){
    digitalWrite(relay, HIGH);
//    Serial.println(1);
    
    
  }
//  else {
//    digitalWrite(relay, HIGH);
//     
//  }
}

Make it really simple and try this

#define sensorPin1 A3

int sensorState1 = 0;
int count = 0;
int lastsensorState1 = HIGH;

void setup()
{
  Serial.begin(115200);
  pinMode (sensorPin1, INPUT_PULLUP);
}

void loop()
{
  sensorState1 = digitalRead(sensorPin1);
  if (sensorState1 != lastsensorState1)   //just changed the sensor1 to check is it working
  {
    if (sensorState1 == LOW )
    {
      count++;
      Serial.println(count);
      delay(100);
    }
    lastsensorState1 = sensorState1;
  }
}

Adjust the pin number and baud rate to suit your hardware but I know that this code works to detect a change in state from HIGH to LOW

tried this..but count isnt working..their is no count showing up in serial monitor

Have you matched the baud rate of the code with that of the monitor ?

Are you sure that the input is going LOW on the pin that you are using ?

1 Like

yeah im sure...im using ir sensors and they get triggered when go low...please can you help me to fix the count issues.
this one is messing the whole project

Maybe now's the time for you to contribute to your project, and characterise how the sensors behave.

I have been building the whole project for a while...and this count problem i tried few methodes but nothing worked..thats why im asking help in community...anyway thanks

Please post a schematic of the circuit that you used when you ran the code in post #14 and your actual code if you made any changes to it, such as the pin number

A photo of a hand drawn circuit is good enough. Please post a link to the PIR sensor that you are using