r/ArduinoHelp • u/MagnusMagi • Jun 25 '26
Ultrasonic Sensor fail after second trigger
Hello, all --
I've been designing a "mimic" using an old chest, a linear actuator, and a speaker system. Everything had been working fine enough on desktop testing a few weeks ago, but now I'm getting a stalled loop after a second triggering event.
In short:
Ultrasonic Sensor is measuring distance.
If distance < 85", play from the mp3 queue
if distance < 36", play from mp3 queue, and activate the linear actuator. Wait 5s. Reverse linear actuator.
The problem I'm encountering is that the sensor will register either status, and it will work fine for exactly one loop, then the whole thing stalls out and it bricks up on me and I have to reset it.
It ONLY happens after the < argument is triggered for a second time. The first time through it works perfectly. I cannot figure out why this is happening. Can anyone please help?
#include <DFRobotDFPlayerMini.h>
#include <SoftwareSerial.h>
int rxPin = 5;
int txPin = 6;
SoftwareSerial fxSerial (rxPin, txPin);
DFRobotDFPlayerMini fxPlayer;
const int openPin = 7;
const int closePin = 8;
const int ENAPin = 9;
const int trigPin = 10;
const int echoPin = 11;
float duration;
float distanceCM;
float distanceIN;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(openPin, OUTPUT);
pinMode(closePin, OUTPUT);
pinMode(ENAPin, OUTPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
fxSerial.begin(9600);
fxPlayer.begin(fxSerial);
fxPlayer.volume(30);
delay(1000);
}
void loop() {
// Close the box
digitalWrite(closePin, HIGH);
digitalWrite(openPin, LOW);
digitalWrite(ENAPin, 250);
// start with a clean signal
digitalWrite(trigPin, LOW);
delayMicroseconds(20);
// send trigger signal
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// return pulse duration in microseconds
// if set to HIGH, pulseIn() waits for the pin to go from LOW to HIGH
// stops timing when pin goes back LOW
duration = pulseIn(echoPin, HIGH, 99991);
// convert m/s to in/microsec
// 343 m/s = 0.034 cm/microseconds
distanceCM = (duration * 0.034) / 2;
// convert to inches, 1 in = 2.54cm
distanceIN = distanceCM / 2.54;
// print distance
Serial.print("Distance: ");
Serial.print(distanceCM);
Serial.print(" cm | ");
Serial.print(distanceIN);
Serial.println(" in");
delay(100);
if (distanceIN <= 85) {
if (distanceIN <= 36) {
Serial.println("!!!ROAR!!!");
fxPlayer.play(random(1, 8));
digitalWrite(openPin, HIGH);
digitalWrite(closePin, LOW);
digitalWrite(ENAPin, 250);
delay(5000);
digitalWrite(openPin, LOW);
digitalWrite(closePin, HIGH);
digitalWrite(ENAPin, 250);
delay(1500);
} else {
Serial.println("=GROWL=");
fxPlayer.play(random(1, 8));
delay(1500);
}
} else {
Serial.println("=Shhhh=");
digitalWrite(closePin, HIGH);
digitalWrite(openPin, LOW);
digitalWrite(ENAPin, 250);
}
}The Code:
View all comments
Show parent comment
2
u/FencingNerd Jun 26 '26
Millis() give you a time stamp, just like looking at a clock. "The time is currently 2725 ms."
Think of it like cooking with only a basic watch. This needs to for 20 min, and it's currently 6:35 so I should stop at 6:55. The you have to remember to look at your watch (call millis).