problem with pause

Hi everyone, my project is using ultrasonic sensor and turn on the led turn on when something near <4cm and the led 2 turn on when <200, and I want to pause for like 5 seconds before the led (<4cm) turn on. Please help :3 tks so much and sorry for my bad english

#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10

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

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance <= 0){
Serial.println(“Out of range”);
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}

I want to pause for like 5 seconds before the led (<4cm) turn on.

When you detect that the distance has become less than 4 cm (NOTE : not is less than 4 cm - see the StateChangeDetection example in the IDE) save the current value of millis() as the start time and set a boolean to true.

Each time through loop(), if the boolean is true, test whether 4 seconds has elapsed by subtracting the start time from the current time. If the period has not elapsed keep going round loop() until it has. Once the period has elapsed run whatever code you need.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE for extra help on using millis() for non blocking timing.

tks for your help

Hello Everybody,

My purpose is the led is turn on after 1 second when something near HC-SR04 about <4cm, and after that if there is nothing in front of the HC-SR04 about 4cm → the led turn off. But my led dont turn off :frowning:

Please help and tks for your reading <3

unsigned long blinkStartMillis;
unsigned long currentMillis;
const unsigned long blinkPeriod=1000; //blink period
const byte blinkLedPin=13;
#define trigPin 11

#define echoPin 12
void setup(){
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(blinkLedPin,OUTPUT);
blinkStartMillis=millis(); //start time of blinking LED
}

void loop()
{
currentMillis = millis();
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH); // delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens
blink();
}
else {
digitalWrite(blinkLedPin,LOW);
}
}
void blink()
{
if (currentMillis - blinkStartMillis >= blinkPeriod) //test whether the period has elapsed
{
digitalWrite(blinkLedPin, !digitalRead(blinkLedPin));
blinkStartMillis=currentMillis;
}

}

Please edit you post to have code tags. See “How to use the forum”. Tip, press Ctrl+T in the IDE before you copy it. Looks better, right?

But start by adding some prints to Serial in strategic places. You already have it set up :wink: For example, print the durations etc.

You have a call Serial.begin(), but no calls to Serial.print(). So, you are debugging by guesswork.

That way sucks. Good luck with that approach. I prefer to debug with facts.

tks for help and sorry for my mistake, now I will try to make my sketch more clearly, and sorry for my bad english

unsigned long blinkStartMillis;
unsigned long currentMillis;
const unsigned long blinkPeriod = 1000; //blink period
const byte blinkLedPin = 13;
#define trigPin  11
#define echoPin 12

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(blinkLedPin, OUTPUT);
  blinkStartMillis = millis(); //start time of blinking LED
}

void loop()
{
  currentMillis = millis();
  long duration, distance;
  digitalWrite(trigPin, LOW);    // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);  //  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  if (distance < 4) {  // This is where the LED On/Off happens
    blink();
  }
  else {
    digitalWrite(blinkLedPin, LOW);
  }

}
void blink()
{
  if (currentMillis - blinkStartMillis >= blinkPeriod) //test whether the period has elapsed
  {
    digitalWrite(blinkLedPin, !digitalRead(blinkLedPin));
    blinkStartMillis = currentMillis;
  }
}

Thanks for posting it correctly and clearly. But uhm, where are the suggested debug lines?

sorry Iam just a begginer, tks for being patient

unsigned long blinkStartMillis;
unsigned long currentMillis;
const unsigned long blinkPeriod = 1000; //blink period
const byte blinkLedPin = 13;
#define trigPin  11

#define echoPin 12
void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(blinkLedPin, OUTPUT);
  blinkStartMillis = millis(); //start time of blinking LED
}

void loop()
{
  currentMillis = millis();
  long duration, distance;
  digitalWrite(trigPin, LOW);    // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);  //  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  if (distance < 4) {  // This is where the LED On/Off happens
    blink();
    Serial.println("blinkStart");
  }
  else {
    digitalWrite(blinkLedPin, LOW);
    Serial.println("blinkStop");

  }


}
void blink()
{
  if (currentMillis - blinkStartMillis >= blinkPeriod) //test whether the period has elapsed
  {
    digitalWrite(blinkLedPin, !digitalRead(blinkLedPin));
    blinkStartMillis = currentMillis;
  }

}

and the serial monitor show me strange symbol such as reverse question mark

and the serial monitor show me strange symbol such as reverse question mark

Does the baud rate in the Serial Monitor application match that of the code on the Arduino?

Please don't double Post. This is the same project as in your other Thread. Please click Report to Moderator and ask to have them merged.

The links you got in your other Thread have the basis for answering the question in this Thread..

When posting code please use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor See How to use the Forum

...R

tks I have reported to merge 2 posts and changed a baud, and what I receive from Serial Monitor is blinkStart and wont stop :frowning:

unsigned long blinkStartMillis;
unsigned long currentMillis;
const unsigned long blinkPeriod = 1000; //blink period
const byte blinkLedPin = 13;
#define trigPin  11

#define echoPin 12
void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(blinkLedPin, OUTPUT);
  blinkStartMillis = millis(); //start time of blinking LED
}

void loop()
{
  currentMillis = millis();
  long duration, distance;
  digitalWrite(trigPin, LOW);    // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);  //  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  if (distance < 4) {  // This is where the LED On/Off happens
    blink();
    Serial.println("blinkStart");
  }
  else {
    digitalWrite(blinkLedPin, LOW);
    Serial.println("blinkStop");
  }
  Serial.print(distance);
  Serial.println("cm");
  delay(30);

}
void blink()
{
  if (currentMillis - blinkStartMillis >= blinkPeriod) //test whether the period has elapsed
  {
    digitalWrite(blinkLedPin, !digitalRead(blinkLedPin));
    blinkStartMillis = currentMillis;
  }

}

So what is the value of distance, before the if statement? Does the value of distance match what you expect to see?

just only blinkStart, and I am ok with it but it wont stop :frowning: I want it to stop

Romonaga:
So what is the value of distance, before the if statement? Does the value of distance match what you expect to see?

tks everybody, I will stop this project for awhile to research a millis function and more :frowning: really appreciate for your helps guys.

But why don’t you do what’s suggested? Aka, not only print “blinkStart” or “blinkStop” but also print the values of which you are now guessing it’s value.

unsigned long blinkStartMillis;
unsigned long currentMillis;
const unsigned long blinkPeriod = 1000; //blink period
const byte blinkLedPin = 13;
#define trigPin  11

#define echoPin 12
void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(blinkLedPin, OUTPUT);
  blinkStartMillis = millis(); //start time of blinking LED
}

void loop()
{
  currentMillis = millis();
  long duration, distance;
  digitalWrite(trigPin, LOW);    // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);  //  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  Serial.print("Duration ");
  Serial.println(duration);
    
  distance = (duration / 2) / 29.1;
  Serial.print("Distance ");
  Serial.println(distance);
  
  if (distance < 4) {  // This is where the LED On/Off happens
    blink();
    Serial.println("blinkStart");
  }
  else {
    digitalWrite(blinkLedPin, LOW);
    Serial.println("blinkStop");
  }
  Serial.print(distance);
  Serial.println("cm");
  delay(30);

}
void blink()
{
  if (currentMillis - blinkStartMillis >= blinkPeriod) //test whether the period has elapsed
  {
    digitalWrite(blinkLedPin, !digitalRead(blinkLedPin));
    blinkStartMillis = currentMillis;
  }

}