switch:case question

I’ve got these global variables defined:

byte backspace = 0x5c; // use to indicate backspace '\'
byte hold =     0x2b; //tbd  +
byte oneLeft =  0x3c; //tbd  <
byte normal =   0x3d; //tbd  =
byte oneRight = 0x3e; //tbd  >
byte move_control = normal;

I use them as part of a switch:case here within loop for serial data coming in to map them to readable names

if (Serial.available()>0){
  incomingByte = Serial.read();
  goodFont = 3;
  // mapping from incoming character to font_array or other commands: \ + = < >
  // invalid characters are not defined and are ignored

  switch(incomingByte){
  case 0x2b:
    // change scroll movement
    move_control = hold; // +, hold
    goodFont = 2; // not a character to add to displayArray
    break;
  case 0x3c:
    // change scroll movement
    move_control = oneLeft; // <, move 1 left
    goodFont = 2; // not a character to add to displayArray
    break;
  case 0x3d:
    // change scroll movement
    move_control = normal; // normal
    goodFont = 2; // not a character to add to displayArray
    break;
  case 0x3e:
    // change scroll movement
    move_control = oneRight; // >, move 1 right
    goodFont = 2; // not a character to add to displayArray
    break;
  case 0x5c:
    // use 0x5c '\' for backspace to delete characters
    move_control = backspace;
    goodFont = 2; // not a character to add to displayArray
    break;
  } // end switch

So why is it when I try to adjust a pointer right after this, I get this error:
‘hold’ cannot appear in a constant-expression

I tried moving the switch:case chunks around, I get the same error message at the first case: listed.

  switch (move_control){
    // other movement checks

  case hold:
    // do nothing
    break;

  case oneRight:
    if (displayStart<190){
      displayStart = displayStart +1;
    }
    else {
      displayStart = 0;  // if not <191, must be 191
    }  
    move_control = hold;
    break;// end test move right

  case oneLeft:
    if (displayStart>0){
      displayStart = displayStart -1;
    }
    else {
      displayStart = 191;  // if not >0, must be 0
    }
    move_control = hold;
    break;  // end test move left

  case backspace:
    if ( displayEnd >5){
      displayEnd = displayEnd -6; // move end of array back 1 character + spacer
    }
    break;

  case normal:
    // allow shifting on time checks
    move_control = normal;
    break;

    updateDisplay = 1;
  } // end move control

Thanks

Does this help…

static const byte backspace = 0x5c; // use to indicate backspace ‘’
static const byte hold = 0x2b; //tbd +
static const byte oneLeft = 0x3c; //tbd <
static const byte normal = 0x3d; //tbd =
static const byte oneRight = 0x3e; //tbd >
static const byte move_control = normal;

Can you post code that reproduces the problem? For example, this compiles OK:

byte backspace = 0x5c; // use to indicate backspace '\'
byte hold =     0x2b; //tbd  +
byte oneLeft =  0x3c; //tbd  <
byte normal =   0x3d; //tbd  =
byte oneRight = 0x3e; //tbd  >
byte move_control = normal;

byte incomingByte, goodFont;

void setup ()
{
  if (Serial.available()>0){
  incomingByte = Serial.read();
  goodFont = 3;
  // mapping from incoming character to font_array or other commands: \ + = < >
  // invalid characters are not defined and are ignored

  switch(incomingByte){
  case 0x2b:
    // change scroll movement
    move_control = hold; // +, hold
    goodFont = 2; // not a character to add to displayArray
    break;
  case 0x3c:
    // change scroll movement
    move_control = oneLeft; // <, move 1 left
    goodFont = 2; // not a character to add to displayArray
    break;
  case 0x3d:
    // change scroll movement
    move_control = normal; // normal
    goodFont = 2; // not a character to add to displayArray
    break;
  case 0x3e:
    // change scroll movement
    move_control = oneRight; // >, move 1 right
    goodFont = 2; // not a character to add to displayArray
    break;
  case 0x5c:
    // use 0x5c '\' for backspace to delete characters
    move_control = backspace;
    goodFont = 2; // not a character to add to displayArray
    break;
  } // end switch
  }
  
}

void loop () {}

Coding Badly’s suggestion might help. I don’t think you need the “static” part though.

The cases in a switch have to be constant. If you don't put "const" there it thinks you are switching on something that might change at runtime.

I made this change:

static const byte backspace = 0x5c; // use to indicate backspace '\'
static const byte hold =     0x2b; //  +
static const byte oneLeft =  0x3c; //  <
static const byte normal =   0x3d; //  =
static const byte oneRight = 0x3e; //  >
byte move_control = normal;

that seemed to fix it.
worked this way too

const byte backspace = 0x5c; // use to indicate backspace '\'
const byte hold =     0x2b; //  +
const byte oneLeft =  0x3c; //  <
const byte normal =   0x3d; //  =
const byte oneRight = 0x3e; //  >
byte move_control = normal;

Didn’t realize the case values couldn’t be regular variables. Had never come across that before.
Thanks! I can go to bed happy now :slight_smile:

It may not be required but it's still a good idea to include it. :wink:

It may not be required but it's still a good idea to include it.

What does static contribute to a constant, global variable? Inquiring minds, and all that crap.

What does static contribute to a constant, global variable?

It specifies internal linkage.
Just one use,
If a variable is declared (.h) as extern ( external linkage ), a static definition of a variable gives the 'defined in' translation unit its own copy initialised to the static initialisation, while other translation units share the definition of external variable.

IIRC "static" global variables are module- (i.e. file-) private. That is, they can't be accessed by other modules (.c(pp) files) even if they host an "extern" declaration that refers to it.

OK. Thanks to both of you for the responses. Most of what I do with the Arduino is all in one file, so I forgot about the need to localize the scope of the variable.

pyro_65, tuxduino, not quite sure what all that means. All my code is either 1 .ino file, or multiple tabs under 1 .ino file.
Not complex enough for .h and .cpp stuff.
I make all my variables global so I can access anything from anywhere in the code. Maybe poor programming practice, but it works for me.

All my code is either 1 .ino file, or multiple tabs under 1 .ino file.

Multiple tabs == multiple files. But, the IDE combines all files with the .ino extension into one .cpp file for compiling.

Not complex enough for .h and .cpp stuff.

It's all C++ under the wrappers.

See, that's why I like the IDE - takes care of all that stuff, and I can just concentrate on hardware designs 8)

I am not sure why they are disparaged on Arduino, but #define would work here, too.

I am not sure why they are disparaged on Arduino, but #define would work here, too.

#define code does not allow for type checking. A #define statement in one file doesn't provide any information that another file can use, unless the #define statement is in a header file that is included in other files.

CrossRoads:
pyro_65, tuxduino, not quite sure what all that means. All my code is either 1 .ino file, or multiple tabs under 1 .ino file.
Not complex enough for .h and .cpp stuff.
I make all my variables global so I can access anything from anywhere in the code. Maybe poor programming practice, but it works for me.

Yes, thats my SOP also. I understand the why and reasons for good scoping practices, especially for large programs being worked on by multiple people, but using mostly globals works well for me in my simple projects. I also avoid trying to create any C++ only features, other then utilizing existing C++ library functions that others have created. Simple straight C for this simple mind seems to be all I really require so far.

Hardware rules :wink:

Lefty

I don't think I've see anyone disparage them, you see it used in sketches that are posted.
This is the first time in 2+ years of arduino coding where I had this problem. Lucky? :roll_eyes:

"In general, the const keyword is preferred for defining constants and should be used instead of #define. "

Sounds like disparagement to me...8^)

dis·par·age? ?verb (used with object), dis·par·aged, dis·par·ag·ing.

  1. to speak of or treat slightingly; depreciate; belittle: Do not disparage good manners.
  2. to bring reproach or discredit upon; lower the estimation of: Your behavior will disparage the whole family.

Only if you're really thin-skinned :wink: