Arduino serial communication

Ok... So here is my problem: I want to make a temperature monitoring application. Now, I am using an LM35DZ which is directly calibrated in deg. Celsius (10mV/C). What I want to do is send from my PC, via serial comm, a command to arduino that will make the conversion from C to F or K. This is the code that I got so far.

int inPIN = 0;
float v = 0;
unsigned long time = 0;
float t=0;

void setup() {
  Serial.begin(9600);
  pinMode(LEDPIN, OUTPUT);
}

void loop() {
  // send data only when you receive data:
        inPIN = analogRead(A0);
          time=millis();
          v = (float)inPIN/204.6;
          
 // if (Serial.available() > 0) 
 // {
    int incomingState = Serial.read();
    if(incomingState == '1')
      {
        t=(100*v)*9/5+32;
      }
     if(incomingState == '2')
     {
       t=(100*v)+273.15;
     }
    if(incomingState == '3')
     {
       t=v*100;
     }
 // }
  Serial.println(t, DEC);
  Serial.println(time, DEC);

}

Ok. So, this is the code. The code actually works but if I press 1 for example to convert to F, it just repeats the value it converted at the moment the command was processed. Here is what I get:

39756
25.4154434204
39779
25.4154434204
39801
25.4154434204
39825
25.4154434204
39847
25.4154434204
39870
25.4154434204
39894
25.4154434204
39916
25.4154434204
39939
25.4154434204
39962
25.4154434204

My question is: how do I do this conversion and output the correct values? I hope I was clear...

If there is no data in the serial buffer Serial.read() returns -1. Therefore, except when you actually press the key, incomingState is being set to -1, and t is never re-computed.

Perhaps you just want

if (Serial.available() > 0) incomingState = Serial.read();

Though you probably want some checking to preserve incomingState if the user presses something other than 1, 2, or 3; otherwise you have the same problem.

Actually, you pretty much have it - just uncomment a few lines and move the print functions inside the loop:

int inPIN = 0;
float v = 0;
unsigned long time = 0;
float t=0;

void setup() {
  Serial.begin(9600);
  pinMode(LEDPIN, OUTPUT);
}

void loop() {
  // send data only when you receive data:
        inPIN = analogRead(A0);
          time=millis();
          v = (float)inPIN/204.6;
          
 if (Serial.available() > 0) 
  {
    int incomingState = Serial.read();
    if(incomingState == '1')
      {
        t=(100*v)*9/5+32;
      }
     if(incomingState == '2')
     {
       t=(100*v)+273.15;
     }
    if(incomingState == '3')
     {
       t=v*100;
     }
  Serial.println(t, DEC);
  Serial.println(time, DEC);
  }

}

Oh my god... it was so simple :D Thank you Professor Chaos. Your reply made me think and look again at the code from a different point of view. :D

And wildbill was right, the code was there already. I just had to look again. Thanks for the replies :).

The actual code that works... missed a variable declaration at the beginning and ... a } in another place. I marked the changes in the code. I will post the code, who knows, maybe someone will need the info.

int inPIN = 0;
float v = 0;
unsigned long time = 0;
float t=0;
int in=0; //this is one line I had to add...
void setup() {
  Serial.begin(9600);

}

void loop() {
  // send data only when you receive data:
        inPIN = analogRead(A0);
          time=millis();
          v = (float)inPIN/204.6;
          t=v*100; //added this line so it is on Celsius by default
 if (Serial.available() > 0) 
 {in = Serial.read();} //here is another modification
    if(in == '1')
      {
        t=(100*v)*9/5+32;
      }
     if(in == '2')
     {
       t=(100*v)+273.15;
     }
    if(in == '3')
     {
       t=v*100;
     }

  Serial.println(t, DEC);
  Serial.println(time, DEC);

}