Pir sensor to count how many times it went off.

Ok my project is using a PIR sensor in which it is important to count the times it goes off by the motion/heat of something. I tried to use a for loop for counting how many times it went off and it never worked. I even just put times++; to see if it would change but I could never get it count it just displays "times"? My question is: Is there a way to post the total at the end while it keeps track of the amount of times it goes off? A for loop to count it? Like the code records how many times it goes off and when the board is turned off the it displays "6 detections" in the serial monitor? Thanks in advance, and sorry for the bad question I am new to this type of programming.

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int times = 0;

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input

  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
   digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(100);
  digitalWrite(ledPin, HIGH);
  delay(1000);
  times++;  //wanted to use this to keep track of how many it goes off.
  Serial.println(times);

The PIR sensor can not count how many times it went off. YOU must do the counting.

The code you posted won't even compile.

it just displays "times"?

Don't you know whether it does? There is nothing in that code that would display the string "times", so I have a hard time believing you.

That the switch IS high is not what you want to count. You need to look at the state change detection example.

when the board is turned off the it displays "6 detections" in the serial monitor?

That would be a good trick. You could just display the number each time the PIR is triggered or have a button that displayed the total when it was pressed. Either way you do not want to add to the total [u]while[/u] the PIR is triggered but [u]when[/u] the PIR is triggered. Only add 1 to the count when its state changes from off to on.

Oh, sorry. I meant that I did it not the sensor. Also, I posted the code part where the question was about. But, yes this posts "times" whenever the sensor detections something rather than a number. Thanks again. Here is the entire code.

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int times = 0;

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input

  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
   digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);
  digitalWrite(ledPin, HIGH);
  delay(1000);
  times++;
  Serial.println(times);
  
  

    
    if (pirState == LOW) {
     pirState = HIGH;
    }
  } else {
      digitalWrite(ledPin, LOW); // turn LED OFF
      
      delay(500);    
      if (pirState == HIGH){
    
      pirState = LOW;
    }
  }
}

I'm not going to try to read code that runs all over the page.

There is a Tools + Auto Format function. You ARE expect to use that BEFORE posting code here.

sorry again!

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int times = 0;

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input

  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
    digitalWrite(ledPin, HIGH);
    delay(1000);
    times++;
    Serial.println(times);




    if (pirState == LOW) {
      pirState = HIGH;
    }
  } 
  else {
    digitalWrite(ledPin, LOW); // turn LED OFF

    delay(500);    
    if (pirState == HIGH){

      pirState = LOW;
    }
  }
}
    if (pirState == LOW) {
      pirState = HIGH;
    }

You can’t set the state of an input sensor.

If you are reading from a PIR sensor, the value that you read should be stored in pirState, and you should be reading from pirPin.

You haven’t looked at the state change detection example, have you?