Call a method when receiving a string value from c# code

I have the following code that calls a function when a<3. What i want to do and i can't find the solution is to call this function when receiving a string value from c#.

Arduino code:

int ThermistorPin = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
int a = 2;

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

void loop() {
if (a<3)
  tempvalue();
else
  Serial.print("eeee");
  delay (1000000);
}

void tempvalue () { 
  Vo = analogRead(ThermistorPin);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  Tc = T - 273.15;
  Tf = (Tc * 9.0)/ 5.0 + 32.0; 
  Serial.println(Tc);
  delay(10000);
}

How is C# communicating with the Arduino? By writing to the Arduino’s serial port ?
If so, your loop has to check if there is anything in the serial buffer an if so call the required function. Look at Serial.available() and Serial.read().

So rather than "if (a<2) " what should take its place?

Start by saying how the C# is communicating with the Arduino and what data it is transferring (if any).
Post your C# code. I am anyway curious to see it.
The way you have structured your C++ code at the moment, tempvalue() will be called on every loop() iteration because the condition a<3 will always be true as the value of a is always 2.

I will post the code in a while. As concerning the a value i was changing it to test it that is why it looks like that. I will be removed anyway because i want to cal that function only if it gets a string value from c# rather than that if loop with the a variable.

AntonisPlb:
So rather than "if (a<2) " what should take its place?

As each character is received it must be added to the string until it is complete. How will the Arduino know that it is complete ?

Have a look at Serial input basics - updated

Once the string has been received then the strcmp() function can be used to compare it to a fixed string.

Note that this refers to strings not Strings

Thanks for the information i will give this a try!

This is my updated code. I am printing the Tc at the showNewData function when there is connection between c# and arduino.

int ThermistorPin = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

char receivedChar;
boolean newData = false;

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

void loop() {
recvOneChar();
showNewData();
}

void recvOneChar(){

  if(Serial.available()>0){
    receivedChar=Serial.read();
    newData = true;
    
  }
}

void showNewData () { 
  if(newData == true){
  Vo = analogRead(ThermistorPin);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  Tc = T - 273.15;
  Tf = (Tc * 9.0)/ 5.0 + 32.0; 
  Serial.println(Tc);
  delay(10000);
  }
}

If there is anything that need to be improved or changed please let me know

Well, on first glance it looks OK, but does it work as expected ?

I am waiting for the c# code from a friend of mine because we are working on a group project. Once we run it I will update the post. Thanks for the help!

May I suggest that you don't use single character variable names. It is impossible to do a search to find all the instances because every similar character will be included.

Long meaningful variable and function names generally make a program much easier to understand and less likely to have silly mistakes.

...R

AntonisPlb:
This is my updated code. I am printing the Tc at the showNewData function when there is connection between c# and arduino.

If there is anything that need to be improved or changed please let me know

As written there is, of course, no test for what has been received nor any method of knowing that the message received is complete and the showNewData() function will be called for every new character

AntonisPlb:
This is my updated code. I am printing the Tc at the showNewData function when there is connection between c# and arduino.

Your system will be much more reliable if you use start- and end-markers for your messages so that the Arduino can be sure it has received the full message.

I had the impression from your Original Post that you want to receive a message containing several characters so I don't understand why you have chosen the recvOneChar() function.

...R

I changed my mind because it would be much easier for us if we received a single character rather than multiple ones

AntonisPlb:
I changed my mind because it would be much easier for us if we received a single character rather than multiple ones

Even if you use single character commands (which I recommend) it would be a good idea to send them with start- and end-markers - for examples ""

...R