Ultrasonic sensor

i have project and i do not know how to code for this

  • If it detects any object less than 5 cm away for more than 5 seconds, the buzzer will sound

i do not know how to code the part where they say if the object is detected for more then 5 seconds
any help would be appreciated, thanks

i don not know how to code the part where they say if the object is detected for more then 5 sec.

When the object becomes detected save the value of millis() as the start time

Whilst the object is still in range check whether millis() minus the start time is greater than the required period sound the buzzer

ohh i see thank you very much
but i dont get how to save the value of millis() part
( i learnt only basic codes sorry)
when i search online i got this code
unsigned long time;

void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Time: ");
time = millis();

Serial.println(time); //prints time since program started
delay(1000); // wait a second so as not to send massive amounts of data
}

how do i link it to mine?

The trick is to save the value of millis()

startTime = millis();

when the object becomes detected rather than while it is detected

Take a look at the StateChangeDetection example in the IDE. You need to save the previous detection state and test whether it has changed next time you read it, which should be each time through loop(). If it has changed and is now detecting an object then save the value of millis() but don't do it again unless/until the conditions are met again. A boolean variable is handy for this. Now you have the start time saved to compare with the current time to detect the passing of your period

ohhh i see ill try it now

thank you very much

i have a project that needs this criteria
" if object detected is 5 cm or less for more then 5 sec led will light up
the problem i have with my code now is that the ultrasonic sensor stops reading values after object is detected it 5 cm or less and the led does not stop lighting up.

This is my code

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
int led = 3;
// defines variables
long duration;
int distance;

unsigned long currentTime = 0;
unsigned long pastTime = 0;
int currentState = 0;
unsigned long seconds = 0;

//unsigned long previousTime = 0;
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
pinMode(led, OUTPUT);
}

void loop() {
digitalWrite(trigPin, LOW); // clears trigpin
delayMicroseconds(5);
digitalWrite(trigPin, HIGH); // set trigpin on high fro 10ms
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
noInterrupts();
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
interrupts();
distance= duration*0.034/2; // Calculating the distance
Serial.print("Distance: “); // Prints the distance on the Serial Monitor
Serial.println(distance);
delay(1000);
while(distance < 5){
pastTime = currentTime;
currentTime = millis();
unsigned long timePassed = currentTime - pastTime;
Serial.print(” time: ");
Serial.println(timePassed);
delay(1000);
if (timePassed >= 5000);
{
seconds ++;
pastTime = currentTime;
digitalWrite(led, HIGH);
}
while (distance > 5){
digitalWrite(led,LOW);
}
}
}

if (timePassed >= 5000);Oops

Please remember to use code tags when posting code

More here

i still have the same problem
what else can i do?

@shribola

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.

   while (distance > 5){
           digitalWrite(led,LOW);
           }

You could start by eliminating the road-block, and the issue already highlighted.

Sorry about the repost will not do it again.
Thank you i will try to fix the code.

even after changing the code it still causes my ultrasonic sensor reading to pause and just lights up the led continuously

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
int led = 3;
// defines variables
long duration;
int distance;

unsigned long currentTime  = 0;
unsigned long pastTime     = 0;
int           currentState = 0;
unsigned long seconds = 0;

//unsigned long previousTime = 0;
void setup() 
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
pinMode(led, OUTPUT);
}

void loop() {
digitalWrite(trigPin, LOW); // clears trigpin
delayMicroseconds(5);
digitalWrite(trigPin, HIGH); // set trigpin on high fro 10ms
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
noInterrupts();
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
interrupts();
distance= duration*0.034/2; // Calculating the distance
Serial.print("Distance: ");  // Prints the distance on the Serial Monitor
Serial.println(distance);
delay(1000);
if (distance < 5){
pastTime    = currentTime;
   currentTime = millis();
   unsigned long timePassed = currentTime - pastTime;
    while (timePassed >= 5000) {
  
              pastTime = currentTime;
             digitalWrite(led, HIGH);
           }
}
           if(distance>5){
             digitalWrite(led,LOW);
           
}
delay(1000);
}
while (timePassed >= 5000) {
 
              pastTime = currentTime;
             digitalWrite(led, HIGH);
           }

Another road-block.

The while() code does not seem to work but if i use if() it sorta works it lights up 2 sec after the object is detected. I cant seem to fix it no matter what.
Really sorry if im bothering you

If timePassed ever exceeds 4999, the whole loop be entered, but you can never get out of it, because nothing in the loop body ever modifies the value of timePassed

TheMemberFormerlyKnownAsAWOL:
If timePassed ever exceeds 4999, the whole loop be entered, but you can never get out of it, because nothing in the loop body ever modifies the value of timePassed

ah i see, ill go fix it thank you very much