Page 1 of 1

Help a noob with code for Teensy sketch?

Posted: Thu May 03, 2018 9:03 pm
by BlastoSupreme
Hello everyone,

I recently picked up Erik_Gee's common ground board (its great by the way :D ) and it has more buttons than the ABXY that is in Wermy's Teensy sketch in the written guide. I have no experience with programming at all. I tried to change it to add C, Z, F4 (for the function button), and the arrow keys for the D pad. I also wanted it to be in the same order on the board so I could use a ribbon cable and have it be flat. When I click the verify button it comes up with several problems at the bottom of the screen. I pasted those below.

Code: Select all

Game_Boy_Zero: In function 'void setupKeys()':
Game_Boy_Zero:22: warning: large integer implicitly truncated to unsigned type 
   keys[0] = key(KEY_F4, 0);
                          ^
Game_Boy_Zero:25: warning: large integer implicitly truncated to unsigned type 
   keys[3] = key(KEY_LEFT_ARROW, 3);
                                  ^
Game_Boy_Zero:26: warning: large integer implicitly truncated to unsigned type 
   keys[4] = key(KEY_UP_ARROW, 4);
                                ^
Game_Boy_Zero:27: warning: large integer implicitly truncated to unsigned type 
   keys[5] = key(KEY_DOWN_ARROW, 5);
                                  ^
Game_Boy_Zero:28: warning: large integer implicitly truncated to unsigned type 
   keys[6] = key(KEY_RIGHT_ARROW, 6);
                                   ^
Game_Boy_Zero:30: warning: large integer implicitly truncated to unsigned type 
   keys[8] = key(KEY_ENTER, 8);
                             ^
Sketch uses 9236 bytes (14%) of program storage space. Maximum is 63488 bytes.
Global variables use 3704 bytes (45%) of dynamic memory, leaving 4488 bytes for local variables. Maximum is 8192 bytes. 
Here is the sketch I edited.

Code: Select all

#include <Bounce.h>

#define NUM_KEYS 15

struct Key {
  char keycode;
  Bounce* bounce;
};

Key keys[NUM_KEYS];

Key key(char keycode, int pin) {
  Key *ret = new Key;
  ret->keycode = keycode;
  ret->bounce = new Bounce(pin, 10);
  pinMode(pin, INPUT_PULLUP);
  return *ret;
}

void setupKeys() {
  keys[0] = key(KEY_F4, 0);
  keys[1] = key('r', 1);
  keys[2] = key('l', 2);
  keys[3] = key(KEY_LEFT_ARROW, 3);
  keys[4] = key(KEY_UP_ARROW, 4);
  keys[5] = key(KEY_DOWN_ARROW, 5);
  keys[6] = key(KEY_RIGHT_ARROW, 6);
  keys[7] = key('s', 7);
  keys[8] = key(KEY_ENTER, 8);
  keys[9] = key('z', 9);
  keys[10] = key('c', 10);
  keys[11] = key('x', 11);
  keys[12] = key('y', 12);
  keys[13] = key('b', 13);
  keys[14] = key('a', 14);
}

void setup() {
  setupKeys();
  Keyboard.begin();
  //  pinMode(0, INPUT_PULLUP);
}

void loop() {
  for (int i = 0; i < NUM_KEYS; i++) {
    keys[i].bounce->update();
    if (keys[i].bounce->fallingEdge()) {
      Keyboard.press(keys[i].keycode);
    } else if (keys[i].bounce->risingEdge()) {
      Keyboard.release(keys[i].keycode);
    }
  }
}
I would greatly appreciate any help you could provide. ^_^

Re: Help a noob with code for Teensy sketch?

Posted: Sun May 06, 2018 2:23 pm
by erik_gee
What happens when you actually upload it to the board? Warnings won't prevent you from uploading, only an error will.

I usually see that warning when you try to pass a larger bit value into a smallee one. Such as passing a 16bit integer into and 8bit variable. It's just warning that it's cutting off the extra bits.

Try uploading the board and post back with what it's doing

Re: Help a noob with code for Teensy sketch?

Posted: Sun May 06, 2018 4:57 pm
by GunZi
Change

Code: Select all

struct Key {
  char keycode;
  Bounce* bounce;
};
into this

Code: Select all

struct Key {
  unsigned long int keycode;
  Bounce* bounce;
};
and change this

Code: Select all

Key key(char keycode, int pin) {
into this

Code: Select all

Key key(unsigned long int keycode, int pin) {
This worked when I had the same problem as you :)

Re: Help a noob with code for Teensy sketch?

Posted: Sun May 06, 2018 8:03 pm
by erik_gee
GunZi wrote:
Sun May 06, 2018 4:57 pm
Change

Code: Select all

struct Key {
  char keycode;
  Bounce* bounce;
};
into this

Code: Select all

struct Key {
  unsigned long int keycode;
  Bounce* bounce;
};
and change this

Code: Select all

Key key(char keycode, int pin) {
into this

Code: Select all

Key key(unsigned long int keycode, int pin) {
This worked when I had the same problem as you :)
Yup, that way it won't truncate the extra bits as they will be of the same size

Re: Help a noob with code for Teensy sketch?

Posted: Sun May 06, 2018 9:01 pm
by BlastoSupreme
Hey,

So I loaded it into the teensy and the buttons came back like this:

Up: R
Right: O
Down: Q
Left: P
Select: s
Start: (
Z: z
C: c
Y: y
X: x
B: b
A: a
L: l
R: r
F4: =

Im going to add the suggested changes and see what happens.

Re: Help a noob with code for Teensy sketch?

Posted: Sun May 06, 2018 9:08 pm
by BlastoSupreme
It worked!!! Yay! This is so exciting! Thank you both for your help!