Simple code, must be a simple fix! need help!!

Hey guys I am very new to writing codes. :slight_smile: and Im becoming hooked.....

I am using a simple method to control my home hvac with relays and the arduino uno. The code works fine without the temperature portion of it, and the temperature portion works fine on its own. When I combine the two in the same code I receive the error code "'getVoltage' was not declared in this scope"..
Any help would be great. I plan to slowly build on what I learn and eventually be able to control these relays via the internet.

The temp purpose was the show the serial monitor the ambient temp then eventually I would have these relays be controlled by the ambient temp..

Thanks for reading!

float SUPPLY_VOLTAGE = 5.0;
int tempPin = 0;
int w = 7;
int g = 9;
int y = 8;
int BAUD_RATE = 9600;


void setup(){
  
  pinMode (w, OUTPUT);
  pinMode (g, OUTPUT);
  pinMode (y, OUTPUT);
  Serial.begin(BAUD_RATE);
}

  void loop(){
    {
      float temperature = getVoltage(tempPin);
      temperature = (temperature - .5) * 100;
      Serial.print(temperature);
      Serial.println(" C");
      delay(1000);
    }
    float getVoltage(int pin){
      return (analogRead(pin) * .004882814);
    }
  
 
  
   if (Serial.available() > 0){
    int command = Serial.read();
    
    if(command == '1'){
      digitalWrite(w, HIGH);
      Serial.println("HEAT ON");
      delay(4000);
      digitalWrite(g, HIGH);
    
    }else if (command == '2'){
      digitalWrite(w, LOW);
      Serial.println("HEAT OFF");
      delay(4000);
      digitalWrite(g, LOW);
      
    }
        
        if(command == '3'){
      digitalWrite(y, HIGH);
      digitalWrite(g, HIGH);
      Serial.println("A/C ON");
    }else if (command == '4'){
      digitalWrite(y, LOW);
      digitalWrite(g, LOW);
      Serial.println("A/C OFF");
    }
 
    
   
  }
}

Disregard the poll......

You have declared your function getVoltage inside loop. clean up your braces & move it outside.

Also please go back to your original post, click on "modify", then highlight the code, click on the # icon on the editor's toolbar, then click on "save".

Thank you. I have the getVoltage out of the main loop. Now its saying error: expected unqualified-id before 'if'....(the 'if' statement right after the temp loop. A bracket problem?...

No, you need to post your updated code

float SUPPLY_VOLTAGE = 5.0;
int tempPin = 0;
int w = 7;
int g = 9;
int y = 8;
int BAUD_RATE = 9600;


void setup(){
  
  pinMode (w, OUTPUT);
  pinMode (g, OUTPUT);
  pinMode (y, OUTPUT);
  Serial.begin(BAUD_RATE);
}

  void loop()
  {
      float temperature = getVoltage(tempPin);
      temperature = (temperature - .5) * 100;
      Serial.print(temperature);
      Serial.println(" C");
      delay(1000);
    }
    
    float getVoltage(int pin){
      return (analogRead(pin) * .004882814);
    }
    
    
  
  
  
    if (Serial.available() > 0){
    int command = Serial.read();
    
    if(command == '1'){
      digitalWrite(w, HIGH);
      Serial.println("HEAT ON");
      delay(4000);
      digitalWrite(g, HIGH);
    
    }else if (command == '2'){
      digitalWrite(w, LOW);
      Serial.println("HEAT OFF");
      delay(4000);
      digitalWrite(g, LOW);
      
    }
        
        if(command == '3'){
      digitalWrite(y, HIGH);
      digitalWrite(g, HIGH);
      Serial.println("A/C ON");
    }else if (command == '4'){
      digitalWrite(y, LOW);
      digitalWrite(g, LOW);
      Serial.println("A/C OFF");
    }
    }

Now you've moved pretty much everything out of loop. Try this:

float SUPPLY_VOLTAGE = 5.0;
int tempPin = 0;
int w = 7;
int g = 9;
int y = 8;
int BAUD_RATE = 9600;


void setup()
{
  pinMode (w, OUTPUT);
  pinMode (g, OUTPUT);
  pinMode (y, OUTPUT);
  Serial.begin(BAUD_RATE);
}

void loop()
{
  float temperature = getVoltage(tempPin);
  temperature = (temperature - .5) * 100;
  Serial.print(temperature);
  Serial.println(" C");
  delay(1000);

  if (Serial.available() > 0)
  {
    int command = Serial.read();

    if(command == '1')
    {
      digitalWrite(w, HIGH);
      Serial.println("HEAT ON");
      delay(4000);
      digitalWrite(g, HIGH);
    }
    else if (command == '2')
    {
      digitalWrite(w, LOW);
      Serial.println("HEAT OFF");
      delay(4000);
      digitalWrite(g, LOW);

    }
    if(command == '3')
    {
      digitalWrite(y, HIGH);
      digitalWrite(g, HIGH);
      Serial.println("A/C ON");
    }
    else if (command == '4')
    {
      digitalWrite(y, LOW);
      digitalWrite(g, LOW);
      Serial.println("A/C OFF");
    }
  }
}

float getVoltage(int pin)
{
  return (analogRead(pin) * .004882814);
}

One of the most important things you can learn about programming early on, is that debugging is easier if you keep your code tidy. Use the auto-format function in the IDE (CTRL-t/cmd-t). I prefer to keep open braces on the their own line.

Thank you for the help and the tip. Much appreciated!

chris8644:
Disregard the poll......

Glad you said that. I was having real trouble choosing.