keypad brainfart

Heres a snippet of code from my current project, I am reading from a keypad, which works fine because if I press button F, the Serial.println(key); returns F.
but then immediately I try to run a switch routine in function whichkey() and I get no value returned for any key. It doesn’t print the “switch on” text, and if I try to serialPrinmt t it shows 0 in HEX BIN or whatever ?

It prints the “whichkey routine” so I know its getting that far …

I have tried it as case ‘F’ and as char 70 but no luck.

I have copied the same code I use when sending over virtualwire with a radio link which works fine ( switching with the ASCII code )

I know I am going to kick myself as I think I have been here before ! :slight_smile: Give me a reminder someone please …

void loop (){
 
  char key = keypad.getKey();                 // reading the keypad
  if(key)                                     // same as if(key != NO_KEY)- did something change?
  {     Serial.print("key read =");   Serial.println(key); 
  whichkey();
   show ();
}
}// end of loop
void whichkey (){ 
     Serial.println("whichkey routine");
   Serial.println(key, HEX );
  switch (key)  {
      case 'F':  //  F chr 70  ON    dont have to do anything, ISR brings it on
   Serial.print("switch on");
   break;
     
  } // end of switch key
}// end of

Scope of “key” (you need to post code)

  char key = keypad.getKey();                 // reading the keypad

Declares a local variable named key and values it.

void whichkey (){ 
     Serial.println("whichkey routine");
   Serial.println(key, HEX );

Prints the value of the global variable named key.

They are NOT the same variable.

OK Awol , I wil post the code when I tidy it, but I did declare char key; in the initial global settings......

And Paul, I think I can see what you are saying, I am confused that I can get it to Serial.print("key read ="); Serial.println(key); OK so I thought that the variable has now been set to F or char 70 whatever, and as I had declared it globally I could use it elsewhere..

let my do some tidying up and post the whole code

Thanks Guys

We learn something every day !

I removed the char in the get key routine and now I have only one key to play with :-)

What a great forum !

Is there any preference for where to declare a variable type?
Does it “cost” more to have it globally declared, for instance?

You should only give a variable the scope it needs, no more.

I declare all my variables in the code listing before void setup().

May be overkill, but then they're all in 1 place for declaring types, initial values, I/O pin assignments. And would have eliminated the issue you saw John.

Thanks guys, perhaps I shall start with everything upfront, and once the sketch is running, start moving scope specific declarations to their functions one by one and test.

Then again, I always mean to go back and remove commented out junk, and duplicated instructions, but never seem to get the time to do so...

AWOL, is it a memory thing?

"but never seem to get the time to do so..."

Yup, sounds like a memory thing to me :D

AWOL, is it a memory thing?

Partly. Scope can be a tricky concept, particularly if, like me, your early exposure to programming was a language like BASIC which in its early forms, had no concept of scope. I try to keep variables scoped as local as possible, transferring values by function return or pointers/references. I also try to use "static" within functions for variables that need to maintain values from call-to-call, rather than always relying on global scope.

Thanks I must have a look at pointers when I eventually get some time off.