0
votes

I'm newbie in arduino and I'm having some issues here. I bought two moviment PIR sensors, when someone passes in front of the sensor the logic value is HIGH, otherwise the value is LOW.

Image

Besides that I would like to increment a value when the sensor A is HIGH, and decrement the same value when the Sensor B is HIGH.

But I have to issues here, I can't increment and decrement the variable value, and the serial print is repeating constantly for 5000 miliseconds, because I need to use delay, otherwise, the code will not work.

int pinPIRa;
int pinPIRb;
int count;

void setup() {
  Serial.begin(9600);
  pinMode(pinPIRa,INPUT);
  pinMode(pinPIRb,INPUT);

}

void loop() {

  if(digitalRead(pinPIRb) == HIGH){

    Serial.print("Sensor B");
    count--;

  }

  if(digitalRead(pinPIRa) == HIGH){

    Serial.print("Sensor A");
    count++;

  }

  delay(5000);

}

Now I have two questions for you guys, how can I prevent the serial from repeating numerous times? I tried to use Serial.flush() but it doesn't work.

And I would like to know how can I increment and decrement the count variable? The count variable is decrementing and incrementing more than one time because the code is running inside a loop.

Thank you.

1

1 Answers

2
votes

To prevent repetition you may also keep a state variable to keep track of changes. This will make you only consider the state if it actually changed. You won't hit any memory limits by doing this :)

This should encapsulate both the printing and the counter change (fix two at once)