Simplifying Functions in a Sketch

Hello All, I have a keypad with 16 buttons. I want to create a separate function for each key so if I pressed key 1 it would call function key1() and so on. Do I need to initialize each function or is there a better way. Also do I need a bunch of if statements to call the various functions or is there a more elegant way? Here is my code as it stands, its only setup for keys one and seven :D.

#include <Keypad.h>
#define LED1 13
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3','e'},
  {'4','5','6','u'},
  {'7','8','9','d'},
  {'c','0','l','r'}
};
byte rowPins[ROWS] = {4, 5, 6, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {0, 1, 2, 3}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
 

void setup(){
  pinMode (LED1, OUTPUT);
  digitalWrite (LED1, LOW);
  int key1();
  int key7();
}
void loop(){
  char key = keypad.getKey();
  if (key == '1'){
    key1();
  if (key == '7'){
    key7();
     }
  }
}
void key7(){ 
  digitalWrite (LED1, HIGH);
  delay (1000);
  digitalWrite (LED1, LOW); 
}

void key1(){
  digitalWrite (LED1, HIGH);
  digitalWrite (LED1, LOW);
}

Hello All, I have a keypad with 16 buttons. I want to create a separate function for each key so if I pressed key 1 it would call function key1() and so on. Do I need to initialize each function or is there a better way. Also do I need a bunch of if statements to call the various functions or is there a more elegant way? Here is my code as it stands, its only setup for keys one and seven :D.
No code??

Well, you might need some code for it to work.

Actually, the question brings to mind something very very similar to what I am used to coming from PHP. Variable variables, for example:

$led_1 = 1;
$led_2 = 2;
$led_3 = 3;
$led_4 = 4;

In PHP, I could access the variables as such:

echo $"led_" . $ledNumber // in PHP, variables are indicated with dollar signs

The same thing can be done with function calls:

$functionName($argument);

I have wondered if there was a way to do the same thing in C. It would turn a massive if/else or switch statement into pretty much a single line.

Function pointers can cover some of that, but not all - if my reading of the PHP is correct. You'll still need some logic to setup the points and the parameters to it though, however after that you could have a single function call similar to that. But that won't really save you anything - function pointers are more for passing to other functions for them to call in a generic fashion.

G.

ps. I can spell PHP, that's about it... :wink:

Also do I need a bunch of if statements to call the various functions or is there a more elegant way?

Assuming that you can get the key # as a 4-bit binary you can use that number to index into an array of function pointers.

I can't remember the syntax off hand, something like

void (*funcs[])() = {
  func1, func2, func3 // etc
};


main () {
   k = getkeynum();
   funcs[k]();

}

func1(){};
func2(){};
func3(){};

But don't qoute me :slight_smile:

Sorry, it took me a while to figure out how to post code and I had to leave. It seems like there should be an easier way to code this but I'm still slightly figuring things out so all feedback is greatly appreciated. Thanks

Also do I need a bunch of if statements to call the various functions or is there a more elegant way?

Look up switch in the reference section. If your functions would only have a couple of lines of code, you can do them in the case. Otherwise, you can call your functions.

Graynomad did it well (except he left out some brackets...). The array keys has to be adapted for this (coding 0,1,2... rather than '0','1',...)

Also: Mind using pin 0 an 1!!!

How do I adapt the array keys? I tried deleting the single quote marks and verified it and it seems that it doesn't like the array letters. Should I use byte () to convert everything into a byte? Switch case uses bytes right? Thank you all for being so very helpful and kind.

No, the letters wouldn't work any longer. You need an index to the finction pointer array you are using. This is 0,1,2,....9,10,11,12,.... You can cheat a little bit by writing 0xA rather than 10 e.t.c.

#include <Keypad.h>
#define LED1 13
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {1,2,3,12},
  {4,5,6,13},
  {7,8,9,14},
  {10,0,11,15},
};
byte rowPins[ROWS] = {5, 6, 7, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {1, 2, 3, 4}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup(){
  pinMode (LED1, OUTPUT);
  digitalWrite (LED1, LOW);
}
void loop(){
  int key = keypad.getKey();
  switch (key);{
    case 1:
    digitalWrite (LED1, HIGH);
    delay (1000);
    digitalWrite (LED1, LOW);
    break;
    case 2:
    digitalWrite (LED1, HIGH);
    delay (3000);
    digitalWrite (LED1, LOW);
    break;
  }
}

Here's the code that has been edited to what I think your saying. It gives me an error about how "case label '1' not within a switch statement". What am I doing wrong? I'm sure that its something simple and I just can't see it. Sorry about being so clueless but I have not used this switch statement before and the reference page is rather brief.

switch (key);{

You have an extra semicolon!

wow, that fixed it. I am a total idiot :D. Thanks for your help