Doubts on how to set an " house proximity alarm".

#define trigpPin X
#define echoPin Y
#define buzzer Z
int incomingByte = 2710

void setup() {

 pinMode(trigPin,  OUTPUT);
 pinMode(echoPin,  INPUT);
 digitalWrite(trigPin,  LOW);
 pinMode(buzzer, OUTPUT);

void loop() {

unsigned long duration;
float distance;

digitalWrite(trigPin,  HIGH);
digitalWrite(trigPin,  LOW);
duration =  pulseIn(echoPin,  HIGH);
distance =    duration /  58.31;  

Serial.print("Distance-> ");

if (Serial.available() >= 0){
incomingByte =;

delay (1000);
delay (1000);
delay (1000);
delay (1000);
delay (1000);
delay (1000);
delay (1000);
delay (1000);
delay (1000);
delay (1000);

 tone(buzzer, 880);

I would like if someone could help me with this example.

I was supposed to do a proximity alarm which will trigger when something is less than 60cm away from the house. The distance will have to show up on screen if something enters the perimeter one time. Then i would have to set a 10 second delay in order to enter a code “2710” and if i wasn’t on time the buzzer would trigger (880Hz for 0.5s).

Atm, i’m kind of stuck on how to set the code to stop the loop, the countdown is just optional as long as there is a 10 sec delay

What part of your code currently works ? You are using an ultrasonic sensor. Does the part which prints the distance work ?

Break the rest of it down into smaller parts. 1. sounding a buzzer as a specific frequency. 2. entering a pin code e.g. 2170 and testing it. 3. testing if a delay period has expired and test these in a separate sketch.

Use code tags when posting here.

I don't really have a away of testing it, and i don't know how to use tinkercad. This is an example to study for a project i will be evaluated doing.

For what i've tried before i know that the code for the ultrasonic sensor works and the buzzer one too, but i've never implemented a code to stop the loop. That's why i don't know where and how to set it.

You can't stop the loop. You can choose to execute statements in setup() and loop(), or not execute them, using control statements.

Please go back and edit your post to put your code in code tags.

So how am i supposed to add this “password” during the 10 sec delay to stop the buzzer from triggering?

You don't have a hope of making this work, while using the delay() function. You will not be able to input any keystrokes because delay() prevents any other processing from occurring until it exits.

Actually, I don't see any code at all that accepts input. So you have to explain to us how that will work.


The house has an anti-theft mechanism based on a distance sensor (ultrasonic sensor placed on the entrance door. When the detected distance is less than 60 cm, it is considered that an intruder may be present near, the distance value is immediately recorded on the screen (a single detection). Then, a countdown of 10 seconds is initiated during which a code (defined by the students) should be applied through the serial console. If the correct code does not get detected in time, "INTRUSION!" will appear on the screen (serial console) and the buzzer will be activated which will be terminated only when the correct code is finally introduced.

In order to be quicker when they examinate my coding, they will check what happens if they put a random code and it just stops the buzzer / they put mine and the buzzer dont go off / they don't put anything to see if it goes off. And the buzzer will sound at 880Hz during 0.5sec

You didn’t ask any question there.

I am supposed to write the coding? then i will have to set up the montage.

k1zz: I am supposed to write the coding?

That's why you're post here, isn't it? To get help with YOUR code?

60 cm is pretty darn close. The "intruder" can reach out and touch the house at that distance.

my doubt is how do i set a line where it would stop the buzzer to go of, because i don't know how to and i'm not finding the info. It's a doubt i didn't asked anyone to do the the rest. It's the only question i asked on the main post. Isn't the forum to post doubts??

It's a small montage that's why the "house proximity alarm" is between quotation marks. It's a question from an exercise sheet -_-

By “doubts”, you mean questions.

Anyway, if you want to sound a buzzer for a certain amount of time, you set a timer and check periodically if the timer has expired and, if so, act on it.

Look at the “blink without delay” example sketch to see how you can use millis() to do this but basically it is this in a crude form:

void loop() {

static unint32_t myTimer = 0 ;

if ( intruder detected ) {
myTimer = millis() ;

if ( millis() - myTimer < buzzerSoundingTimeInMs ) sound_buzzer() ;


Invert the logic if you want to explicitly stop the buzzer and maybe have a buzzer status variable.