Teensy 2.0 problems

Arduino/Teensy software/code questions and discussion
Post Reply
User avatar
NyteFury
Posts: 29
Joined: Wed Jul 20, 2016 1:14 pm
Has thanked: 2 times
Been thanked: 8 times

Teensy 2.0 problems

Post by NyteFury » Mon Sep 12, 2016 4:14 pm

I'm having a hard time getting my buttons to work. I've got a Teensy 2.0 and unlike everyone else I'm using pins 11-21. I thought I changed all the appropriate values. Did I miss any?

[spoiler="Wiring and code"]Image
Image
Image[/spoiler]

Code: Select all

#include <Bounce.h>

#define NUM_KEYS 12

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[21] = key('w', 21);
  keys[20] = key('s', 20);
  keys[19] = key('a', 19);
  keys[18] = key('d', 18);
  keys[17] = key('p', 17);
  keys[16] = key('l', 16);
  keys[15] = key('o', 15);
  keys[14] = key('k', 14);
  keys[13] = key('x', 13);
  keys[12] = key('z', 12);
  keys[11] = key('q', 11);
  keys[10] = key('e', 10);
}

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);
    }
  }
}
Gameboy Pocket Zero [in progress]: http://imgur.com/a/L87uk

User avatar
ABH
Posts: 188
Joined: Sat May 28, 2016 7:01 pm
Location: Qatar
Has thanked: 82 times
Been thanked: 113 times

Re: Teensy 2.0 problems

Post by ABH » Tue Sep 13, 2016 6:30 am

Try this code .

Code: Select all

#include <Bounce.h>

#define NUM_KEYS 12

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

Key keys[NUM_KEYS];

Key key(int 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('w', 21);
  keys[1] = key('s', 20);
  keys[2] = key('a', 19);
  keys[3] = key('d', 18);
  keys[4] = key('p', 17);
  keys[5] = key('l', 16);
  keys[6] = key('o', 15);
  keys[7] = key('k', 14);
  keys[8] = key('x', 13);
  keys[9] = key('z', 12);
  keys[10] = key('q', 11);
  keys[11] = key('e', 10);
}

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);
    }
  }
}

User avatar
NyteFury
Posts: 29
Joined: Wed Jul 20, 2016 1:14 pm
Has thanked: 2 times
Been thanked: 8 times

Re: Teensy 2.0 problems

Post by NyteFury » Tue Sep 13, 2016 4:45 pm

Thanks! That worked great, and now I know which bits of the code I shouldn't have touched haha!
Gameboy Pocket Zero [in progress]: http://imgur.com/a/L87uk

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest