While loop doesn't stop when i change the serial input

Hello, i'm kinda new to arduino programming and can't seem to find the problem here.

So i have a temperature sensor that is connected to my Arduino UNO. It should start sending the temperature when i type in "start" in the serial port, and it does.
But when i type in "stop" it's supposed to stop and it doesn't. The while loop doesn't seem to stop and it infinitely goes on and keeps printing the temperature every second.

Does anyone know how to make it work as i don't see my mistake?

float tempData;
float tempC;
float tempF;
int tempPin = A0; //A0
int sampleTime = 1000; //1000 ms = 1s

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

void loop()
{

  if(Serial.available()){
    int state;
    String input;
    input = Serial.readString();
    input.trim(); 
    
    if (input == "start") {
        state = 1;

    if (input == "stop"){
        state == 0;
    }
    while (state == 1){

      tempData = analogRead(tempPin);
      tempC = (tempData * 5.0 / 1024.0) - 0.5;
      tempC = tempC / 0.01;

      Serial.print(tempC);
      Serial.println(" °C");

      tempF = tempC * 9/5;
      tempF = tempF + 32;

      Serial.print(tempF);
      Serial.println(" °F");
      delay(sampleTime);
      
      } 
    }
  }
}

Ouch, I see a while-statement.

The "Arduino way" is to let the loop() run over and over again as many times as possible. Keep that in mind. The more responsive and the more things you want to do, the more the loop() should run.

A while-statement to keep doing something or to wait a long time is often not needed. In your sketch, you even don't bail out of the while-statement.

Can you test the "start" and "stop" first ? Send a message to the serial monitor when "start" or "stop" was received.

When something should run on its own and it should be turned on and off, then it has to be at the main level of the loop().

bool enableSampling = false;

void setup()
{
 ...
}

void loop()
{
 ...

 if( input == "start")
 {
   enableSampling = true;
 }
 else if( input == "stop")
 {
   enableSampling = stop;
 }

 if( enableSampling)   // at the main level in the loop()
 {
   // Take one sample with analogRead(), use delay() or millis() to do it once per second
   // Be sure to take just one sample and next time the loop() runs, take the next sample.
 }
}

I see a couple of errors:
*: missing closing curly brace in line 23;
*: one curly brace too many in line 43;
*: "state == 0;" in line 25 will not work as expected;
*: "input = Serial.readString();" in line 18 will never be executed after the while() loop in line 27 was entered, because the only way this loop will exit is when state becomes 0, which will never happen because "input = Serial.readString();" is never executed, etc., etc... :wink:

Koepel:

   enableSampling = stop;

I guess you meant "enableSampling = false;"? :wink:

I've tried what you told me Koepel and it works! And as you said Erik it was just missing the "false", thank you both!

if (input == "stop"){
        state == 0;
    }

Oops!

if (input == "stop"){
        state = 0;
    }