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!!!
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.)
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;
}
}
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();
}
}
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.