Limit number of motion sensor readings and println's

Hello. How to limit number of println's to 1 per second as I get thousands of reading in short period of time?

//pir 
int pirPin = 23;                 // PIR Out pin 
int pirStat = 0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
   //pir
  pirStat = digitalRead(pirPin);
    if (pirStat == HIGH) {            // if motion detected
    Serial.println("Hey I got you!!!");

}

and then I get over 9000 Serial.println's

17:57:28.282 -> Hey I got you!!!


over 9000 lines on the same Serial.println


17:58:01.473 -> Hey I got you!!!

Take a look at the blink without delay example in the IDE

1 Like

How often do you want to see the line printed?

If you just want one print per activation, look at the state change detection method.

2 Likes

I'm going to venture to suggest you may have asked the wrong question, and that although the answer in post #2 answers that question, it's not the solution to your problem.

I wonder if your question should have been "How can I restrict my prints to when there is new motion detected". The answer to that one is given here at adafruit in their pir tutorial.

(Since I started typing, groundFungus had the same idea, and his suggestion of state change detection is pretty much what the adafruit code does: it alerts you to new motion of the end of motion.)

1 Like

An example showing the use of the state change detection method to get one print per PIR activation. I also fixed some data type usage.

//pir
const byte pirPin = 23;                 // PIR Out pin
bool pirState = 0;

void setup()
{
   // put your setup code here, to run once:
   Serial.begin(115200);
}

void loop()
{
   // put your main code here, to run repeatedly:
   //pir
   static bool lastPirState = LOW;
   pirState = digitalRead(pirPin);
   if (pirState != lastPirState)
   {
      if (pirState == HIGH)              // if motion detected
      {
         Serial.println("Hey I got you!!!");
      }
      lastPirState = pirState;
   }
}
2 Likes
unsigned long timePrint = 1000;
unsigned long timePrintPast = millis();
//pir 
int pirPin = 23;                 // PIR Out pin 
int pirStat = 0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);

}

void loop() {
  // put your main code here, to run repeatedly:
   //pir
  pirStat = digitalRead(pirPin);
    if (pirStat == HIGH) {            // if motion detected
    Serial.println("Hey I got you!!!");
}
//print stuff once a second
if( millis() - timePrintPast >= timePrint )
{
Serial.println( "do das bus stop here once a second?" );
timePrintPast = millis();
}

}

Another way.

See File|Examples|02.Digital|BlinkWithoutDelay

Many times even with your code posted below

