Page 1 of 1

Teensy 2.0 problems

Posted: Mon Sep 12, 2016 4:14 pm
by NyteFury
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);
    }
  }
}

Re: Teensy 2.0 problems

Posted: Tue Sep 13, 2016 6:30 am
by ABH
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);
    }
  }
}

Re: Teensy 2.0 problems

Posted: Tue Sep 13, 2016 4:45 pm
by NyteFury
Thanks! That worked great, and now I know which bits of the code I shouldn't have touched haha!