# SR04 interface, frequency control

**URL:** <https://forum.arduino.cc/t/sr04-interface-frequency-control/1405122>\
**Category:** General Guidance\
**Created:** [September 2, 2025, 12:28pm UTC](https://forum.arduino.cc/t/sr04-interface-frequency-control/1405122 "2025-09-02T12:28:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![imnotgoodatthis12](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/imnotgoodatthis12/32/1128454_2.png) [@imnotgoodatthis12](https://forum.arduino.cc/u/imnotgoodatthis12)\
**Post date:** [September 2, 2025, 12:28pm UTC](https://forum.arduino.cc/t/sr04-interface-frequency-control/1405122/1 "2025-09-02T12:28:32Z")

</div>

I have to create an interface that uses as input a ultrasonic sensor to control independently 3 actuators as output:  
• Light intensity of a LED flash light  
• Frequency of a piezo sound  
• Car wiper frequency  
The reaction time of the userinterface should be \<2 second. The settings of the 3 actuators should be displayed on LiquidCrystalDisplay (LCD).  
The arduino should count the amount of movements in 2 seconds, 1 movement means changing the LED intensity, 2 movements the Piezo and 3 the motor.  
The distance of the last movement to the ultrasound should control the intensity of the outputs.

I have been able to count the movements in a counter now, but am unsure how to continue, would someone have tips? I am unable to upload a picture for now.

```arduino
const int trigPin = 9;
const int echoPin = 10;
const int LEDone = 12;
const int LEDtwo = 11;

unsigned long startTime = 0;
unsigned int eventCount = 0;
bool wasBelow = false; 
bool countingActive = false;

float duration, distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  startTime = millis();

}

void loop() {
  unsigned long currentTime = millis();
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(250);
 // Wait for first trigger to start counting
  if (!countingActive && distance > 0 && distance < 10) {
    countingActive = true;
    eventCount = 1; // first detection counts as event
    startTime = currentTime;
    wasBelow = true;
  }

  if (countingActive) 
  {
    // While in 2-second window, count further entries
    if (distance > 0 && distance < 10) {
      if (!wasBelow) {
        eventCount++;
        wasBelow = true;
      }
    } else 
    {
      wasBelow = false;
  }

    // Check if 2 seconds have passed
    if (currentTime - startTime >= 2000) {
      Serial.print("Events in 2s: ");
      Serial.println(eventCount);

      // Reset system
      countingActive = false;
      eventCount = 0;
      wasBelow = false;
    }
  }
      digitalWrite(LEDone, LOW);
      digitalWrite(LEDtwo, LOW);

      }
}

```

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [September 2, 2025, 2:26pm UTC](https://forum.arduino.cc/t/sr04-interface-frequency-control/1405122/2 "2025-09-02T14:26:32Z")

</div>

Thanks for posting the code correctly, using code tags.

However, please identify a _specific_ problem that forum members can help with.

Explain what you wanted the code to do, what you tried, and describe what happened instead of what you wanted.

Hint: a good approach is to write code to do one particular task, and get that working perfectly, before adding another task. Keep in mind that tasks can be developed and tested independently, before combining.

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [September 2, 2025, 2:42pm UTC](https://forum.arduino.cc/t/sr04-interface-frequency-control/1405122/3 "2025-09-02T14:42:20Z")

</div>

> [@imnotgoodatthis12](#):
>
> count the amount of movements in 2 seconds,

Define movement, and how you are sensing it. The SR04 will not detect LED intensity, Piezo for sure and the motor???

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [September 2, 2025, 3:00pm UTC](https://forum.arduino.cc/t/sr04-interface-frequency-control/1405122/4 "2025-09-02T15:00:20Z")

</div>

> [@imnotgoodatthis12](#):
>
> would someone have tips?

Your code does not compile. You lost track of open/close braces (`{` and `}`) beginning here...

```arduino
  if (countingActive) {

```

... resulting in one-too-many close braces. Removing the last close brace allows it to compile, but is the resulting code correct?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 1, 2026, 3:00pm UTC](https://forum.arduino.cc/t/sr04-interface-frequency-control/1405122/5 "2026-03-01T15:00:30Z")

</div>

Closed on March 1, 2026, 3:00pm UTC

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