The sketch is a whole project, so watch out. The section for the push button is in the loop after the potentiometer section. As for the schematic, it includes 12 potentiometers and five other push buttons. The board I'm using is a Teensy 4.1, with these being the connections as they relate to the other elements on the board:
Encoder SW pin > Teensy pin 9
Encoder GND pin > Breadboard ground pin, where one other 6mm momentary push button is connected.
I'm going across the board in the correct direction, the ground is connected to the ground, all the other components work. It's a mystery. For the record, I'm only using the encoder purely as a pushbutton for the time being. It will be replaced with a proper momentary button, so I'm not trying to achieve anything else. The encoder working is just an accident but I just want to make sure it's not damaging my board.
Here's the code. You'll notice that digitalRead(9), the one connected to the encoder, is the button that counts down in the code while digitalRead(eight) counts up. Perhaps it's triggering the pin next to it?
#include <MIDI.h>
#include <EEPROM.h>
MIDI_CREATE_DEFAULT_INSTANCE();
byte potPin[] = {24, 25, 26, 27, 38, 39, 40, 41, 14, 15, 16, 17 };
int CCChannel[] = { 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33};
int bankAddr[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};
int buttonState1;
int lastButtonState1;
int buttonState2;
int lastButtonState2;
int buttonState3;
int lastButtonState3;
int BankNumber = 1;
int FirstSelectedPresetWithinBank = 1;
void setup() {
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
MIDI.begin();
//Serial.begin(9600);
}
void loop() {
delay(30);
byte initPot[12];
byte lastInitPot[12];
byte potVal[12];
byte threshold = 1;
for(int i = 0; i < 12; i++){
initPot[i] = map(analogRead(potPin[i]), 0, 1023, 0, 127);
if (abs(initPot[i] - lastInitPot[i]) > threshold){
lastInitPot[i] = initPot[i];
// lastInitPot[i] = map(analogRead(potPin[i]), 0, 1023, 0, 127);
potVal[i] = initPot[i];
MIDI.sendControlChange(CCChannel[i], potVal[i], 1);
}
}
lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;
lastButtonState3 = buttonState3;
buttonState1 = buttonState2 = buttonState3 = HIGH;
buttonState1 = digitalRead(8);
if (buttonState1 != lastButtonState1) {
if (buttonState1 == LOW) {
FirstSelectedPresetWithinBank = FirstSelectedPresetWithinBank + 12;
if (FirstSelectedPresetWithinBank > 181) FirstSelectedPresetWithinBank = 1;
}
}
buttonState2 = digitalRead(9);
if (buttonState2 != lastButtonState2) {
if (buttonState2 == LOW) {
FirstSelectedPresetWithinBank = FirstSelectedPresetWithinBank - 12;
if (FirstSelectedPresetWithinBank < 1) FirstSelectedPresetWithinBank = 181;
}
}
buttonState3 = digitalRead(10);
if (buttonState3 != lastButtonState3) {
if (buttonState3 == LOW) {
BankNumber = BankNumber + 1;
if (BankNumber > 8) BankNumber = 1;
FirstSelectedPresetWithinBank = 1;
}
}
for(int i = 0; i < 12; i++){
/* buttonState3 = digitalRead(10);
if (buttonState3 != lastButtonState3) {
if (buttonState3 == LOW) {
EEPROM.update(bankAddr[i], potVal[i]);
// Serial.println("~~~~Saved!~~~~");
}
}*/
bankAddr[i] = BankNumber*192 + FirstSelectedPresetWithinBank + i;
//else{
if (buttonState1 != lastButtonState1){
potVal[i] = EEPROM.read(bankAddr[i]);
MIDI.sendControlChange(CCChannel[i], potVal[i], 1);
}
if (buttonState2 != lastButtonState2){
potVal[i] = EEPROM.read(bankAddr[i]);
MIDI.sendControlChange(CCChannel[i], potVal[i], 1);
}
}
for(int i = 0; i < 12; i++){
MIDI.sendControlChange(CCChannel[i], potVal[i], 1);
Serial.print( "ADDR:");
Serial.println(bankAddr[i]);
Serial.print( " ");
Serial.print( "POTVAL");
Serial.println(potVal[i]);
Serial.println( " ");
}
// Serial.println("~~~ ~~~");
//delay(2000);
}