Switch case statement

First time using the Switch / case and I get the following error message:
exit status 1
jump to case label [-fpermissive]

Here is the code/

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue ;  // variable to store the value coming from the sensor
int outputHz ;        // desired output in hzs
int outHz = 2;         //select the pin for output to stepper
int enablePin = 3;   //select pin to enable the stepper motor controller
int dirPinOut = 4;   //select pin to output dir control to stepper motor controller
int dirPinIn = 5;    //select pint to read dir switch high = clockwise
int brakePinIn = 6;  // locks motor when high
int cutoff = 50;      // above this point enable drive
int RUNled = 13;      // turns on when drive is enable or in brake mode
int function;         // call subroutine called by switch case statement
float calValue ;       // cal value for delay of output
float senone ;
String dir;
String enable;



void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(enablePin, OUTPUT);
  pinMode(RUNled, OUTPUT);
  pinMode(outHz, OUTPUT);
  pinMode(dirPinOut, OUTPUT);
  pinMode(dirPinIn, INPUT);
  pinMode( brakePinIn, INPUT);
  Serial.begin(250000);

}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  outputHz = map(sensorValue, 40, 1023, 40, 24000);
  if (digitalRead(brakePinIn == HIGH))
  {
    function = 1;
  }

  if (outputHz < cutoff)
  {
    function = 2;
  }

  if (outputHz >= cutoff && digitalRead(dirPinIn == HIGH))

  {
    function = 3;
  }

  if (outputHz >= cutoff && digitalRead(dirPinIn == LOW))

  {
    function = 4;
  }
  switch (function)
  {
    case 1:
      digitalWrite(enablePin, LOW);
      digitalWrite(RUNled, HIGH);
      noTone(outHz);
      Serial.print("  case 1");
      delay(200);
      break;

    case 2:
      outHz = 31;
      digitalWrite(enablePin, HIGH);
      digitalWrite(RUNled, LOW);
      char enable[] = "OFF";
      noTone(outHz);
      Serial.print("   case 2");
      delay(200);
      break;

    case 3:
      digitalWrite(enablePin, LOW);
      digitalWrite(RUNled, HIGH);
      digitalWrite(dirPinOut, HIGH);
      Serial.print("   case 3");
      tone(outHz, outputHz);
      break;

    case 4:
      digitalWrite(enablePin, LOW);
      digitalWrite(RUNled, HIGH);
      digitalWrite(dirPinOut, LOW);
      Serial.print("   case 4");
      tone(outHz, outputHz);
      break;
  }

  if (digitalRead(dirPinIn) == HIGH && digitalRead(enablePin) == HIGH)
  {
    digitalWrite(dirPinOut, HIGH);
    dir = "CW";
  }
  if (digitalRead(dirPinIn) == LOW && digitalRead(enablePin) == HIGH)
  {
    digitalWrite(dirPinOut, LOW);
    dir = "CCW";
  }


  Serial.println(millis());
  Serial.print(outputHz);
  Serial.print("  ");
  Serial.print(sensorValue);
  Serial.print("   ");
  Serial.print(dir);
  Serial.print("   ");
  Serial.print(enable);



}

the line " case 4 is highlighted.
UNO CPU

any help??

Thanks

JB

JB

Try this for EACH case:

case 4:
      {
      digitalWrite(enablePin, LOW);
      digitalWrite(RUNled, HIGH);
      digitalWrite(dirPinOut, LOW);
      Serial.print("   case 4");
      tone(outHz, outputHz);
      }
      break;

Notice the curly braces.

Delta_G:

case 2:

outHz = 31;
     digitalWrite(enablePin, HIGH);
     digitalWrite(RUNled, LOW);
     char enable[] = "OFF";
     noTone(outHz);
     Serial.print("   case 2");
     delay(200);
     break;




You don't want to create a new variable (enable) inside a case statement. If you try to use it outside the case statement then it might or might not have actually been defined because the code might or might not have gone through that case. Putting curly braces around it might make it compile, but it won't fix anything because you still have the same problem. It will compile but if you try to use that variable outside that particular case statement then you'll get a scope error since the variable now only lives inside that one case block. Your best fix is to define that variable outside the switch case and give it another value inside if you want to.

Although you are probably right, there are two things to consider:

  1. enable is already defined in as a global string
  2. enable isn't even referenced in case 4

I'm using IDE 1.6.9 under Windows 7 and if I comment out line 71:

// char enable[] = "OFF";

which defines enable[], the code compiles. I'm not sure, but this seems to be a bug as data definitions should be allowed in a case statement block.

Other than learning how to use switch/case, you could write the code:

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue ;  // variable to store the value coming from the sensor
int outputHz ;        // desired output in hzs
int outHz = 2;         //select the pin for output to stepper
int enablePin = 3;   //select pin to enable the stepper motor controller
int dirPinOut = 4;   //select pin to output dir control to stepper motor controller
int dirPinIn = 5;    //select pint to read dir switch high = clockwise
int brakePinIn = 6;  // locks motor when high
int cutoff = 50;      // above this point enable drive
int RUNled = 13;      // turns on when drive is enable or in brake mode
int function;         // call subroutine called by switch case statement
float calValue ;       // cal value for delay of output
float senone ;
String dir;
String enable;



void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(enablePin, OUTPUT);
  pinMode(RUNled, OUTPUT);
  pinMode(outHz, OUTPUT);
  pinMode(dirPinOut, OUTPUT);
  pinMode(dirPinIn, INPUT);
  pinMode( brakePinIn, INPUT);
  Serial.begin(250000);

}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  outputHz = map(sensorValue, 40, 1023, 40, 24000);
  if (digitalRead(brakePinIn == HIGH))
  {
    function = 1;
    digitalWrite(enablePin, LOW);
    digitalWrite(RUNled, HIGH);
    noTone(outHz);
    Serial.print("  case 1");
    delay(200);

  }

  if (outputHz < cutoff)
  {
    function = 2;
    outHz = 31;
    digitalWrite(enablePin, HIGH);
    digitalWrite(RUNled, LOW);
    //     char enable[] = "OFF";
    noTone(outHz);
    Serial.print("   case 2");
    delay(200);

  }

  if (outputHz >= cutoff && digitalRead(dirPinIn == HIGH))
  {
    function = 3;
    digitalWrite(enablePin, LOW);
    digitalWrite(RUNled, HIGH);
    digitalWrite(dirPinOut, HIGH);
    Serial.print("   case 3");
    tone(outHz, outputHz);
  }

  if (outputHz >= cutoff && digitalRead(dirPinIn == LOW))
  {
    function = 4;
    digitalWrite(enablePin, LOW);
    digitalWrite(RUNled, HIGH);
    digitalWrite(dirPinOut, LOW);
    Serial.print("   case 4");
    tone(outHz, outputHz);
  }

  if (digitalRead(dirPinIn) == HIGH && digitalRead(enablePin) == HIGH)
  {
    digitalWrite(dirPinOut, HIGH);
    dir = "CW";
  }
  if (digitalRead(dirPinIn) == LOW && digitalRead(enablePin) == HIGH)
  {
    digitalWrite(dirPinOut, LOW);
    dir = "CCW";
  }


  Serial.println(millis());
  Serial.print(outputHz);
  Serial.print("  ");
  Serial.print(sensorValue);
  Serial.print("   ");
  Serial.print(dir);
  Serial.print("   ");
  Serial.print(enable);

}

and do away with switch block. Anytime I use a switch, I always have a default like:

   switch (function) {

      case 1:
      // a bunch more code in the block...

      default:
         Serial.print("I shouldn't be here, Line XX, function = ");
         Serial.println(function);
         break;
   }

Sometime the default can make it easier to figure out why things went South.

I changed in case 2:

      char enable[] = "OFF";

to

      enable = "OFF";

But that doesn't fix the other things that won't runs as you may expect.

Neither of these expressions for example, I have no real idea what they will do except no what you want.

  digitalRead(dirPinIn == HIGH)

  digitalRead(dirPinIn == LOW)

You set enable to "OFF" but never to "ON".