18:28:35.510 -> Hey I got you!!!
18:28:35.556 -> Hey I got you!!!
18:28:35.556 -> Hey I got you!!!
18:28:35.556 -> Hey I got you!!!
18:28:35.604 -> Hey I got you!!!
18:28:35.604 -> Hey I got you!!!
18:28:35.650 -> Hey I got you!!!
18:28:35.650 -> Hey I got you!!!
18:28:35.696 -> Hey I got you!!!
18:28:35.696 -> Hey I got you!!!
18:28:35.696 -> Hey I got you!!!
18:28:35.744 -> Hey I got you!!!
18:28:35.744 -> Hey I got you!!!
18:28:35.790 -> Hey I got you!!!
18:28:35.790 -> Hey I got you!!!
18:28:35.837 -> Hey I got you!!!
18:28:35.837 -> Hey I got you!!!
18:28:35.837 -> Hey I got you!!!
18:28:35.883 -> Hey I got you!!!
18:28:35.883 -> Hey I got you!!!
18:28:35.930 -> Hey I got you!!!
18:28:35.930 -> Hey I got you!!!
18:28:35.976 -> Hey I got you!!!
18:28:35.976 -> Hey I got you!!!
18:28:35.976 -> Hey I got you!!!
18:28:36.023 -> Hey I got you!!!
18:28:36.023 -> Hey I got you!!!
18:28:36.071 -> Hey I got you!!!
18:28:36.071 -> Hey I got you!!!
18:28:36.118 -> Hey I got you!!!
18:28:36.118 -> Hey I got you!!!
18:28:36.118 -> Hey I got you!!!
18:28:36.166 -> Hey I got you!!!
18:28:36.166 -> Hey I got you!!!
18:28:36.212 -> Hey I got you!!!
18:28:36.212 -> Hey I got you!!!
18:28:36.257 -> Hey I got you!!!
18:28:36.257 -> Hey I got you!!!
18:28:36.257 -> Hey I got you!!!
18:28:36.303 -> Hey I got you!!!
18:28:36.303 -> Hey I got you!!!
18:28:36.350 -> Hey I got you!!!
18:28:36.350 -> Hey I got you!!!
18:28:36.396 -> Hey I got you!!!
18:28:36.396 -> Hey I got you!!!
18:28:36.396 -> Hey I got you!!!
18:28:36.441 -> Hey I got you!!!
18:28:36.441 -> Hey I got you!!!
18:28:36.489 -> Hey I got you!!!
18:28:36.489 -> Hey I got you!!!
18:28:36.535 -> Hey I got you!!!
18:28:36.535 -> Hey I got you!!!
18:28:36.535 -> Hey I got you!!!
18:28:36.580 -> Hey I got you!!!
18:28:36.580 -> Hey I got you!!!
18:28:36.627 -> Hey I got you!!!
18:28:36.627 -> Hey I got you!!!
18:28:36.673 -> Hey I got you!!!
18:28:36.673 -> Hey I got you!!!
18:28:36.720 -> Hey I got you!!!
18:28:36.720 -> Hey I got you!!!
18:28:36.720 -> Hey I got you!!!
18:28:36.767 -> Hey I got you!!!
18:28:36.767 -> Hey I got you!!!
18:28:36.813 -> Hey I got you!!!
18:28:36.813 -> Hey I got you!!!
18:28:36.860 -> Hey I got you!!!
18:28:36.860 -> Hey I got you!!!
18:28:36.860 -> Hey I got you!!!
18:28:36.907 -> Hey I got you!!!
18:28:36.907 -> Hey I got you!!!
18:28:36.954 -> Hey I got you!!!
18:28:36.954 -> Hey I got you!!!
18:28:36.954 -> Hey I got you!!!
18:28:37.000 -> Hey I got you!!!
18:28:37.000 -> Hey I got you!!!
18:28:37.047 -> Hey I got you!!!
18:28:37.047 -> Hey I got you!!!

That is not what I get when testing with my Uno and a PIR sensor like this:

I get one "Hey I got you!!!" for each activation of the PIR sensor.

If this is not what you have, please post a schematic, data sheets for your components and the actual code that you have uploaded.

Final working code

pirStat = digitalRead(pirPin);
if (pirStat != lastPirStat){ 
    Serial.println(pirStat); //for my debugging 
     if (pirStat == HIGH) {            // if motion detected
       Serial.println("Hey I got you!!!"); //for my debugging
       client.publish("Sensor1","1"); //MQTT Publish
       }

    lastPirStat = pirStat;
}

I am using something similar to this and after doing a bit of brain storming and debugging I added a pullup resistor and all the noise went away
sensor

Thank you for all of your ideas and help

Ah, good call. I certainly had in mind one of those shown in #8, which output a high or a low explicitly if I recall correctly, with no need for a pullup. Didn't think of that.

Don't forget the internal pullups which can be turned on in pinMode(myPin, INPUT_PULLUP); and save the need for messing around with actual resistors.

At the moment I am testing the sensor with ESP32 so adding an actual pullup resistor was quicker than going through the documentation but in final version the sensors (6 of them) will be added to my Home Automation on Mega 2560 so will try to see which of the IO support INPUT_PULLUP

All of the digital pins on a Mega support the internal pullup resistors. That includes the "analog input" pins which are really digital pins with analog input as a special function.

1 Like

Thank you, that is a great news :sparkler:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.