Problem interfacing TMP36 sensors with LEDS

Hii! There is a probleme that I have to solve:

I have to use 4 ‘‘TMP36’’ and 3 LEDS. I have 4 appartments and one temperature for each one. My TMP sensor will read thease temperatures with this function: T (oC) = (((analogValue*5)/1024) - 0.5)*100

The function which calculates the average temperature is Τ = x1*T1 + x2*T2 + x3*T3 + x4*T4 ,
in which x1,x2,x3,x4 are by default x1 = 0.1, x2 = 0.3, x3 = 0.5, x4 = 0.1

Afterwards
if T<=20 then LEDS OFF
if T>20 && T<=30 then one LED ON
if T>30 && T<=40 then two LEDS ON
if T>40 then tree LEDS ON

Hii! There is a probleme that I have to solve:

I have to use 4 ‘‘TMP36’’ and 3 LEDS. I have 4 appartments and one temperature for each one. My TMP sensor will read thease temperatures with this function: T (oC) = (((analogValue*5)/1024) - 0.5)*100

The function which calculates the average temperature is Τ = x1*T1 + x2*T2 + x3*T3 + x4*T4 ,
in which x1,x2,x3,x4 are by default x1 = 0.1, x2 = 0.3, x3 = 0.5, x4 = 0.1

Afterwards
if T<=20 then LEDS OFF
if T>20 && T<=30 then one LED ON
if T>30 && T<=40 then two LEDS ON
if T>40 then tree LEDS ON

So, what's your problem? Looks to me like you've solved it!

Ok, what is the problem? Post your code, perhaps we can help.

REPORT TO MODERATOR is not a REPLY option.

Bob.

Thank you for answering!
Here is my code, in serial monitor the exit is always zero.

/Temperature Reader/

int tempVal1;
int tempVal2;
int tempVal3;
int tempVal4;

void setup()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
Serial.begin(9600);
}

void loop()
{
tempVal1=analogRead(1);
tempVal2=analogRead(2);
tempVal3=analogRead(3);
tempVal4=analogRead(4);

float x1=0.1;
float x2=0.3;
float x3=0.5;
float x4=0.1;

float T1=(tempVal15/1024-0.5)/100;
float T2=(tempVal2
5/1024-0.5)/100;
float T3=(tempVal35/1024-0.5)/100;
float T4=(tempVal4
5/1024-0.5)/100;

float T=x1T1+x2T2+x3T3+x4T4;

Serial.print(T);
Serial.print(“C”);
Serial.println();
delay(1000);

if (T<=20)
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
}
else if (T>20 && T<=30)
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
}
else if (T>30 && T<=40)
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
}
else
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
}
}

Thank you for answering!
Here is my code, in serial monitor the exit is always zero.

/Temperature Reader/

int tempVal1;
int tempVal2;
int tempVal3;
int tempVal4;

void setup()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
Serial.begin(9600);
}

void loop()
{
tempVal1=analogRead(1);
tempVal2=analogRead(2);
tempVal3=analogRead(3);
tempVal4=analogRead(4);

float x1=0.1;
float x2=0.3;
float x3=0.5;
float x4=0.1;

float T1=(tempVal15/1024-0.5)/100;
float T2=(tempVal2
5/1024-0.5)/100;
float T3=(tempVal35/1024-0.5)/100;
float T4=(tempVal4
5/1024-0.5)/100;

float T=x1T1+x2T2+x3T3+x4T4;

Serial.print(T);
Serial.print(“C”);
Serial.println();
delay(1000);

if (T<=20)
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
}
else if (T>20 && T<=30)
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
}
else if (T>30 && T<=40)
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
}
else
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
}
}

@margetik

Could you take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

It's because you're mixing integer and floating-point arithmetic, eg (5/1024) is dividing integer 5 by 1024, giving 0. Convert or cast everything to floating point. for your constants, use eg (5.0/1024.0).

Use "float tempVal1" etc. instead if "int".

Not sure if this is absolutely necessary, but to be certain (and make it obvious what you are doing), cast the result from analogRead(), eg "tempVal1= (float) analogRead(1)" etc.

Thanks a lot johndg, I solved it!!

I think my code's problem is because i had mixed integer and floating-point arithmetic, even so thanks for answering!

margetik:
Thanks a lot johndg, I solved it!!

Welcome. Bit of karma would be a nice Christmas present - thanks in advance! (Have a good one, and stay safe).

Thanks a lot johndg, I solved it!!

I think you mean :

Thanks a lot johndg, YOU solved it!!

:slight_smile:

karma indeed, nice Christmas and happy new year you too : )

margetik:
karma indeed .... : )

... and you click where it says [add]! :slight_smile:

I guess the word ‘obvious’ is relative.

Please modify your post and add code tags around the code.

margetik:
I think my code's problem is because i had mixed integer and floating-point arithmetic, even so thanks for answering!

So you fixed it? If so, please post your corrected code so that other beginners can learn from your error, and so that experienced forum members do not waste time trying to help after the problem is fixed.

I was going to suggest

  float T1=(tempVal1*5.0/1024-0.5)/100;
  float T2=(tempVal2*5.0/1024-0.5)/100;
  float T3=(tempVal3*5.0/1024-0.5)/100;
  float T4=(tempVal4*5.0/1024-0.5)/100;

But maybe you already did that?

Duplicate topics moved to the same forum section and merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.