Here's the sketch with a fixes for those and a few hints for changes as well.
It has nothing beyond beginner level unless you count C string functions as beyond beginner in which case strcpy means string copy and the syntax is strcpy( destination, source ), it copies the source string to the destination string that must be large enough for the source to fit plus 1 char for the terminating 0.

#include "Arduino.h"
#include <string.h>

byte sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue ;  // variable to store the value coming from the sensor
int outputHz ;        // desired output in hzs
byte outHzPin = 2;         //select the pin for output to stepper
byte enablePin = 3;   //select pin to enable the stepper motor controller
byte dirPinOut = 4;   //select pin to output dir control to stepper motor controller
byte dirPinIn = 5;    //select pint to read dir switch high = clockwise
byte brakePinIn = 6;  // locks motor when high
int cutoff = 50;      // above this point enable drive
byte RUNled = 13;      // turns on when drive is enable or in brake mode
byte function;         // call subroutine called by switch case statement

float calValue ;       // cal value for delay of output... float for delay???
float senone ;         // not used, good, Arduino floats are slowwwwww

char dir[ 4 ];                   // can fit 3 characters plus terminator, "CW" or "CCW" both fit
char enableStepper[ 4 ];   // can fit 3 characters plus terminator, "ON" or "OFF" both fit


void setup() 
{
  pinMode(enablePin, OUTPUT);
  pinMode(RUNled, OUTPUT);
  pinMode(outHzPin, OUTPUT);
  pinMode(dirPinOut, OUTPUT);
  pinMode(dirPinIn, INPUT);
  pinMode( brakePinIn, INPUT);
  Serial.begin(250000);             // 115200 used to be top speed       
}

void loop() 
{
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  outputHz = map(sensorValue, 40, 1023, 40, 24000);
  if (digitalRead(brakePinIn == HIGH))
  {
    function = 1;
  }

  if (outputHz < cutoff)
  {
    function = 2;
  }
  else // outputHz must be >= cutoff
  {
    if (digitalRead( dirPinIn ) == HIGH)
    {
      function = 3;
    }
    else
    {
      function = 4;
    }
  }

  switch (function)
  {
    case 1:
      digitalWrite(enablePin, LOW);
      digitalWrite(RUNled, HIGH);
      strcpy( enableStepper, "ON" );
      noTone(outHzPin);
      Serial.print("  case 1");
      delay(200);
      break;

    case 2:
      outHzPin = 31;
      digitalWrite(enablePin, HIGH);
      digitalWrite(RUNled, LOW);
      strcpy( enableStepper, "OFF" );
      noTone(outHzPin);
      Serial.print("   case 2");
      delay(200);
      break;

    case 3:
      digitalWrite(enablePin, LOW);
      digitalWrite(RUNled, HIGH);
      digitalWrite(dirPinOut, HIGH);
      Serial.print("   case 3");
      tone(outHzPin, outputHz);
      break;

    case 4:
      digitalWrite(enablePin, LOW);
      digitalWrite(RUNled, HIGH);
      digitalWrite(dirPinOut, LOW);
      Serial.print("   case 4");
      tone(outHzPin, outputHz);
      break;
  }

  if (  digitalRead(enablePin) == HIGH )
  {
    if (digitalRead(dirPinIn) == HIGH )
    {
      digitalWrite(dirPinOut, HIGH);
      strcpy( dir, "CW" );
    }
    else // digitalRead(dirPinIn) must be LOW
    {
      digitalWrite(dirPinOut, LOW);
      strcpy( dir, "CCW" );
    }
  }

  Serial.println(millis());
  Serial.print(outputHz);
  Serial.print("  ");
  Serial.print(sensorValue);
  Serial.print("   ");
  Serial.print(dir);
  Serial.print("   ");
  Serial.println(enableStepper);

}

Sometimes it pays to read the whole error message. When I try to compile your code, I get:

sketch_may19a.ino: In function 'void loop()':
sketch_may19a:77: error: jump to case label
sketch_may19a:71: error: crosses initialization of 'char enable [4]'

Which tells you EXACTLY what the problem is.

If you want to assign a new set of values to an array, you have to do it one element at a time (possibly using a function that knows how to do that).

As to how to do that, I couldn't really say because you have an array named enable and a stupid String instance called enable.