expected unqualified-id before 'switch'

May 4, 2015 at 4:38am
Hello everybody! I try to resolve my problem in this code:

#include <FastLED.h>

#define NUM_LEDS 16
#define DATA_PIN 3
#define CLOCK_PIN 2

CRGB leds[NUM_LEDS];

// Pin initialization for button
int buttonPin = 10;

// Values
int brightValue = 0;
int satValue = 0;
int hueValue = 0;

// Button
boolean buttonValue = true;
boolean buttonState = true;

// Pattern
int patternProgram = 0;

void setup() {
// Declare signal pins
pinMode(buttonPin,INPUT_PULLUP);
// Sanity check delay - allow reprogramming if accidently blowing power w/leds
delay(2000);

FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, GBR>(leds, NUM_LEDS);
Serial.begin(9600); // Initialize serial communication at 9600 bits per second
}

void loop() {

Serial.println(buttonValue);
Serial.println(patternProgram);

// Button press cycle
buttonValue = !digitalRead(buttonPin); // Read input value and store it in value
if (buttonValue != buttonState) { // The button state has changed
if (buttonValue == 0) { // Check if button is pressed
if (patternProgram == 0) { // If set to smooth logarithmic mapping
patternProgram = 1; // Switch to stepped chromatic mapping
}
else {
if (patternProgram == 1) {
patternProgram = 2; // Switch to next mode
}
else {
if (patternProgram == 2) {
patternProgram = 3;
}
else {
if (patternProgram == 4) {
patternProgram = 5;
}
else {
if (patternProgram == 5) {
patternProgram = 0;
}
}
}
}
}
}
}
buttonState = buttonValue; // Save the new state in your value
}

switch (patternProgram) {
case 0:
void loop() {
for(int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB::Blue;
FastLED.show();
// Clear this led for the next time around the loop
leds[dot] = CRGB::Black;
delay(30);
break;
}
}
}

and debugger from arduino says me error, like:

75:1: error: expected unqualified-id before 'switch'

Can somebody explain or correct the mistake? And why it happens?
May 4, 2015 at 10:41am
your function loop endsd before the switch statement so switch is in no function.
Also note that you are not allowed to write a function implementation inside of a function.

put that switch in a function and delete the void loop() and it's brackets like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void switcher(int patternProgram)
{
  switch (patternProgram) 
  {
    case 0:
      for(int dot = 0; dot < NUM_LEDS; dot++) 
      {
        leds[dot] = CRGB::Blue;
        FastLED.show();
        // Clear this led for the next time around the loop
        leds[dot] = CRGB::Black;
        delay(30);
      }
      break;
  }
}
May 5, 2015 at 1:16am
Great! Many thanks! YOu r the best)
Last edited on May 5, 2015 at 1:17am
Topic archived. No new replies allowed.