expected unqualified~id before 'switch' please help

I'm haveing real trouble I'm getting this error expected unqualified~id before 'switch' for this code

const int AudioSource1 = 12;    //audio inputs decleared at pin 12 and 13
const int AudioSource2 = 13;
const int LeftEarphones = 7; //left ear phones declared at pin A0
const int RightEarphones = A1; //Right ear phones declared at pin A1
const int LeftDolbyFront = A2;//Dolby 5:5 outputs
const int LeftDolbyBack = A3;
const int RightDolbyFront = A4;
const int RightDolbyBack = A5;
const int DolbySub = ~11;//dolby sub-woofer
//const int Button
int k = A0;
int adc_key_val[5] ={50, 200, 400, 600, 800 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
boolean incremented = false;
int state = 0;


//int k;//keypress

void setup() {
  // initialize serial communication:
  Serial.begin(9600);  
  pinMode (AudioSource1, INPUT);
  pinMode (AudioSource2, INPUT);
  pinMode (LeftEarphones, OUTPUT);
  pinMode (RightEarphones, OUTPUT);
  pinMode (LeftDolbyFront, OUTPUT);
  pinMode (LeftDolbyBack, OUTPUT);
  pinMode (RightDolbyFront, OUTPUT);
  pinMode (RightDolbyBack, OUTPUT);
  pinMode (DolbySub, OUTPUT);
  
  Serial.begin(9600);
} 

void loop() {
  // read the sensor:
 if (adc_key_in < 200 && (incremented = false));{
   state ++;
   incremented=true;
 }
if (adc_key_in < 200);
{
    incremented =false;
    if(state >2);
   state=0;
 }
} 
  // map the sensor range to a range of four options:
//  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // do something different depending on the 
  // range value:
  [color=red]switch (adc_key_in < 200) {[/color]
  case 0: {   // rightspeakers
    Serial.println('Right speakers');
    int RightDolbyFront = HIGH;
    int RightDolbyBack = HIGH;
    int DolbySub = HIGH;
    
    
    break;
  }
  case 1:  {//Left speakers
    Serial.println('Left speakers');
      int LeftDolbyFront = HIGH;
    int LeftDolbyBack = HIGH;
    int DolbySub = HIGH;
    
    break;
  }
  case 2:{    // Headphones
    Serial.println('Headphones');
    int LeftEarphones = HIGH;
    int RightEarphones = HIGH;
    break;
  }
  Default:{    
    Serial.println("off");
    break;
  } 
  delay(1);        // delay in between reads for stability
}

(code tags added by moderator)

which I heavley modified from an exampleI found on the web. the compiler says the error is in in the line in red but I just cant see whats wrong please help

if (adc_key_in < 200 && (incremented = false));
if (adc_key_in < 200);
{

You'll be wanting to fix those three too.

Please use code tags when posting code.

const int DolbySub = ~11;

Do you have a pin number -12?

Your braces don't match.

Default:

sp. "default".

    Serial.println('Headphones');
    int LeftEarphones = HIGH;
    int RightEarphones = HIGH;

Double quotes.
Lose the "int" in both cases - these should be "digitalWrite"s, I 'm guessing
Rinse and repeat.

// read the sensor:

The comment says you're doing something, but you don't.

SWITCH just takes the name of a variable so it should be

switch (adc_key_in) {

and please go back to your original post and modify it to put the code in code tags so it looks like the above.

...R

Hi,

Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Please read above link.

Tom..... :slight_smile:

Robin2:
SWITCH just takes the name of a variable so it should be

No, "switch" takes any integer expression, but obviously with a comparison as that expression, the range of possible cases is severely limited. :wink:

AWOL:

Robin2:
SWITCH just takes the name of a variable so it should be

No, "switch" takes any integer expression, but obviously with a comparison as that expression, the range of possible cases is severely limited. :wink:

I was going to post, in fact I did then deleted it, exactly what Robin said. But when I took the comparison out, it still gave the OP's quoted compile error. I realised that it's legit as code, although as you say AWOL, the cases then need to have passed the test first. Interesting.....

Your code, from switch to the end is outside void loop()
Remove the } above switch and put it at the end.
Then it will compile.

I realised that it's legit as code, although as you say AWOL, the cases then need to have passed the test first.

No, the "case"s ARE the result of the test, i.e. zero or one only.

There is a lot of code that is not inside a function. None of that code will compile.

This code will compile, but still needs work to make it do anything.

const byte AudioSource1 = 12;    //audio inputs decleared at pin 12 and 13
const byte AudioSource2 = 13;
const byte LeftEarphones = 7; //left ear phones declared at pin A0
const byte RightEarphones = A1; //Right ear phones declared at pin A1
const byte LeftDolbyFront = A2;//Dolby 5:5 outputs
const byte LeftDolbyBack = A3;
const byte RightDolbyFront = A4;
const byte RightDolbyBack = A5;
const byte DolbySub = 11;//dolby sub-woofer

const int adc_key_val[5] ={50, 200, 400, 600, 800 };
const int NUM_KEYS = 5;

int k = A0;
int adc_key_in;
int key=-1;
int oldkey=-1;
boolean incremented = false;
int state = 0;

void setup() 
{
  Serial.begin(9600); 
  pinMode (AudioSource1, INPUT);
  pinMode (AudioSource2, INPUT);
  pinMode (LeftEarphones, OUTPUT);
  pinMode (RightEarphones, OUTPUT);
  pinMode (LeftDolbyFront, OUTPUT);
  pinMode (LeftDolbyBack, OUTPUT);
  pinMode (RightDolbyFront, OUTPUT);
  pinMode (RightDolbyBack, OUTPUT);
  pinMode (DolbySub, OUTPUT);
}

void loop() 
{
  // read the sensor THIS NEEDS SOME CODE
  
  if (adc_key_in < 200 && (incremented ==false))
  {
    state ++;
    incremented=true;
  }
  
  if (adc_key_in < 200)
  {
    incremented = false;
    if(state >2)
      state=0;
  }
  // map the sensor range to a range of four options THIS LOOKS PROMISING
  //int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
  int range;  // DUMMY PLACEHOLDER - see above
  switch (range)
  {
    case 0:    // rightspeakers
      Serial.println("Right speakers");
      digitalWrite (RightDolbyFront, HIGH);
      digitalWrite (RightDolbyBack, HIGH);
      digitalWrite (DolbySub, HIGH);
    break;

    case 1:  //Left speakers
      Serial.println("Left speakers");
      digitalWrite (LeftDolbyFront, HIGH);
      digitalWrite (LeftDolbyBack, HIGH);
      digitalWrite (DolbySub, HIGH);
    break;

    case 2:    // Headphones
      Serial.println("Headphones");
      digitalWrite (LeftEarphones, HIGH);
      digitalWrite (RightEarphones, HIGH);
    break;
  
    default:   
      Serial.println("off");
    break;
  }
  delay(1);
}