Make millis timer wait until a if statement i true(Need help)

int interval=20000;

unsigned long previousMillis=0;

void loop() {

unsigned long currentMillis = millis();

if (bdistance >=15 && bdistance <=1){
  previousMillis = currentMillis;
}

if ((unsigned long)(currentMillis - previousMillis) >= interval) {
  
  Serial.println("FullFULLFULL");
  previousMillis = currentMillis;

}
}

Why wont it work

The code does not work because it is missing a setup() function.

Help us help you.

......................................................................

Welcome to the forum

Do you realise that you have posted in the forum section where users giving help expect to be paid ?

Because once you first if statement becomes true, value previousMillis will reinitialized every loop() runs and the second condition never match.
You need to initialize previousMillis only once, use a flag:

nt interval=20000;
unsigned long previousMillis=0;
bool interval_is_running = false;

void loop() {

unsigned long currentMillis = millis();

if (bdistance >=15 && bdistance <=1){
  if (! interval_is_runnung) 
      previousMillis = currentMillis;
  interval_is_runnung = true;
}

if ((currentMillis - previousMillis) >= interval) {
    Serial.println("FullFULLFULL");
    previousMillis = currentMillis;
  }
}

ooooh, where do i post?

Hi, thanks for the respond!

It's still not working for me, is there anything i'm missing out?

#include<Servo.h>

Servo servo;
Servo servo2;

int const trigPin = 6; //navngir pin 6 til trigPin
int const echoPin = 5;
int const btrigPin = 10;
int const bechoPin = 11;

int interval=20000;
unsigned long previousMillis=0;
bool interval_is_running = false;

void setup() { //Denne koden køyres kun eingong
Serial.begin(9600); //Bits per sekund
pinMode(trigPin, OUTPUT); //Gjør trigPin om til Output
pinMode(echoPin, INPUT); //Gjør echoPin om til Input
pinMode(btrigPin, OUTPUT);
pinMode(bechoPin, INPUT);

servo.attach(3);
servo2.attach(9);

}

void loop() { //Denne koden køyres heile tida
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); //Gjøpr trigpin om til HIGH eller 1
delayMicroseconds(2);
digitalWrite(trigPin, LOW); //Gjør trigPin om til LOW eller

long timedelay = pulseIn(echoPin, HIGH);
int distance = 0.0343 * (timedelay/2);
if (distance <=50 && distance >=1) {  //Visst noko er mellom 0cm og 50cm så kjører det denne if koden
        servo.write(80);  //Justerer servoen til 50 grader
        servo2.write(140);
      delay(3000); //venter 3 sekund
}   else {

          servo.write(180); //Gjør servoen om til 160 grader som gjør til at lokket lukker seg.
          servo2.write(40);//Gjør servoen om til 40 grader som gjer at lokket lukker seg.
}
delay(60);

delayMicroseconds(2);

digitalWrite(btrigPin,LOW);
delayMicroseconds(2);
digitalWrite(btrigPin, HIGH); //Gjøpr trigpin om til HIGH eller 1
delayMicroseconds(2);
digitalWrite(btrigPin, LOW); //Gjør trigPin om til LOW eller 0


long td = pulseIn(bechoPin,HIGH);
int bdistance = 0.0343 * (td/2);

unsigned long currentMillis = millis();

if (bdistance >=15 && bdistance <=1){
if (! interval_is_running)
previousMillis = currentMillis;
interval_is_running = true;
}

if ((currentMillis - previousMillis) >= interval) {
Serial.println("FullFULLFULL");
previousMillis = currentMillis;
}

delay(1000);

Serial.println(currentMillis);
Serial.println(previousMillis);
Serial.println(bdistance);


Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Could you edit your post#6 to use code tags?

If you are unable to understand how to use code tags then getting help may be difficult.

And post the complete code, please.

The code tags perhaps?

Your code is very inconvenient to work with, please fix it

Yeah im sorry for that, i'm a bgeinner and it's a scholl project with little to no help from the teachers. I tried to make it better to read if thats what you meant by inconvenient

#include<Servo.h>

Servo servo;
Servo servo2;

