Newbie here. Help please with understanding a sequence

Dear community as I am a beginner with code can someone please explain a few lines of the following code? :

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

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

void loop() {

  Vo = analogRead(ThermistorPin);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  T = T - 273.15;
  T = (T * 9.0)/ 5.0 + 32.0; 

  Serial.print("Temperature: "); 
  Serial.print(T);
  Serial.println(" F"); 

  delay(500);
}

Why ThermistorPin in parenthesis follows after analogRead? What does this combination analogRead(ThermistorPin) mean?

On the next line why is float in parenthesis? Vo above is declared as an object, so (float) means the value of Vo can change, and by what it will change to, the result of Vo is/will be then, deduct 1.0? Why is float in parenthesis ?

In other words, I understand what T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2)); does, but I am confused by the logR2 = log(R2); because R2 is in parenthesis.

Thank you !

may be you need to read an introduction to C++ so the you get what function calls look like ?

analogRead() is a function call.

The function requires a parameter which is the PIN number to read.

see the doc for analogRead() to see what it returns (which will be stored into the variable Vo).

It's the same type of command as delay(500). here you call the function delay and you pass the parameter 500.

(float) means convert the variable that is after into a floating point value to conduct the math (it's called type casting). In C++ if you do math with variables and numbers that are all integral numbers then the result you get is also a integral number, that it 3 / 2 is not 1.5, it's truncated to 1. but if you had 3.0 / 2 then the first number is a float and the math is done using floating point calculation and you get the expected 1.5.

(here (float) was not necessary as 1023.0 is already a float, so the math was already conducted using floating point maths.)

2 Likes

(1) define the pin... the value 0 (meaning Analog pin 0) replaces the defined integer "ThermistorPin"

(2) read the defined pin Thermistor (A0) and return an integer to store in variable Vo

1 Like

Never be afraid to just check the documentation. This is all explained already.

3 Likes

Thank you all! I really appreciate it !