Having a function run only once in loop function

Here is the sketch I have now:

/* *Skectch to find the the area of the wall *Inputs will be the length and height of the wall *Will determine the distance to set the module off the wall */

long length=0; int totallength;

void setup() { Serial.begin(9600); Serial.println("Enter Today's Date to Start Program"); }

void loop() { getanswer(); askquestion(); }

void askquestion() { Serial.print("Overall length of the wall: "); }

long getanswer() { if(Serial.available()) { char ch=Serial.read(); if(isDigit(ch)) //is the ascii number between 0 and 9? { totallength=(totallength*10)+(ch-48); } else if(ch==10) { length=totallength; Serial.println(length); totallength=0; } } }

I'm wanting the askquestion() function to only show once when I run the program so i can use the getanswer() function immediately after. But i can't get it do that.

Any suggestions?

bool asked = false;

void loop()
{ 
  if(!asked)
  {
     askquestion();  
     asked = true;
  }
  getanswer();
}

By the way, getanswer() is missing a return statement. It, therefore, doesn't actually return anything, despite the return type being long.

That helped a lot!! I really appreciate it!

Can I use that same format for other functions as well?

Also, about the return statement, would I just say "return length"

Can I use that same format for other functions as well?

Certainly.

Also, about the return statement, would I just say "return length"

Yes, but... Returning a global variable from a function is rather silly. Instead, you should have:

length = getanswer();

in loop(), and

return totallength;

in getanswer();

totallength should be set to 0 at the start of the function, not at the end, and totallength should NOT be global.

Okay cool thanks!!!

While I’m thinking about it, it might be super easy or not possible at all I’m just not sure.

Is it possible to see if a function has happened yet?

So in the loop(), would it be possible to see if the getanswer() function has happened yet?

Yes in exactly the same way. Set a global variable to false and in the function set it to true. If you look at it and it says true then you know that function has been called at least once.

So in the loop(), would it be possible to see if the getanswer() function has happened yet?

You could test the current value of length against the initial value:

if(length != -1)
   // A length has been entered (assuming that the default value of length is -1)

Or, you could set a flag:

bool answered = false;

and set that to true in the getanswer() function. Then,

if(answered)
   // Got an answer

long length;

void setup() { Serial.begin(9600); }

void loop() { length=getanswer(); Serial.print(length); }

long getanswer() { if(Serial.available()) { int totallength; totallength=0; char ch=Serial.read(); if(isDigit(ch)) //is the ascii number between 0 and 9? { totallength=(totallength*10)+(ch-48); } else if(ch==10) { return totallength; } } }

So something like this for the return totallength and not making it a global variable?

So something like this for the return totallength and not making it a global variable?

Something along those lines, yes. But, consider how many characters you will read using that function. There is no looping, so the maximum will be one character. That will limit your length values to 0 to 9.

If that’s OK, you are done. If not, you need to think about what defines the end of an answer. Notice that when we write, we use spaces to end words and periods to end sentences. When reading, these visual cues provide a lot of useful information. Visual cues are useless to an Arduino, which can’t see them.

If the data is being sent from the Serial Monitor, it has the ability to append additional characters, like carriage return or line feed, or both, to what you type.

You can make your function loop as long as there is data to read, by replacing the if with a while statement. But, that is not sufficient. You need to wait for serial data to arrive, because serial data transmission is ssslllooowww.

You can add a condition to the while statement to make it loop for a period of time waiting for serial data, or, you could use Serial.parseInt() instead, which handles the timeout, multiple characters, negative numbers, etc.

I really appreciate it!!!

Or you could just put the call to the askquestion() function in setup()... Just saying...