I am testing out this multiplexer code I wrote. If I manually set the selector pins to Y0, Y1, Y2, etc... each individual button functions properly. However, when I use the PORTB code to cycle through the selector pins, my serial monitor just returns a constant stream of 0,1,0,1,0,1, at the speed of the debounce, which I believe is just the alternating Serial.println of my if statements. I am guessing it has something to do with the "i" variables not properly storing values as intended, but I am not sure.
This is a Leonardo board, 16 channel mux, selector pins on 8-11, digital input on pin 7.
thanks
#include <frequencyToNote.h>
#include <MIDIUSB.h>
#include <pitchToFrequency.h>
#include <pitchToNote.h>
// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).
// controlChange(0, 10, 65); // Set the value of controller 10 on channel 0 to 65
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
}
#define numPots 0
#define numButtons 4
int MP1_PIN1[4] = {0,0,1,1};
int MP1_PIN2[4] = {0,1,0,1};
int MP1_PIN3[4] = {0,0,1,0};
int MP1_PIN4[4] = {0,0,0,0};
//0,0,0,0 is button 1
//0,1,0,0 is button 2
//1,0,1,0 is button 3
//1,0,0,0 is button 4
//1,1,0,0 is also button 4?
int lastButtonState[numButtons];
int lastPotValue[numPots];
unsigned long lastDebounce = 0;
int potValue[numPots];
int buttonValue[numButtons];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(7, INPUT_PULLUP);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
unsigned long currentTime = millis();
for (int i=0; i<numButtons;i++){
PORTB = (PORTB & B00001111) | (i<<4);
// digitalWrite(8, MP1_PIN1[i]);
// digitalWrite(9, MP1_PIN2[i]);
// digitalWrite(10, MP1_PIN3[i]);
// digitalWrite(11, MP1_PIN4[i]);
buttonValue[i] = digitalRead(7);
if (buttonValue[i] == LOW && currentTime - lastDebounce > 500){
if (lastButtonState[i] == 0){
controlChange(1,51+i,127);
MidiUSB.flush();
Serial.println(lastButtonState[i]);
lastButtonState[i] = 1;
lastDebounce = currentTime;
}
else {
controlChange(1,51+i,0);
MidiUSB.flush();
Serial.println(lastButtonState[i]);
lastButtonState[i] = 0;
lastDebounce = currentTime;
}
}
}
}