How to loop a section of code under the loop branch only once?

Hi, I’ve been struggling with some code lately. Just like I mentioned in my question, I want to loop a single section of code under the loop() block only once. I have done my research and am aware of the two common ways to do this. One is to put the code I want to run once in the setup() block and the second being using the while(1) statement in the loop() block. Unfortunately, both ways are not suitable for my code. The first reason is it is compulsory for my code to be in the loop() section. I cannot use the second option as all the code under the loop() block ends up running once. Like I said before, I want only a section of code in the loop() block to run once.

For your information, the purpose of this code is to display in an LCD, the amount of mililetres left for the user to drink for a healthy water consumption. For example, a person should drink min 1800ml a day for proper hydration. If the user drinks 123ml of water, the lcd should display (1800-123)

//Adds the liquid crystal library 
#include <LiquidCrystal.h>

//defines lcd pin numbers
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// defines ultra sonic sensor pin numbers
const int trigPin = 8;
const int echoPin = 7;

// defines variables
long duration;
long volume;
long interval = 3600000; //1 hour
unsigned long stime = millis();
double pdist = 0;
double cdist = 0; 
double mcons = 4.5; //128ml;

void setup() {
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
Serial.begin(9600); // Starts the serial communication 
lcd.begin(16, 2); // Starts the lcd communication 
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

//Converting the distance to cm
double cdist = duration / 29 / 2;

 //Finding volume of the water
double volume_of_rem_height = 3.14*3*3*(cdist);
Serial.println(1800-volume_of_rem_height);

//I WANT ONLY THE BELOW STATEMENT TO BE RUN ONCE. REST ALL SHOULD CONTINUE TO LOOP.
lcd.print(1800-volume_of_rem_height);

You're going to have to isolate the one-time code somehow. You could put it in a function which is only called once or inside an if() whose condition is only true once or, even a for() which only loops once (an oddity).

The way to do what you're asking is to use a boolean variable, initially false. Check whether it's true before you do the lcd display part and if it is, don't display anything and set it true.

I doubt that's really what you need though. How about checking if the value has changed and only if it has, display on the LCD?

Hi

Try the following:-

At the start of the program declare a boolean variable and set it to false:-

boolean displayed = false;

Then in your loop put the following:-

      if (!displayed) {
         lcd.print(1800-volume_of_rem_height);
         displayed = true;
      }

This will only run once.

If you want to update the display e.g. because more liquid has been drunk then just set “displayed” to false and the next time through the loop the display will change to the new value.

Ian

he second being using the while(1) statement in the loop() block.

That is absolutely not the way to do it.

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

The while loop will not end under normal circumstances because the condition will always be true. Let loop() keep running freely

Take Bill's advice and use a boolean variable set to true to flag the fact that the "one time" code has run and only run it when the boolean is false

I have tried what Ian Crowe had suggested. I changed displayed to 'false' because I do need to change the value as the person drinks more water. The value does change, but the value keeps repeating on the LCD. For eg, if the value is 123, this is what gets displayed on the LCD: 123123123123123123..... and so on. Is there a way to avoid this? So, I can get '123' displayed only once on the LCD?

P.S. Is there a way I can reply to each one of you without having to make another post?

@wildbill Thanks for your suggestion. Do you think you can guide me on how to display the value on the LCD only if the value has changed?

DevikaSujith:
I have tried what Ian Crowe had suggested. I changed displayed to 'false' because I do need to change the value as the person drinks more water. The value does change, but the value keeps repeating on the LCD. For eg, if the value is 123, this is what gets displayed on the LCD: 123123123123123123..... and so on. Is there a way to avoid this? So, I can get '123' displayed only once on the LCD?

P.S. Is there a way I can reply to each one of you without having to make another post?

I haven't used the LiquidCrystal library yet, but it seems this might work:

lcd.clear();
lcd.print(1800-volume_of_rem_height);

@evanmars and @IanCrowe Thank you so much! It works perfectly. You guys are great.

I haven't used the LiquidCrystal library yet, but it seems this might work:

That will clear the screen and print new text. But, it may be the same text that was just erased.

To print data only when there is a change, you need to detect that a change occurred. To do this you MUST keep track of the value when it was printed.

int currValue;
int prevValue = -1;

void loop()
{
   currValue = howMuchHasUserDrunk();
   if(currValue != prevValue)
   {
      printValue(currValue);
   }
   prevValue = currValue;
}`