Matrix Keypad - Optimisation

Yes you can nest switch statements but proper indenting your code will help to keep the overview in the editor. Use tools >> auto format or press CTRL-T

Not sure if you can use just letters like that, or if they need to defined as 'a', 'b', etc.
Try it, see which way works.
not sure 'char long' is a type - think you only get char, or unsigned char. 1 byte long in either case.

Nested Switch:case works.
Need to watch the {} and getting the break; out of the case in all the right places.
switch(value)
{
case 1:
switch(value2)
{
case 'a':
// code 'a'
break;
}
break;
case 2:
// code 2
break;
}

Must be defined as 'a' 'b' as then they are of the type char, just a would be a variable name.

The values allowed in a switch statement behind the case include byte, char, int, long and unsigned versions of it. Furthermore the enum type can be used.

// sample code to show the usage of enum type

enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

void setup()
{

  Days thisDay = Sunday;
  
  switch ( thisDay )
  {
    case Sunday:
    break;
    case Monday:
    break;
    case Tuesday:
    break;
  }
}

void loop()
{
}

[edit]enums are ideal for state machines[/edit]

enum - that's kinda neat. Could make following code easier.

it's not liking my using char's for keypressed values; i'm assuming because it's a case label and needs to be an integer

keypad:62: error: case label does not reduce to an integer constant

it refers to line 62 which is the line prior to where I declare case "a" line 62 is blank ; there is of course one of these errors for each non numerical keypressed value I declare

case "a":
// do a
break;

keypad:35: error: invalid conversion from 'const char*' to char

else if ((keyboardValue >108) && (keyboardValue <=153)){keypressed = 3;}
  else if ((keyboardValue >153) && (keyboardValue <=186)){keypressed = "a";}

line 35 is the line where I declare (keypressed = 3;)

I guess I could define the cases as numbers and just have a comment for the non numerical keys as to what they represent and change keypressed back to an integer;

will try that;

case "a": ==> case 'a':
you must use single quotes, otherwise it is a string and the case will use the address of the string or so.

single quotes worked, thanks

I still don't get the fuction of this code:

  int k = kbValue(keyboardValue);   // maps the value of the key being pressed "keypressed" i.e. 0-9
  
  Serial.println(k);                // print the value back to the Serial view window on your PC
  delay(1000);
}

// interpret the keyboard routine
int kbValue(int kbv)

int k = kbValue(keyboardValue);

in plain English: kbValue() will do a lookup of keyboardValue (the parameter passed) and returns a value 0..16 to indicate the keypressed. This return value is assigned to the var k.

The names are a bit misleading here. Better names could be

  • int rawKeyPressed = analogread(A0);
  • int key = ConvertRawToKey(rawKeyPressed);
  • switch(key)

etc

try just single quotes, not double. Think you have to decide on one other - is keypressed going to by type char, or type int (or byte)?

yeah the single quotes worked on case

rob- thanks for the disambiguation

-carl