# Repeated Beam Sensor Inputs interfering with Loop

**URL:** <https://forum.arduino.cc/t/repeated-beam-sensor-inputs-interfering-with-loop/1310748>\
**Category:** Programming\
**Created:** [October 13, 2024, 5:55am UTC](https://forum.arduino.cc/t/repeated-beam-sensor-inputs-interfering-with-loop/1310748 "2024-10-13T05:55:40Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 13, 2024, 6:05am UTC](https://forum.arduino.cc/t/repeated-beam-sensor-inputs-interfering-with-loop/1310748/2 "2024-10-13T06:05:00Z")

</div>

> [@bmacon](#):
>
> I can't seem to find the best way to prevent the reading of additional inputs until the actuator is finished

Given you use delay() to perform the actuator's motion, you don't read the sensor at all during the movement.

Regarding your code structure, you might benefit from studying state machines. Here is a small introduction to the topic: [Yet another Finite State Machine introduction](https://forum.arduino.cc/t/yet-another-finite-state-machine-introduction/1198997)

if you want blocking code, you could probably do something as simple as this (typed here, fully untested)

```arduino
const byte pirPin = 12;
const byte ENApin = 6;
const byte IN1pin = 7;
const byte IN2pin = 8;

void goCW() {
  digitalWrite (IN2pin, LOW);
  digitalWrite (IN1pin, HIGH);
  digitalWrite (ENApin, HIGH);
}

void goCCW() {
  digitalWrite (IN1pin, LOW);
  digitalWrite (IN2pin, HIGH);
  digitalWrite (ENApin, HIGH);
}

void stop() {
  digitalWrite (ENApin, LOW);
  digitalWrite (IN1pin, LOW);
  digitalWrite (IN2pin, LOW);
}

void setup() {
  pinMode (pirPin, INPUT);
  pinMode (ENApin, OUTPUT);
  pinMode (IN1pin, OUTPUT);
  pinMode (IN2pin, OUTPUT);
  delay (60000);
}

void loop() {
  if (digitalRead(pirPin) == HIGH) {
    goCW(); delay (3000);
    stop(); delay(3000);
    goCCW(); delay (4000);
    stop();
  }

  // wait until PIR rests before triggering a new motion
  while (digitalRead(pirPin) == HIGH) ;
}

```

---

_[View the full topic](https://forum.arduino.cc/t/repeated-beam-sensor-inputs-interfering-with-loop/1310748)._
