You should be able to connect all of the button grounds to one ground pin on the Teensy. As for connecting the encoder, I would first get a working sketch before I hard wired it...
Again it is hard to give you much advice on how to put these pieces with each other. That is it would help to have you sort of map out what each IO pins connects to. For example in the two sketches it look like you have IO pins 5 and 6 both as buttons and connected to the encoder.
I should note, I am not doing here what I would normally do in my own code. I am resisting from using more advanced things like arrays, and functions with parameters....
But for now lets assume your encoder is on pins 5 and 6 as that is what the line: Encoder myEnc(5, 6);
Is specifying in the second sketch.
So the simplest way to combine these two sketches is to remove all of the code in the first sketch that uses pins 5 and 6. Actually I would remove all of the stuff for any pins you are not using. That is if you only have buttons on 4 pins maybe remove the other pins being defined as buttons...
But here is a quick and dirty combine of the two sketches. I also maybe put it such that the encoder may drive the left slider value... Again it compiles but have not tried...
Code:
// Hacked up combined of two example sketches. With pins 5 and 6
// not being used as buttons but instead encoder.
#include <Bounce.h>
#include <Encoder.h>
// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10); // 10 = 10 ms debounce time
Bounce button2 = Bounce(2, 10); // which is appropriate for
Bounce button3 = Bounce(3, 10); // most mechanical pushbuttons
Bounce button4 = Bounce(4, 10);
Bounce button7 = Bounce(7, 10);
Bounce button8 = Bounce(8, 10);
Bounce button9 = Bounce(9, 10);
Encoder myEnc(5, 6);
void setup() {
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
myEnc.write(512); // try to init in middle
}
void loop() {
// Update all the buttons. There should not be any long
// delays in loop(), so this runs repetitively at a rate
// faster than the buttons could be pressed and released.
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
button7.update();
button8.update();
button9.update();
// Check each button for "falling" edge.
// Update the Joystick buttons only upon changes.
// falling = high (not pressed - voltage from pullup resistor)
// to low (pressed - button connects pin to ground)
if (button0.fallingEdge()) {
Joystick.button(1, 1);
}
if (button1.fallingEdge()) {
Joystick.button(2, 1);
}
if (button2.fallingEdge()) {
Joystick.button(3, 1);
}
if (button3.fallingEdge()) {
Joystick.button(4, 1);
}
if (button4.fallingEdge()) {
Joystick.button(5, 1);
}
if (button7.fallingEdge()) {
Joystick.button(8, 1);
}
if (button8.fallingEdge()) {
Joystick.button(9, 1);
}
if (button9.fallingEdge()) {
Joystick.button(10, 1);
}
// Check each button for "rising" edge
// Update the Joystick buttons only upon changes.
// rising = low (pressed - button connects pin to ground)
// to high (not pressed - voltage from pullup resistor)
if (button0.risingEdge()) {
Joystick.button(1, 0);
}
if (button1.risingEdge()) {
Joystick.button(2, 0);
}
if (button2.risingEdge()) {
Joystick.button(3, 0);
}
if (button3.risingEdge()) {
Joystick.button(4, 0);
}
if (button4.risingEdge()) {
Joystick.button(5, 0);
}
if (button7.risingEdge()) {
Joystick.button(8, 0);
}
if (button8.risingEdge()) {
Joystick.button(9, 0);
}
if (button9.risingEdge()) {
Joystick.button(10, 0);
}
// Play around with Encoder?
long enc = myEnc.read(); // get current value
// make sure it is in the bounds for joystick
if (enc < 0) {
enc = 0;
myEnc.write(0);
} else if (enc > 1023) {
enc = 1023;
myEnc.write(1023);
}
Joystick.sliderLeft(enc);
}
But again this may not match your hardware.