# Doing Things Proper

**URL:** <https://forum.arduino.cc/t/doing-things-proper/1438084>\
**Category:** Programming\
**Created:** [April 1, 2026, 6:16pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084 "2026-04-01T18:16:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![squishyjiggles](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/squishyjiggles/32/1148440_2.png) [@squishyjiggles](https://forum.arduino.cc/u/squishyjiggles)\
**Post date:** [April 1, 2026, 6:16pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/1 "2026-04-01T18:16:54Z")

</div>

I made a proximity sensor with lights and sound. But being self taught, was wondering if an " else" funtion to keep lights and sound off is a better option?

```arduino
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 3;
const int ledPin1 = 5;
const int ledPin2 = 6;

long duration;
float distanceInches;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    
}

void loop() {
  // Clear the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Send 10 µs pulse to trigger
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read echo time
  duration = pulseIn(echoPin, HIGH);

  // Convert to inches
  distanceInches = duration * 0.0133 / 2;

  // Print to Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distanceInches);
  Serial.println(" inches");

  delay(250);
    
 if(distanceInches < 4) {
        tone(buzzerPin, 1000);
        digitalWrite(ledPin1, HIGH);
        delay(100);
        tone(buzzerPin, 1500);
        digitalWrite(ledPin2, HIGH);
        delay(100);
        noTone(buzzerPin);
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        delay(400);
    }   
    
}

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [April 1, 2026, 6:39pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/2 "2026-04-01T18:39:09Z")

</div>

- **Always** show us a good schematic of your proposed circuit. Show us good images of **your** ‘actual’ wiring.

```arduino

/*
 * Ultrasonic Distance Alarm
 * Detects objects within 4 inches and triggers a two-tone alarm with LEDs.
 */

const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 3;
const int ledPin1 = 5;
const int ledPin2 = 6;

long duration;
float distanceInches;

void setup() {
  // Initialize Serial communication
  Serial.begin(9600);
  
  // Define pin modes
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  // Clear the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Send 10 microsecond pulse to trigger the sensor
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echo time (duration of the travel in microseconds)
  duration = pulseIn(echoPin, HIGH);

  // Convert time to inches
  // Calculation: (Speed of Sound is ~13,500 inches/sec) / 2 for round trip
  distanceInches = duration * 0.0133 / 2;

  // Debugging output to Serial Monitor
  Serial.print("Distance: ");
  if (duration == 0) {
    Serial.println("Out of range");
  } else {
    Serial.print(distanceInches);
    Serial.println(" inches");
  }

  // Alarm Logic
  // distanceInches > 0 ensures we don't trigger on a sensor timeout
  if (distanceInches > 0 && distanceInches < 4) {
    // Stage 1: First tone and LED
    tone(buzzerPin, 1000);
    digitalWrite(ledPin1, HIGH);
    delay(100);

    // Stage 2: Second tone and LED
    tone(buzzerPin, 1500);
    digitalWrite(ledPin2, HIGH);
    delay(100);

    // Turn off feedback
    noTone(buzzerPin);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    
    // Short pause to keep the alarm "pulsing"
    delay(200); 
  } else {
    // Standard delay between readings when no object is close
    delay(250);
  }
}

```

- Is this a passive buzzer ?
- Does the LED have a current limiting resistor ?

## Edit

You may want to extend the delay:

~~delay(100);~~

```arduino
    tone(buzzerPin, 1000);
    digitalWrite(ledPin1, HIGH);
    delay(1000); // <——-<<<<

```

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [April 1, 2026, 6:46pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/3 "2026-04-01T18:46:04Z")

</div>

Been programming for over 65 years, never heard of an else 'function'.

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [April 1, 2026, 6:51pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/5 "2026-04-01T18:51:05Z")

</div>

> [@squishyjiggles](#):
>
> ` distanceInches = duration * 0.0133 / 2;`

Are you sure you want the conversion inherent in that statement  
and this one

> [@squishyjiggles](#):
>
> ` if(distanceInches < 4) {`

---

<div class="post-metadata">

**Author:** ![squishyjiggles](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/squishyjiggles/32/1148440_2.png) [@squishyjiggles](https://forum.arduino.cc/u/squishyjiggles)\
**Post date:** [April 1, 2026, 7:04pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/6 "2026-04-01T19:04:05Z")

</div>

Im a self taught noob lol. I think i ment else as a " command" not function. Still learning proper terms when disecting code. Aint got no one to teach me , but by golly im trying. Lol

---

<div class="post-metadata">

**Author:** ![squishyjiggles](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/squishyjiggles/32/1148440_2.png) [@squishyjiggles](https://forum.arduino.cc/u/squishyjiggles)\
**Post date:** [April 1, 2026, 7:13pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/7 "2026-04-01T19:13:44Z")

</div>

I get what your saying aout explaining my work and ill start doing that better. But that code is soo simple ,i didnt feel the need. U a smart dude! I wanna be like Larry. I do have the the book "Algorithms and data structures equal programs on the way. Supposed to be a good one. Thanx!

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [April 1, 2026, 7:22pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/8 "2026-04-01T19:22:09Z")

</div>

Not a command either. May I suggest reading cover to cover 'The C Programming Language'. Then for Arduino specifics, the Arduino Cookbook.

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [April 1, 2026, 7:24pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/9 "2026-04-01T19:24:02Z")

</div>

Actually it's Data Structures and Algorithms but yes it is a good intermediate to advanced book. You need to start at the beginning so you know what 'else' is etc.

---

<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:** [April 1, 2026, 7:48pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/10 "2026-04-01T19:48:54Z")

</div>

> [@squishyjiggles](#):
>
> keep lights and sound off

Would you describe how you intend the devices in this project to function, especially the lights and sound. Is the proximity sensor stationary or moving?

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [April 1, 2026, 10:25pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/11 "2026-04-01T22:25:44Z")

</div>

> [@squishyjiggles](#):
>
> being self taught, was wondering if an " else" funtion to keep lights and sound off is a better option?

code looks good, clean to me

i wonder if what you're really asking if there is a way to maintain an active indication as long as the distance \< 4 and only turn it off when distance \>= 4?

```arduino

bool active = false;

void check (
    int distance )
{
    if (! active && 4 > distance) {
        active = true;
        turnOn ();
    }
    else if (active && 4 < distance) {
        active = false;
        turnOff ();
    }
}

```

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [April 2, 2026, 12:37am UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/12 "2026-04-02T00:37:09Z")

</div>

You can also start here, if you need baby steps:  
[https://docs.arduino.cc/language-reference/](https://docs.arduino.cc/language-reference/)

Those Arduino Reference pages provide only a subset of the full language features, which is actually a blessing for many. While browsing it, you can be waiting for the recommended reading to arrive, presuming you've ordered it.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [April 2, 2026, 8:46am UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/13 "2026-04-02T08:46:04Z")

</div>

> [@camsysca](#):
>
> Those Arduino Reference pages provide only a subset of the full language features

[The C Programming Language](https://seriouscomputerist.atariverse.com/media/pdf/book/C%20Programming%20Language%20-%202nd%20Edition%20(OCR).pdf) provides a complete description of "C" and small programs demonstrating its features

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [April 2, 2026, 10:25am UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/14 "2026-04-02T10:25:22Z")

</div>

> [@squishyjiggles](#):
>
> was wondering if an " else" funtion to keep lights and sound off is a better option?

Has anyone answered your question or do you still need help?  
I don't need a schematic and I understand what you mean even though your terminology is not according to C specifications.

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [April 2, 2026, 10:25am UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/15 "2026-04-02T10:25:57Z")

</div>

That's quite an astute observation, indeed.

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [April 2, 2026, 10:34am UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/16 "2026-04-02T10:34:58Z")

</div>

@squishyjiggles  
Just ask

---

<div class="post-metadata">

**Author:** ![squishyjiggles](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/squishyjiggles/32/1148440_2.png) [@squishyjiggles](https://forum.arduino.cc/u/squishyjiggles)\
**Post date:** [April 2, 2026, 12:58pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/17 "2026-04-02T12:58:45Z")

</div>

Thanks for thr reply Jim. No, no one really seemed to get my question. But many did recomend some good reading material to check out. Was just just wondering if at the very end of my code- after the "if" , should i use an "else" with the commands in those curly brackets to keep sound and visuals off. Amature trying to look more professional, i guess

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [April 2, 2026, 1:18pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/18 "2026-04-02T13:18:22Z")

</div>

> [@squishyjiggles](#):
>
> Amature trying to look more professional, i guess

Well, in your setup() you should initialize any outputs to a known state, so set the LEDs to LOW and do a noTone. Now when the program starts the LEDs will be off and there will not be a tone unless you do otherwise.

Since in your **if** statement you do turn the LEDs off and do a noTone, putting it again in an **else** would be superfluous. Also, the compiler may see that it’s already been done and actually optimize out the else statement altogether.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [April 2, 2026, 5:08pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/19 "2026-04-02T17:08:45Z")

</div>

> [@squishyjiggles](#):
>
> ```arduino
> if(distanceInches < 4) {
> tone(buzzerPin, 1000);
> digitalWrite(ledPin1, HIGH);
> delay(100);
> tone(buzzerPin, 1500);
> digitalWrite(ledPin2, HIGH);
> delay(100);
> noTone(buzzerPin);
> digitalWrite(ledPin1, LOW);
> digitalWrite(ledPin2, LOW);
> delay(400);
> }  
> 
> ```

when the proximity detector recognizes an object within the distance threshold, this code causes the tones to repeatedly cycle on and off as long the the object remains within the distance threshold, as well as to complete the cycle when an object goes outside of the distance threshold, not terminate immediately

---

<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:** [April 2, 2026, 6:19pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/20 "2026-04-02T18:19:39Z")

</div>

> [@squishyjiggles](#):
>
> no one really seemed to get my question

?

> [@xfpd](#):
>
> describe how you intend the devices in this project to function

> [@xfpd](#):
>
> Is the proximity sensor stationary or moving?

The information you provide directly effects the help you receive.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [April 2, 2026, 9:55pm UTC](https://forum.arduino.cc/t/doing-things-proper/1438084/21 "2026-04-02T21:55:09Z")

</div>

> [@squishyjiggles](#):
>
> wondering if an " else" funtion to keep lights and sound off is a better option?

> [@squishyjiggles](#):
>
> no one really seemed to get my question.

> [@gcjr](#):
>
> ```arduino
> bool active = false;
> 
> void check (
> int distance )
> {
> if (! active && 4 > distance) {
> active = true;
> turnOn ();
> }
> else if (active && 4 < distance) {
> active = false;
> turnOff ();
> }
> }
> 
> ```

do you understand the above code?

> [@squishyjiggles](#):
>
> should i use an "else" with the commands in those curly brackets to keep sound and visuals off.

[Next page](https://forum.arduino.cc/t/doing-things-proper/1438084.md?page=2)
