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