Sketch won't compile, and I can't see the problem.

This NOOB trying to use Arduino to make pc fan cooling system for PS3 and a Nighthawk router. Main
goal is to have a LED indicator light go on , and a cooling fan turn on when degreesC > 32. I'm a very frustrated NOOB. Here is my sketch. I've looked everywhere, can't find an answer. Any help would be appreciated. Thanks.

const int tempPin=0;
const int LEDPin=13;
const int fan1=3;
const int degreesC;

void setup() {
Serial.begin(9600);
pinMode(LEDPin,OUTPUT);
pinMode(fan1,OUTPUT);

}

void loop() {
float voltage,degreesC;
voltage = getVoltage(tempPin);
degreesC =(voltage-0.5) * 100.0;

Serial.print("voltage: ");
Serial.print(voltage);
Serial.print(" deg C: ");
Serial.println(degreesC);
delay(1000);

}
float getVoltage(int pin)
{
return(analogRead(pin) * 0.004882814);
if{ (degreesC > 32)
digitalWrite(LEDPin,HIGH);
digitalWrite(fan1,HIGH);
}
else{
digitalWrite(LEDPin,LOW);
digitalWrite(fan1,LOW);
}
}

What is the error that you are receiving?

const int tempPin=0;
const int LED=13;
const int fan1=3;
 



void setup() {
 Serial.begin(9600);
 pinMode(LED,OUTPUT);
 pinMode(fan1,OUTPUT); 

}

void loop() {
 float voltage,degreesC;
 voltage = getVoltage(tempPin);
 degreesC =(voltage-0.5) * 100.0;
 
 Serial.print("voltage: ");
 Serial.print(voltage);
 Serial.print(" deg C: ");
 Serial.println(degreesC);
 delay(1000);
 
}
 float getVoltage(int pin)
 {
 return(analogRead(pin) * 0.004882814);
    if (degreesC > 32){
        digitalWrite(LED,HIGH);
        digitalWrite(fan1,HIGH);
     }
   
    else{
        digitalWrite(LED,LOW);
        digitalWrite(fan1,LOW);
     }
 }

Have you included all of the necessary libraries?

 float getVoltage(int pin)
 {
 return(analogRead(pin) * 0.004882814);
 if (degreesC > 32){

A return does exactly that. Nothing else is executed.

Please read the posting guidelines.

The: if (degreesC >32) statement gets a was not declared in this scope. Error message "degreesC' was not declared in this scope" to be exact. How can I restucture code so it works? I believe no libraries are needed for this sketch? The TMP 36 is a very accurate sensor. This is the first time I've used a forum.

Problem 1: code in grey will never execute, because return passes control back to the calling function.

float getVoltage(int pin)
{
return(analogRead(pin) * 0.004882814);
if (degreesC > 32){
digitalWrite(LED,HIGH);
digitalWrite(fan1,HIGH);
}
else{
digitalWrite(LED,LOW);
digitalWrite(fan1,LOW);
}
}

Problem 2: you are accessing degreesC outside of scope. If you need that variable to be visible from everywhere, declare it as global variable.

Okay, I made a few changes to the sketch, see above. I globally declared const int degreesC;. Then I isolated the if statement by moving a beginning curly bracket. Probably did that wrong. Now when I go to compile the sketch. I get an error message of "uninitialized const 'degreesC' [-fpermissive]". Did I declare degreesC improperly?

Instead of writing random nonsense and expecting it to work, you might save time if you got an elementary book about C/C++ coding, or an online tutorial, and learned how to write simple functions correctly.

degreesC is a float variable. It is not a constant. You didn't show how you declared it in reply #7 of this thread, but I am guessing that you might have something like

const int tempPin=0 ;
const int fanpin=3 ;
const float degreesC ;

If you wrote this, then you made degreesC a float and a const. It isn't a const. It's a variable which will be often calculated and changed by your measurements. Remove that keyword.

Also, you seem to be using Serial.print( ), and also getting your temperature from pin 0. I'd suggest using pin A0 for the analog reading, so " const int tempPin = A0 ". You can't use the same pin for two things. I would suggest not relying on the compiler to figure out that your analogRead( ) from "pin 0" is actually going to be from pin A0.

I see you changed the original post.

degreesC is not a const. It probably shouldn't be an int, either.

Delta_G:
Not only is that exactly what the compiler does for analogRead, it is also exactly what the documentation for that function recommends. When you call analogRead with a pin number, it knows that you mean the analog pin by that number. It is only when using an analog pin as a digital pin that you need to keep up with the difference.

Is that always the case, when the number goes through several layers of functions ? He is not invoking the analogRead( ) function directly using the const int which he declared at the top of the sketch.

And also, there is no shortage of "noobs" who don't realize that the Serial uses pins 0 and 1, and they try to do other things with those pins, and complain that it doesn't work.