SR04 interface, frequency control

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.

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);

      }
}

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.

1 Like

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

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

  if (countingActive) {

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