teensy buttoner not compiling in sketch

Hi there,

Im trying to get this code to work for my project but it's not compiling. Im new to coding so need a bit of help working out if anythings wrong. The code has been tested so not entirely sure why its not working on my sketch... any help would be greatly appreciated :slight_smile:

Code:

https://github.com/selfmodified/TeensyButtoner/blob/master/TeensyButtoner.ino

Error message:

teensybuttoner:110: error: 'myset_key6' was not declared in this scope
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};
^
teensybuttoner:110: error: 'myset_key5' was not declared in this scope
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};
^
teensybuttoner:110: error: 'myset_key4' was not declared in this scope
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};
^
teensybuttoner:110: error: 'myset_key3' was not declared in this scope
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};
^
teensybuttoner:110: error: 'myset_key2' was not declared in this scope
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};
^
teensybuttoner:110: error: 'myset_key1' was not declared in this scope
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};
^
C:\Users\alexander\Documents\Arduino\teensybuttoner\teensybuttoner.ino: In function 'void setup()':
C:\Users\alexander\Documents\Arduino\teensybuttoner\teensybuttoner.ino:103:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define NUMBUTTONS sizeof(buttons)
^
C:\Users\alexander\Documents\Arduino\teensybuttoner\teensybuttoner.ino:135:21: note: in expansion of macro 'NUMBUTTONS'
for (int i=0; i < NUMBUTTONS; i++) {
^
C:\Users\alexander\Documents\Arduino\teensybuttoner\teensybuttoner.ino: In function 'void sendEscape()':
C:\Users\alexander\Documents\Arduino\teensybuttoner\teensybuttoner.ino:294:35: warning: large integer implicitly truncated to unsigned type [-Woverflow]
Keyboard.set_key1(ESCAPE_KEY);
^
exit status 1
'myset_key1' was not declared in this scope

It would be much more helpful if you posted the code here as described in read this before posting a programming question

I did try to post to code as described but it exceeded the 9000 character limit per post :frowning:

3etamax:
I did try to post to code as described but it exceeded the 9000 character limit per post :frowning:

In which case you can break it into a few pieces and post them individually (you probably have to wait an annoying 5 or 10 minutes between posts due to low post count) or attach the file to your post.

Thanks, attach the file! sorry!

teensy buttoner.txt (9.74 KB)

Move the functions above the line where the pointers are stored in the array.

thanks for responding. do you mean like this?

int keySlot = sizeof(keyList) / sizeof(KeyFunction_t*);
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};

#define NUMBUTTONS sizeof(buttons)
typedef void KeyFunction_t(uint8_t c);
#if OUTPUT_MODE == MODE_JOY 
  int buttonActive[NUMBUTTONS];
#else
  KeyFunction_t* buttonActive[NUMBUTTONS];
#endif

I get a new error message further down:

TeensyButtoner:110: error: 'keyList' was not declared in this scope
int keySlot = sizeof(keyList) / sizeof(KeyFunction_t*);
^
TeensyButtoner:110: error: 'KeyFunction_t' was not declared in this scope
int keySlot = sizeof(keyList) / sizeof(KeyFunction_t*);
^
TeensyButtoner:110: error: expected primary-expression before ')' token
int keySlot = sizeof(keyList) / sizeof(KeyFunction_t*);
^
TeensyButtoner:111: error: 'KeyFunction_t' does not name a type
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};
^
C:\Users\alexander\Documents\Teensy 2.0\TeensyButtoner-master\TeensyButtoner\TeensyButtoner.ino: In function 'void setup()':
C:\Users\alexander\Documents\Teensy 2.0\TeensyButtoner-master\TeensyButtoner\TeensyButtoner.ino:113:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define NUMBUTTONS sizeof(buttons)
^
C:\Users\alexander\Documents\Teensy 2.0\TeensyButtoner-master\TeensyButtoner\TeensyButtoner.ino:145:21: note: in expansion of macro 'NUMBUTTONS'
for (int i=0; i < NUMBUTTONS; i++) {
^
C:\Users\alexander\Documents\Teensy 2.0\TeensyButtoner-master\TeensyButtoner\TeensyButtoner.ino: In function 'void sendEscape()':
C:\Users\alexander\Documents\Teensy 2.0\TeensyButtoner-master\TeensyButtoner\TeensyButtoner.ino:304:35: warning: large integer implicitly truncated to unsigned type [-Woverflow]
Keyboard.set_key1(ESCAPE_KEY);
^
C:\Users\alexander\Documents\Teensy 2.0\TeensyButtoner-master\TeensyButtoner\TeensyButtoner.ino: In function 'void activateButton(byte)':
TeensyButtoner:328: error: 'keyList' was not declared in this scope
buttonActive[index] = keyList[keySlot]; //Associate the keySlot function pointer with the button
^
C:\Users\alexander\Documents\Teensy 2.0\TeensyButtoner-master\TeensyButtoner\TeensyButtoner.ino: In function 'void releaseButton(byte)':
TeensyButtoner:346: error: 'keyList' was not declared in this scope
keyList[keySlot] = buttonActive[index]; //retrieve the keySlot function pointer
^
exit status 1
'keyList' was not declared in this scope

Hi there,

Still struggling with this code, if anyone could assist it would be greatly appreciated! :slight_smile:

The intention of Paul_S' suggestion was to move the below

void myset_key1(uint8_t c)
{
  Keyboard.set_key1(c);
}
void myset_key2(uint8_t c)
{
  Keyboard.set_key2(c);
}
void myset_key3(uint8_t c)
{
  Keyboard.set_key3(c);
}
void myset_key4(uint8_t c)
{
  Keyboard.set_key4(c);
}
void myset_key5(uint8_t c)
{
  Keyboard.set_key5(c);
}
void myset_key6(uint8_t c)
{
  Keyboard.set_key6(c);
}

to before the below part of your code

#define NUMBUTTONS sizeof(buttons)
typedef void KeyFunction_t(uint8_t c);
#if OUTPUT_MODE == MODE_JOY 
  int buttonActive[NUMBUTTONS];
#else
  KeyFunction_t* buttonActive[NUMBUTTONS];
#endif
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};
int keySlot = sizeof(keyList) / sizeof(KeyFunction_t*);

In general, the compiler needs to know what functions 'look like' before it can use it; which parameters, what does it return). Your functions like myset_key6 are defined at the end of the code and hence the compiler does not know what they 'look like'. The IDE tries to be intelligent but failed in this case. Moving them up to before where they are used first solves the issue.

The alternative is to specify the prototypes of those functions before they are used in the code; in that case you can leave the functions themselves where they were and add the prototypes as shown below.

// prototypes
void myset_key1(uint8_t c);
void myset_key2(uint8_t c);
void myset_key3(uint8_t c);
void myset_key4(uint8_t c);
void myset_key5(uint8_t c);
void myset_key6(uint8_t c);


#define NUMBUTTONS sizeof(buttons)
typedef void KeyFunction_t(uint8_t c);
#if OUTPUT_MODE == MODE_JOY 
  int buttonActive[NUMBUTTONS];
#else
  KeyFunction_t* buttonActive[NUMBUTTONS];
#endif
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};
int keySlot = sizeof(keyList) / sizeof(KeyFunction_t*);

The above tells the compiler what your functions 'look like'; it does not define what they do though and hence you still need them at the end of your code.

This is the preferred way as you might want to split your code in future into multiple cpp files and prototypes allow for that.

Thanks so much for your help. Managed to get this code working by using your suggestion on prototypes :slight_smile: