trouble / confusion with while statement

Hello - I am trying to trigger a relay with a button push and have the relay stay on until a certain amount of millis have passed.

I though the while state would loop within itself until a time is met but it looks like the whole void loop is running. Am I missing how the while statement work?

The code is below.

//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h> 
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <avr/wdt.h>

// OLED display TWI address
#define OLED_ADDR   0x3C

// reset pin not used on 4-pin OLED module
Adafruit_SSD1306 display(-1);  // -1 = no reset pin


// constants won't change. They're used here to set pin numbers:
const int buttonPin = 8;     // the number of the pushbutton pin
const int relayPin =  6;      // the number of the relay  pin
int sensorPin = A0;    // select the input pin for the potentiometer

//OLED A4
//OLED A5

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int sensorValue = 0;  // variable to store the value coming from the sensor
double fill_time = 0.0;
unsigned long time_now = 0;

void setup() {
   Serial.begin(9600); // open the serial port at 9600 bps:
  // initialize the Relay pin as an output:
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);

  //setup display
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);

  // Clear the buffer.
  display.clearDisplay();
 
  pinMode(LED_BUILTIN, OUTPUT); // Declare the LED as an output
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW

}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  float fill_time = (5.0 + sensorValue * ((90.0-5.0)) / 1023.0);
  long int fill_time_ms = fill_time * 1000;
  
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);  

  //write to oled screen
  display.clearDisplay();
  display.display();
  display.setTextSize(2); //128x32 4 lines at 21 charactors
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print(fill_time);
  display.setCursor(96,16);
  display.print("%");
  // update display with all of the above graphics
  display.display();

  // check if the pushbutton is pressed. If it is, the buttonState is LOW:
  if (buttonState == 0) {
    Serial.println(buttonState);
    time_now = millis();
    Serial.print("Time at button press ");
    Serial.println(time_now);
    Serial.print("millis now ");
    Serial.println(millis());
    Serial.print("set milli seconds ");
    Serial.println(fill_time_ms);
    // turn Relay on:
    digitalWrite(relayPin, LOW);
  

    while(millis() < time_now + fill_time_ms){

 // display.setCursor(64,16);
 // display.print("   ");
  

  // update display with all of the above graphics
 // display.display();
      
        //wait approx. fill_time_ms
        
  display.setCursor(64,16);
  display.print(((millis() - time_now) /fill_time_ms)*100);

  // update display with all of the above graphics
  display.display();
  
        }
  }

    // turn Relay off:
    digitalWrite(relayPin, HIGH);
    
  

  if (millis() > 2000000000) {

    //watchdog timer with 15 milli seconds time out
    wdt_enable(WDTO_15MS);
    delay(1000);

  } 

  
}

Your understanding of how while works is correct

while (thisIsTrue)
  {
    //execute the code here
  }

If your while loop is not working as you expect the first thing to do is to print the result of the while condition and any variables that are used to derive it before the while loop is entered.

You are doing that, so do you see what you expected ? I would be particularly interested in the value of fill_time_ms

What does the Serial Monitor display? You have lots of Serial.print() statements in your code that would provide useful information, but we can not see it.

Thank you for the information. I will run some tests

Anthony

Also try changing this

while(millis() < time_now + fill_time_ms)

To this

while(millis() - time_now < fill_time_ms)