if statement

I’m trying to figure out what am I doing wrong with this code I can read the pot value but why it does not switch functions?
I’m trying to log the values after it goes over 500 and under 500

void HighTemp();
void LowTemp();
void tempdisplay();

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

void loop() {
 int sensorValue = analogRead(A0);
 if(sensorValue > 500){HighTemp();}
 else{tempdisplay();}
}

void tempdisplay(){
 int sensorValue = analogRead(A0);
 Serial.print("Temp: ");
 Serial.println(sensorValue);
 delay(500);
  }

void HighTemp(){
  int sensorValue = analogRead(A0);
  Serial.println("High Temp");
  delay(1000);
  while(1){
    if(sensorValue < 500){LowTemp();}  
    else{tempdisplay();}
    }
  }
void LowTemp(){
  int sensorValue = analogRead(A0);
  Serial.println("Low Temp");
  delay(1000);
  while(1){
    if(sensorValue > 500){HighTemp();} 
    else{tempdisplay();}
  }
  }

I would suggest you throw away the two

while(1){
if(sensorValue < 500){LowTemp();}
else{tempdisplay();}
}

bellow HighTemp() and BellowTemp(). The If (sensorValue… bla bla bla bellow loop() already do that job…

While your code is in this loop...

  while(1){
    if(sensorValue > 500){HighTemp();} 
    else{tempdisplay();}
  }

how does it ever exit the loop? Yes, you call HighTemp() but most functions are supposed to return, so you're still in the loop.

Additionally, how would sensorValue change while in the loop, if you wanted to use that as a method of exiting? You don't use analogRead() again while inside the loop so the value can never change.

@AntonioTesta if I get rid of the two functions how I would able to call "High Temp" and "Low Temp" I need to call that once every time it switch from one to the other.

@MorganS once it exit that loop it would get stuck in the other while loop, I don't understand how it gets back to the same loop. I added the sensor value in inside each function because I got the error "'sensorValue' was not declared in this scope"

He wasn't talking about getting rid of the functions. It's the while(1) structure he was talking about getting rid of. Anytime you catch yourself writing while(1) slap yourself in the face and think, do I really really want to just lock up the code here? The answer to while(1) is almost always going to be NO. As you have it now those are completely buggering up the program flow and that is the root of your problem.

To the second point, just because it cleared the error and got you to compile doesn't mean it was right. You need to read up on "C++ variable scope". Google that. Then you'll see what to do with sensorValue.

@Delta_G that’s what I’m trying to understand how to only print “High Temp” and “Low Temp” once and not every time is > or < than 500.
if I remove the while(1) it would print many times I only want to print once when it pass 500

Record in a variable that you've passed 500 and printed the text. Then if it's still above 500 and that variable is true, don't print.

@MorganS Thank you!

void HighTemp();
void LowTemp();
void tempdisplay();
bool tempsetting = true;

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

void loop() {
 int sensorValue = analogRead(A0);
 if(sensorValue > 500 && tempsetting == true){HighTemp();}
 if(sensorValue < 500 && tempsetting == false){LowTemp();}
 else{tempdisplay();}
}

void tempdisplay(){
 int sensorValue = analogRead(A0);
 Serial.print("Temp: ");
 Serial.println(sensorValue);
 delay(500);
  }

void HighTemp(){
  Serial.println("High Temp");
  tempsetting = false;
  delay(1000);
  }

void LowTemp(){
  Serial.println("Low Temp");
  tempsetting = true;
  delay(1000);  
  }

Why do you read the sensor everywhere in your code. Usually one is interested in the value at a given moment.

Worst case your reading changes between the read in loop() and the read in your functions. Passing the value that you read in loop() to the functions would be a better approach.

 if(sensorValue > 500 && tempsetting == true){HighTemp();}

This would be much easier to read as

if(sensorValue > 500 && tempsetting == true)
  {
    HighTemp();
  }

It makes it much more obvious what code will be executed if the test returns true

scancool: @MorganS Thank you!

void HighTemp();
void LowTemp();
void tempdisplay();

What is this? Looks like some sort of empty functions to me, but 'real' functions comes later in the code.

What is this?

Function prototypes/declarations. Not usually explicitly needed in the Arduino environment as the IDE adds them to your code behind the scenes when it is compiled.

Gabriel_swe: What is this? Looks like some sort of empty functions to me, but 'real' functions comes later in the code.

A little more detail.

The C/C++ compiler needs to know about functions before you can use them in your code; e.g. which parameters to expect.

The Arduino environment first loops throigh the code you wrote and modifies it before compiling; it e.g. adds the prototypes (see reply above).

This would be a proper sequence

// prototype
void someFunc(int x);

void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
  someFunc()
}

void someFunc(int x)
{
  // toggle led
  digitalWrite(13, digitalRead(13));
}

Try to compile the above without the someFunc prototype in a normal environment (not Arduino) and it will slap you around the ears with an error that it does not know about someFunc().

The alternative is to write the someFunc function before were it's used first.

void someFunc(int x)
{
  // toggle led
  digitalWrite(13, digitalRead(13));
}

void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
  someFunc()
}

You can also have a look at the actual include files (e.g. HardwareSerial.h) and you will find the prototypes in there; the actual implementation of the functions (methods) is in HardwareSerial.cpp.