How to make IR Obstacle motion detect 2 times to display 1 at 7 segment digit

Hey... Im new to this arduino project.
I have tried to make an IR motion sensor to display at 7 segment digit using this code below.
Now Im trying to make it detect two motion to display 1 time at 7 segment digit.
I have tried but fail multiple times.
Can anyone help me with the code. Thank you.

const int a = 11; //For displaying segment "a"
const int b = 10; //For displaying segment "b"
const int c = 9; //For displaying segment "c"
const int d = 8; //For displaying segment "d"
const int e = 7; //For displaying segment "e"
const int f = 12; //For displaying segment "f"
const int g = 13; //For displaying segment "g"

bool IRsensor = false;
const int IRsensorPin = 5;

// Variables will change:
int IRsensordetect = 0; // counter for the number of button presses
int IRsensorState = 0; // current state of the button
int lastIRsensorState = 0; // previous state of the button

void setup() {
// put your setup code here, to run once:
pinMode(a, OUTPUT); //A
pinMode(b, OUTPUT); //B
pinMode(c, OUTPUT); //C
pinMode(d, OUTPUT); //D
pinMode(e, OUTPUT); //E
pinMode(f, OUTPUT); //F
pinMode(g, OUTPUT); //G

pinMode( IRsensorPin , INPUT_PULLUP );
Serial.begin(9600);
displayDigit(IRsensordetect);
}

void loop() {

IRsensorState = digitalRead(IRsensorPin);

// compare the buttonState to its previous state
if (IRsensorState != lastIRsensorState) {
// if the state has changed, increment the counter
if (IRsensorState == LOW) {
// if the current state is HIGH then the button went from off to on:
IRsensor = true;
IRsensordetect++;
if( IRsensordetect > 9) IRsensordetect = 0 ;

Serial.println("on");

} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastIRsensorState = IRsensorState;

if( IRsensor ){
turnOff();
displayDigit(IRsensordetect);
}
}

void displayDigit(int digit)
{
//Conditions for displaying segment a
if(digit!=1 && digit != 4)
digitalWrite(a,HIGH);

//Conditions for displaying segment b
if(digit != 5 && digit != 6)
digitalWrite(b,HIGH);

//Conditions for displaying segment c
if(digit !=2)
digitalWrite(c,HIGH);

//Conditions for displaying segment d
if(digit != 1 && digit !=4 && digit !=7)
digitalWrite(d,HIGH);

//Conditions for displaying segment e
if(digit == 2 || digit ==6 || digit == 8 || digit==0)
digitalWrite(e,HIGH);

//Conditions for displaying segment f
if(digit != 1 && digit !=2 && digit!=3 && digit !=7)
digitalWrite(f,HIGH);

//Conditions for displaying segment g
if (digit!=0 && digit!=1 && digit !=7)
digitalWrite(g,HIGH);

}
void turnOff()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

Please provide a detailed description of what you mean by "fail multiple times".

I cant manage to make a correct code for the IR motion detector (Flying fish) to read 2 motion as 1 shown at 7 segment digit.
For example , starting display at no. 0, 1 detection movement will display still number 0, 2 times detection = will display oo 1 digit, 3 movement also display no 1, 4 times detection will display no 2 at 7 digit segment and so on until it reach no 9.

My advice is to test each individual part of your project to make sure each one is working:

  • Write a sketch that cycles through the numbers 0-9 displayed on the 7 segment.
  • Write a sketch that uses Serial.println() to print the motion detection count to Serial Monitor.

Once the two components are working individually, you can combine them.

If I understand what you're asking the basic change you need is a separate displayCount variable. Then after the IRsensorDetect++ set displayCount = IRsensorDetect / 2 and use that in your displayDigit() call. Then you'll need to set both variables to 0 when displayCount > 9.

Steve

pert:
My advice is to test each individual part of your project to make sure each one is working:

  • Write a sketch that cycles through the numbers 0-9 displayed on the 7 segment.
  • Write a sketch that uses Serial.println() to print the motion detection count to Serial Monitor.

Once the two components are working individually, you can combine them.

Thanks for the suggestion. I will try it. Thank you.

slipstick:
If I understand what you're asking the basic change you need is a separate displayCount variable. Then after the IRsensorDetect++ set displayCount = IRsensorDetect / 2 and use that in your displayDigit() call. Then you'll need to set both variables to 0 when displayCount > 9.

Steve

Thank you so much Steve. You have help me with the code that I should used. I have manage to make it work successfully by changing it to "displayDigit(IRsensordetect / 2);"

Thanks again.