"does not name a type" for a defined variable

So I’m getting the error " ‘song’ does not name a type", which usually means you are trying to call something from a library that isn’t defined.

But song is defined in the main sketch as an int. I dont understand, this was working 5 min ago, and now it’s decided to stop. Please help.

// test program for serial input prior to connecting wireless transmitter

int song = 0;

void setup()                   
{
  Serial.begin(9600);       // set up Serial library at 9600 bps
  Serial.flush(); 
}

void loop()                 // we need this to be here even though its empty
{
  Serial.println("Please enter song cue (1-18): ");

  while(Serial.available() == 0)
  {
    delay(100);             // wait 1/10 seconds to allow serial input of more than 1 char
  }
  
  }
  song = Serial.read();
  song = song - 48;         //correct for ascii offset
  
  switch(song)
  {
     case 1:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
     case 2:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;   
     case 3:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
     case 4:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
      case 5:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
      case 6:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
      case 7:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
      case 8:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
      case 9:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
      case 10:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
      case 11:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
      case 12:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break; 
      case 13:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
      case 14:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;  
      case 15:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
      case 16:
        Serial.print("Playing song: ");
        Serial.println(song);
        delay(1000);
        Serial.print("Finished playing song: ");
        Serial.println(song);
        Serial.println(" ");
        break;
      case 17:
        Serial.println("Playing: Semi-Grand Finale");
        Serial.print(song);
        delay(1000);
        Serial.println("Finished playing: Semi-Grand Finale");
        Serial.println(song);
        break;
      case 18:
        Serial.print("Playing: Grand Finale");
        delay(1000);
        Serial.println("Finished playing: Grand Finale");
        Serial.println(" ");
        break; 
      default:
        Serial.print("Invalid song selection please choose song (1-18): ");
        Serial.println(" ");
        break;            
  }
  
  song = 0;
  Serial.flush();
}

this was working 5 min ago

Impossible, you have too many } :wink:

I feel like a moron, thankyou. But that error should really be something more like "mis-matched braces"

Why use a switch statement for that? Every case is identical.

Pete

they're not all exactly identical, and its more for debug. Once its done being tested it will be stripped out of the transmitter and go into the receiver

OK, my bad. Only the first 16 cases are identical. The whole thing could be done with 3 if statements.

Pete

When I get it to accept inputs >9 they will all be individual

When I get it to accept inputs >9 they will all be individual

You seem to be understanding the fact that numerical serial input from the monitor is a string of ASCII characters which represent the digits 0-9 and your switch statement is based on actual numbers, some of which are two digits.

The best way to handle the serial input is with "non-blocking" methods which read in all the characters until an end marker is received. See the excellent tutorial by Robin2 on serial input methods at forum.arduino.cc/index.php?topic=288234.0 There is code presented there which handles simple integer input like you have. You will do well to study and learn the techniques presented in the tutorial.

However, some programs can tolerate "blocking" methods, and I think yours might be a candidate for the use of Serial.parseInt() which is a blocking function.https://www.arduino.cc/en/Serial/ParseInt

You can begin your loop with code like this

Serial.println("Please enter song cue (1-18): ");

  while (Serial.available()== 0)//hold program here waiting serial input
  { }

  song = Serial.parseInt();//Serial.read(); 
  //Serial.parseInt will wait 1000ms for more than one char
  //wait time can be set with Serial.setTimeout(time)
  
  //song = song - 48;         //correct for ascii offset not needed because Serial.parseInt returns number

  switch (song)
//the rest of your code