If/Else Problem

Hi everyone, so I'm fairly new to Arduino, this is my first big project with the arduino and I'm having some trouble with an if/else statement.
Some background for this, I have a temperature sensor and based on what the temperature output is, there would be a "song", so to speak, that plays. So if a temperature is higher than a set point it would play one, and if it was lower than that point it would play another. My code is below and I think for better viewing purposes it would be copied and pasted into the actual Arduino sketchbook. I did not paste in the second song, just because I can learn from the first one what is wrong with the second.
My error is the 'frequency' was not declared in this scope.
Also, if you were to take both codes (just the outputs for the temperature sensor and the song portion) they are verified to work separate of each other, just not sure why one cannot work inside the other.
Thank you so much! :slight_smile:

const int temperaturePin = 0;


void setup()
{

  
 Serial.begin(9600);
}


void loop()
{

  float voltage, degreesC, degreesF;


  voltage = getVoltage(temperaturePin);
  
  
  degreesC = (voltage - 0.5) * 100.0;
  
  
  degreesF = degreesC * (9.0/5.0) + 32.0;
  
 
  


  Serial.print("voltage: ");
  Serial.print(voltage);
  Serial.print("  deg C: ");
  Serial.print(degreesC);
  Serial.print ("  deg F: ");
  Serial.println(degreesF);

   
  delay(1000);
  if (degreesF > 650)
  {
    
const int buzzerPin = 9;


const int songLength = 20;

char notes[] = "ccbacc ccbabb ccbaec";


float beats[] = {1,.5,.5,.5,.5,4,1,1,.5,.5,.5,.5,4,1.5,.5,.5,.5,.5,.5,4};


int tempo = 500;


{
  pinMode(buzzerPin, OUTPUT);
}


{
  int i, duration;
  
  for (i = 0; i < songLength; i++) 
  {
    duration = beats[i] * tempo;
    
    if (notes[i] == ' ')       
    {
       delay(duration);   
    }
    else                    
    {
      tone (buzzerPin, frequency(notes[i]), duration);
      delay(duration);         
    }
    delay(tempo/10);             
  }
  
  while (true){}
}


int frequency(char note) 
{

  
  int i;
  const int numNotes = 14;  
  
  
  char names[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'A', 'B', 'C'};
  int frequencies[] = {880, 988, 1106, 1175, 1319, 349, 392, 415, 466, 523};
  
  

  
  for (i = 0; i < numNotes; i++)  
  {
     if (names[i] == note)        
    {
      return (frequencies[i]);   
    }
  }
  return(0); 
}
  }
  }
  if else (degreesF < 600)
  {
      //Second Song Here
  }
}


float getVoltage int pin)
{
  
 returnanalogRead(pin) * 0.004882814);

  
}

My code is below and I think for better viewing purposes it would be copied and pasted into the actual Arduino sketchbook.

It would be easier to see here if you used code tags around code as requested in the stickies at the top of this forum.

Even without looking at your code I would guess that you have left out or misplaced a brace somewhere at the end of a function but with the code as currently presented I don't want to look at it in detail, but I did see 2 loop() functions and 2 setup() functions, which is wrong..

Please post it in code tags after having used Auto Format in the IDE.

One setup and one loop function per sketch, and you cannot nest them

Please use code tags when posting code -- I think you can see why we ask this.

Sorry about that, I believe I fixed what you asked for.

As for the tips, thanks guys I'll try inserting another } at the end to see if that helps and as far as one setup and one loop per sketch, I am unfamiliar with that. Meaning I can only have one setup and one loop?

Meaning I can only have one setup and one loop?

Correct.

, I believe I fixed what you asked for

Close enough for now.

You will not fix your problem by just putting a closing brace at the end of your program. If a missing brace is the problem you need to find out where it belongs, but that may not be the problem.

As to the setup() and loop() functions, yes, you can only have one of each. The setup() function is executed once at startup and the loop() function does what its name suggests and continually loops back to the start and runs again. Actually it doesn't do that but the Arduino environment makes it seem like it does. As a matter of information you cannot have 2 functions of the same name (any name) in a program. Think about it, how would the program know which one to call ?

Close enough for now

But no cigar !

As a matter of information you cannot have 2 functions of the same name (any name) in a program. Think about it, how would the program know which one to call ?

Usually by the types of their arguments :wink:

Picky, picky, picky (and right) :slight_smile:

Well if adding a } fixed it for now... but I'm getting a new error.

My error is the 'frequency' was not declared in this scope.

As you have not posted your latest code it is difficult to help.....

When you post it please use code tags (#) instead of quote tags and it will be even easier to read.

I fixed it and the latest code was up since the re-edit.

A couple of comments.

  1. Changing code in an old post makes a nonsense of subsequent comments. It is better to post the revised code in a new post.
  2. The code will not Auto Format because there are "Too many right curly braces". Position the cursor to the right of any bracket or brace and its matching bracket or brace will be highlighted in the code. Do this from start to finish in the program. Do all of the pairs match up and make sense ?
  3. Most of the blank lines in the code are unnecessary and make it more difficult to read.
  4. This line  if else (degreesF < 600)is plainly wrong. Read this page http://arduino.cc/en/Reference/Else and follow the program structure it shows.

Awesome! Okay so I did a lot of reformatting with it and all the curly braces match up. I am still getting the error of "'frequency' was not declared in this scope" on line 43.

const int temperaturePin = 0;

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

void loop()
{
  float voltage, degreesC, degreesF;
  voltage = getVoltage(temperaturePin);
  degreesC = (voltage - 0.5) * 100.0;
  degreesF = degreesC * (9.0/5.0) + 32.0;
  Serial.print("voltage: ");
  Serial.print(voltage);
  Serial.print("  deg C: ");
  Serial.print(degreesC);
  Serial.print("  deg F: ");
  Serial.println(degreesF); 
  delay(1000);

  if (degreesF > 650)
  {    
    const int buzzerPin = 9;
    const int songLength = 20;
    char notes[] = "ccbacc ccbabb ccbaec";
    float beats[] = {1,.5,.5,.5,.5,4,1,1,.5,.5,.5,.5,4,1.5,.5,.5,.5,.5,.5,4};
    int tempo = 500;
    {
      pinMode(buzzerPin, OUTPUT);
     }
      {
        int i, duration;
        for (i = 0; i < songLength; i++) 
        {
          duration = beats[i] * tempo;
          if (notes[i] == ' ')       
          {
            delay(duration);   
          }
          else                    
          {
            tone(buzzerPin, frequency(notes[i]), duration);
            delay(duration);         
          }
        delay(tempo/10);             
        }
      while(true){}
      }
      int frequency(char note) 
         {
          int i;
          const int numNotes = 14;  
          char names[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'A', 'B', 'C'};
          int frequencies[] = {880, 988, 1106, 1175, 1319, 349, 392, 415, 466, 523};
          for (i = 0; i < numNotes; i++)  
          {
            if (names[i] == note)       
            {
              return(frequencies[i]);   
            }    
          }
          return(0); 
          }
        }
       }
    else if (degreesF < 600)
  {
      //Second Song Here
  }
float getVoltage(int pin)
{
  return (analogRead(pin) * 0.004882814);
}

Your definition of frequency() on lines 50 .. 64 is inside the loop() function. That's not valid. You need to move that function definition outside of the other functions. Just cutting those lines out and pasting them at the end of the file would be fine.

THAT WORKED! Thank you so so so much. Now on to actually wire the thing together! Here is my new code (pretty much just put the lines 50-64 that you told me and moved it to the very end.

const int temperaturePin = 0;

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

void loop()
{
  float voltage, degreesC, degreesF;
  voltage = getVoltage(temperaturePin);
  degreesC = (voltage - 0.5) * 100.0;
  degreesF = degreesC * (9.0/5.0) + 32.0;
  Serial.print("voltage: ");
  Serial.print(voltage);
  Serial.print("  deg C: ");
  Serial.print(degreesC);
  Serial.print("  deg F: ");
  Serial.println(degreesF); 
  delay(1000); //Delays temperature reading by 1s

  if (degreesF > 650) //Hotter than 650 play Girl on Fire
  {    
    const int buzzerPin = 9;
    const int songLength = 20;
    char notes[] = "ccbacc ccbabb ccbaec";
    float beats[] = {1,.5,.5,.5,.5,4,1,1,.5,.5,.5,.5,4,1.5,.5,.5,.5,.5,.5,4};
    int tempo = 500;
    {
      pinMode(buzzerPin, OUTPUT);
     }
      {
        int i, duration;
        for (i = 0; i < songLength; i++) //Looks at next note and plays frequency associated
        {
          duration = beats[i] * tempo;
          if (notes[i] == ' ') //Space represents a rest
          {
            delay(duration);   
          }
          else                    
          {
            tone(buzzerPin, frequency(notes[i]), duration);
            delay(duration);         
          }
        delay(tempo/10);             
        }
      while(true){}
      }
        }
       
    else if (degreesF < 600)
  {
      //Second Song Here
  }
}
float getVoltage(int pin)
{
  return (analogRead(pin) * 0.004882814);
}
      int frequency(char note) 
         {
          int i;
          const int numNotes = 14;  
          char names[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'A', 'B', 'C'};
          int frequencies[] = {880, 988, 1106, 1175, 1319, 349, 392, 415, 466, 523};
          for (i = 0; i < numNotes; i++)  
          {
            if (names[i] == note)       
            {
              return(frequencies[i]);   
            }    
          }
          return(0); 
          }
    int tempo = 500;
    {
      pinMode(buzzerPin, OUTPUT);
     }

Useless curly braces need to go!

        delay(tempo/10);             
        }
      while(true){}
      }
        }
       
    else if (degreesF < 600)
  {
      //Second Song Here
  }

More useless braces here. But which ones? Your indenting sucks! Use Tools + Auto Format before you post any more crap like this.

Useyourspacebarnowandthentoo!