int const trigPin = 6;
int const echoPin = 5;
int const btrigPin = 10;
int const bechoPin = 11;

int interval=20000;
unsigned long previousMillis=0;
bool interval_is_running = false;

void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(btrigPin, OUTPUT);
pinMode(bechoPin, INPUT);

servo.attach(3);
servo2.attach(9);

}

void loop() {
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin, LOW);

long timedelay = pulseIn(echoPin, HIGH);
int distance = 0.0343 * (timedelay/2);
  if (distance <=50 && distance >=1) { 
        servo.write(80);  
        servo2.write(140);
        delay(3000); 
    } 
  else {

        servo.write(180); 
        servo2.write(40);
   }
delay(60);

delayMicroseconds(2);
digitalWrite(btrigPin,LOW);
delayMicroseconds(2);
digitalWrite(btrigPin, HIGH); 
delayMicroseconds(2);
digitalWrite(btrigPin, LOW); 


long td = pulseIn(bechoPin,HIGH);
int bdistance = 0.0343 * (td/2);


unsigned long currentMillis = millis();

if (bdistance >=15 && bdistance <=1){
  if (! interval_is_running) 
    previousMillis = currentMillis;
     interval_is_running = true;
   }

if ((currentMillis - previousMillis) >= interval) {
Serial.println("FullFULLFULL");
previousMillis = currentMillis;
}

Serial.println(currentMillis);
Serial.println(previousMillis);
Serial.println(bdistance);

}

your code still not inserted correctly.
Please reread advices in posts #7 and #2

I have moved the topic to the Programming category

I need help to figure out how to make my millis timer work properly, it need to "stop counting" when bdistance is >15 and <1.

#include<Servo.h>

Servo servo;
Servo servo2;

int const trigPin = 6;
int const echoPin = 5;
int const btrigPin = 10;
int const bechoPin = 11;

int interval=20000;
unsigned long previousMillis=0;
bool interval_is_running = false;

void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(btrigPin, OUTPUT);
pinMode(bechoPin, INPUT);

servo.attach(3);
servo2.attach(9);

}

void loop() {
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin, LOW);

long timedelay = pulseIn(echoPin, HIGH);
int distance = 0.0343 * (timedelay/2);
  if (distance <=50 && distance >=1) { 
        servo.write(80);  
        servo2.write(140);
        delay(3000); 
    } 
  else {

        servo.write(180); 
        servo2.write(40);
   }
delay(60);

delayMicroseconds(2);
digitalWrite(btrigPin,LOW);
delayMicroseconds(2);
digitalWrite(btrigPin, HIGH); 
delayMicroseconds(2);
digitalWrite(btrigPin, LOW); 


long td = pulseIn(bechoPin,HIGH);
int bdistance = 0.0343 * (td/2);


unsigned long currentMillis = millis();

if (bdistance >=15 && bdistance <=1){
  if (! interval_is_running) 
    previousMillis = currentMillis;
     interval_is_running = true;
   }

if ((currentMillis - previousMillis) >= interval) {
Serial.println("FullFULLFULL");
previousMillis = currentMillis;
}

Serial.println(currentMillis);
Serial.println(previousMillis);
Serial.println(bdistance);

}

millis() never stops really..

what do you mean exactly?

@fotokoppen
Using a mess of millis and delay logic in one code is a way to fail

I still not understand what is your problem.

is this the same question as

don't double post please.. Merging topics.

1 Like

click the </> icon and paste your code in the highlighted area

bdistance can' t be <1 and >15

i think your logic can be simpler

    unsigned long currentMillis = millis();

    if (1 <= bdistance &&  bdistance <= 15)  {
        if ((currentMillis - previousMillis) >= interval) {
            previousMillis = currentMillis;
            Serial.println("FullFULLFULL");
        }
    }

YEah i was confused where to post

Well basically i have a box with two sensor, one of the open the box with servos(which works). i also wanted to add the function that whenm the box gets full it starts a timer and send a message in serial monitor every 20 seconds, the problem i have is that the "timer" activates immediately instead of starting when the sensor spots something.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.