This is a kind of weird error in that the more meaningful part comes a few lines down in the output “crosses initialization of”…
You have initialized the out variable inside the READ case, which caused the error. The way to fix this is to wrap the case code in braces, which makes the out variable local to that scope:
case READ:
{
String out = ">";
for (int i = 0; i <= 32; i++) {
out = String(out) + String("\n") + String(i) + String(": ") + display[i];
}
return out;
}
break;
/Users/john/Documents/Arduino/sketch_dec25a/sketch_dec25a.ino:3:15: warning: jump to case label [-fpermissive]
#define WRITE 2
^
/Users/john/Documents/Arduino/sketch_dec25a/sketch_dec25a.ino:27:10: note: in expansion of macro 'WRITE'
case WRITE:
^~~~~
/Users/john/Documents/Arduino/sketch_dec25a/sketch_dec25a.ino:16:14: note: crosses initialization of 'String out'
String out = ">";
^~~
It's saying that the jump to "case 2:" on line 27 (the '2' being the expansion of the macro 'WRITE' declared on line 10) jumps past the initialization of the local variable 'out' declared on line 14. If that jump were allowed then the local variable would be 'in scope' (visible) but not initialized.
Whilst I would use an enum to define the states, which means that I don't even have to know what value has been assigned to them, I don't see that an enum is "better"
UKHeliBob:
Whilst I would use an enum to define the states, which means that I don't even have to know what value has been assigned to them, I don't see that an enum is "better"
One advantage is that the compiler can warn you if you don't have a case for every member of the enum. That warning makes the enum choice 'better' in my book.
/Users/john/Documents/Arduino/sketch_dec25a/sketch_dec25a.ino: In function 'void loop()':
/Users/john/Documents/Arduino/sketch_dec25a/sketch_dec25a.ino:17:10: warning: enumeration value 'TWO' not handled in switch [-Wswitch]
switch (currentCase)
^
/Users/john/Documents/Arduino/sketch_dec25a/sketch_dec25a.ino:17:10: warning: enumeration value 'THREE' not handled in switch [-Wswitch]
Sketch uses 1438 bytes (4%) of program storage space. Maximum is 32256 bytes.
Global variables use 184 bytes (8%) of dynamic memory, leaving 1864 bytes for local variables. Maximum is 2048 bytes.
Thanks - it had never occurred to me to use the name of the enum in that way, forgetting entirely that the enum was in fact defining a type. I will certainly try to remember to use enums in that way in future.
Is it my imagination (bad memory ?) or wasn't the use of typedef mandatory at some time in the past ?