Teensy LC help

Arduino/Teensy software/code questions and discussion
Post Reply
tmscaf
Posts: 2
Joined: Tue May 24, 2016 11:42 pm

Teensy LC help

Post by tmscaf » Wed May 25, 2016 1:54 am

Hi everyone,

I'm having an issues getting my X , Y buttons to work. I found some teensy code for a gamepad on the net and altered it to reflect the correct pins. This worked fine until I drilled the two additional holes and added X and Y. They respond in notepad but aren't releasing correctly as a normal key press would. Below is my code. I'd love some help troubleshooting it or simply using someone elses that has has been proven to work.

#include <Bounce.h>

Bounce bUp = Bounce(11, 20);
Bounce bDown = Bounce(10, 20);
Bounce bLeft = Bounce(12, 20);
Bounce bRight = Bounce(9, 20);

Bounce bA = Bounce(5, 20);
Bounce bB = Bounce(6, 20);
Bounce bX = Bounce(3, 20);
Bounce bY = Bounce(4, 20);

Bounce bL1 = Bounce(2, 20);
Bounce bR1 = Bounce(1, 20);

Bounce bStart = Bounce(7, 20);
Bounce bSelect = Bounce(8, 20);

int hidMode = 0;

boolean mLeft = false;
boolean mRight = false;

void setup() {
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);

delay(500);
}

void loop() {
updateButtons();
processFallingEdges();
processRisingEdges();
}

void updateButtons() {
bUp.update();
bDown.update();
bLeft.update();
bRight.update();
bA.update();
bB.update();
bX.update();
bY.update();
bStart.update();
bSelect.update();
bL1.update();
bR1.update();
}

void processFallingEdges() {
if ((bSelect.read() == LOW) && (bUp.read() == LOW)) {
hidMode = ((hidMode) ? 0 : 1);
Keyboard.set_key1(0);
Keyboard.set_key2(0);
Keyboard.set_key3(0);
Keyboard.set_key4(0);
Keyboard.set_key5(0);
Keyboard.set_key6(0);
Keyboard.send_now();
delay(500);
}

if ((bSelect.read() == LOW) && (bDown.read() == LOW)) {
Keyboard.set_key1(KEY_ESC);
Keyboard.set_key2(0);
Keyboard.set_key3(0);
Keyboard.set_key4(0);
Keyboard.set_key5(0);
Keyboard.set_key6(0);
Keyboard.send_now();
delay(100);
Keyboard.set_key1(0);
Keyboard.send_now();
delay(500);
return;
}

if (hidMode == 0) {
if (bUp.fallingEdge()) {
Keyboard.set_key1(KEY_U);
Keyboard.send_now();
}

if (bDown.fallingEdge()) {
Keyboard.set_key1(KEY_D);
Keyboard.send_now();
}

if (bLeft.fallingEdge()) {
Keyboard.set_key2(KEY_L);
Keyboard.send_now();
}

if (bRight.fallingEdge()) {
Keyboard.set_key2(KEY_R);
Keyboard.send_now();
}

if (bA.fallingEdge()) {
Keyboard.set_key3(KEY_A);
Keyboard.send_now();
}

if (bB.fallingEdge()) {
Keyboard.set_key4(KEY_B);
Keyboard.send_now();
}

if (bX.fallingEdge()) {
Keyboard.set_key6(KEY_X);
Keyboard.send_now();
}

if (bY.fallingEdge()) {
Keyboard.set_key5(KEY_Y);
Keyboard.send_now();
}

if (bStart.fallingEdge()) {
Keyboard.set_key5(KEY_S);
Keyboard.send_now();
}

if (bSelect.fallingEdge()) {
Keyboard.set_key6(KEY_E);
Keyboard.send_now();
}
}

if (hidMode == 1) {
int moveSpeed = max(((bUp.read() == LOW) ? bUp.duration() : 0), ((bDown.read() == LOW) ? bDown.duration() : 0));
moveSpeed = max(moveSpeed, max(((bLeft.read() == LOW) ? bLeft.duration() : 0), ((bRight.read() == LOW) ? bRight.duration() : 0)));
moveSpeed = min(1 + (moveSpeed / 250), 10);

if (bUp.read() == LOW) {
Mouse.move(0, -moveSpeed);
}

if (bDown.read() == LOW) {
Mouse.move(0, moveSpeed);
}

if (bLeft.read() == LOW) {
Mouse.move(-moveSpeed, 0);
}

if (bRight.read() == LOW) {
Mouse.move(moveSpeed, 0);
}

if (bA.fallingEdge()) {
mLeft = true;
Mouse.set_buttons(mLeft, false, mRight);
}

if (bB.fallingEdge()) {
mRight = true;
Mouse.set_buttons(mLeft, false, mRight);
}

if (bX.fallingEdge()) {
mLeft = true;
Mouse.set_buttons(mLeft, false, mRight);
}

if (bB.fallingEdge()) {
mRight = true;
Mouse.set_buttons(mLeft, false, mRight);
}

delay(10);
}
}

void processRisingEdges() {
if (hidMode == 0) {
if ((bUp.risingEdge()) || (bDown.risingEdge())) {
Keyboard.set_key1(0);
Keyboard.send_now();
}

if ((bLeft.risingEdge()) || (bRight.risingEdge())) {
Keyboard.set_key2(0);
Keyboard.send_now();
}

if (bA.risingEdge()) {
Keyboard.set_key3(0);
Keyboard.send_now();
}

if (bB.risingEdge()) {
Keyboard.set_key4(0);
Keyboard.send_now();
}

if ((bStart.risingEdge()) || (bX.risingEdge())) {
Keyboard.set_key5(0);
Keyboard.send_now();
}

if (bSelect.risingEdge()) || (bY.risingEdge())) {
Keyboard.set_key6(0);
Keyboard.send_now();
}
}

if (hidMode == 1) {
if (bA.risingEdge()) {
mLeft = false;
Mouse.set_buttons(mLeft, false, mRight);
}

if (bB.risingEdge()) {
mRight = false;
Mouse.set_buttons(mLeft, false, mRight);
}
}
}

jonnybravo
Posts: 4
Joined: Fri Jul 22, 2016 9:31 pm
Has thanked: 1 time

Re: Teensy LC help

Post by jonnybravo » Fri Jul 22, 2016 11:28 pm

I'm currently having trouble compiling the teensy code provided by wermy. Whenever i click the check mark in the corner of the arduino software it shows its compiling the sketch and then pops an error that "'key' does not name a type". has anyone else had this same problem? I'm very new to the whole coding thing and don't quite understand it all.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests