Wrong Data Sending

hi I programming with arduino nano. I write a analog sensor application. This is my code.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
      if (Serial.available() > 0) {
    int thisChar = Serial.read();
    switch(thisChar){
      case'1':
      analog();
      break;
      }
    
}
void analog() {
  int analogc = analogRead(A1);
  delay(10);
  Serial.println("Data=");
    delay(10);
  Serial.print(analogc); 
 }

When I sending 1 The console getting only Data= It not getting analogc value. And Im sending again 1. And console Data= 45Data= And sending again it is contunie same. And It getting old value. Example when second sending 1. It getting first analogc value.

Hmmm.

int thisChar

Why do you have delays in there? Take them out.

  Serial.println("Data=");
    delay(10);
  Serial.print(analogc);

I think this should be;

  Serial.print("Data=");
    delay(10);
  Serial.println(analogc);

In the code as you have it, it isnt sending the line end character (the ' ln ' bit in println) until after loop has gone round one more time, so the line doesn't come out on the monitor when you expect it

MB.

Actually, what i would do is this;

  Serial.print("Data=");
  Serial.println(analogc);
  delay(10);

selimerkan, either that isn't your code, or you haven't actually run it, as it will not compile. Specifically, you are missing a right curly brace at the end of loop(). When you post code, you should first get it to compile, then use CTRL+t to format it, then copy it from the IDE (CTRL+a, CTRL+c), then paste it here between code tags.

Adding the missing curly brace allows it to compile.

Now, did you edit the code in the code you posted? I ask because it does print the output (though probably not in the way you wanted to).

It gives me:

Data= 397

If you did change your code, it's not really a good idea to modify it in the code you first posted, because folks who are trying to help think that code is the code you had a problem with.

MarkBatchelour: In the code as you have it, it isnt sending the line end character (the ' ln ' bit in println) until after loop has gone round one more time, so the line doesn't come out on the monitor when you expect it

The Serial monitor (or any other terminal program) will not require a line ending character. See my output in the previous post,

Now, did you edit the code in the code you posted?

Yes she/he/they did.

LarryD:

Now, did you edit the code in the code you posted?

Yes she/he/they did.

We used to refer to a person as s/he/it

HazardsMind: Hmmm.

int thisChar

Why do you have delays in there? Take them out.

Thanks man I solve it with removing delay. Thanks for ur help.