Button Matrix is causing issues

Status
Not open for further replies.
Hi everyone, I recently received my teensy 3.6 and audio shield and have been tinkering with it in attempts to create my synth however when using a button Matrix I'm running into some problems:

First thing I did was test the hardware with this very basic code to tell me when I'm pushing a button in the serial monitor:
Code:
#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte colPins[COLS] = {19, 18, 17};
byte rowPins[ROWS] = {23, 22, 21, 20};

Keypad keypad1 = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  char key = keypad1.getKey();

  if (key != NO_KEY){
    Serial.println(key);
  }
}

And this worked completely fine, the hardware works so no issue there. However when I tried implementing the control in a code I was using to test noises and filters controlled by the audio shield and it would stop my other operating buttons from being recognized, turn off the Teensy LED, and not activate the envelope. Here's the code (I know it's messy):

Code:
#include <Bounce.h>
#include <Keypad.h>

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioSynthWaveform       waveform1;      //xy=124,401
AudioSynthWaveform       waveform2;      //xy=127,488
AudioSynthWaveformSine   sine1;          //xy=151,312
AudioEffectDigitalCombine combine1;       //xy=322,441
AudioEffectMultiply      multiply1;      //xy=466,405
AudioEffectFreeverb      freeverb1;      //xy=608,463
AudioEffectBitcrusher    bitcrusher1;    //xy=616,370
AudioMixer4              mixer1;         //xy=783,440
AudioEffectEnvelope      envelope1;      //xy=893,556
AudioOutputI2S           i2s1;           //xy=994,420
AudioConnection          patchCord1(waveform1, 0, combine1, 0);
AudioConnection          patchCord2(waveform2, 0, combine1, 1);
AudioConnection          patchCord3(sine1, 0, multiply1, 0);
AudioConnection          patchCord4(combine1, 0, multiply1, 1);
AudioConnection          patchCord5(multiply1, bitcrusher1);
AudioConnection          patchCord6(multiply1, freeverb1);
AudioConnection          patchCord7(multiply1, 0, mixer1, 0);
AudioConnection          patchCord8(freeverb1, 0, mixer1, 2);
AudioConnection          patchCord9(bitcrusher1, 0, mixer1, 1);
AudioConnection          patchCord10(mixer1, envelope1);
AudioConnection          patchCord11(envelope1, 0, i2s1, 0);
AudioConnection          patchCord12(envelope1, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=240,630
// GUItool: end automatically generated code



Bounce button0 = Bounce(0, 15);
Bounce button1 = Bounce(1, 15);
Bounce button2 = Bounce(2, 15);
Bounce button3 = Bounce(3, 15);
Bounce button4 = Bounce(4, 15);
Bounce button5 = Bounce(5, 15);

int waveform_type = WAVEFORM_SAWTOOTH;
int waveform_type2 = WAVEFORM_SAWTOOTH;

bool isGoing = true;
bool isGoing2 = true;

const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte colPins[COLS] = {19, 18, 17};
byte rowPins[ROWS] = {23, 22, 21, 20};

Keypad keypad1 = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int a1 = 0;

void setup() {
  // put your setup code here, to run once:
  AudioMemory(160);
  
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  
  Serial.begin(115200);
  
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.8);
  
  waveform1.begin(WAVEFORM_SINE);
  waveform1.frequency(5);
  waveform1.amplitude(1);
  
  waveform2.begin(WAVEFORM_SINE);
  waveform2.frequency(10);
  waveform2.amplitude(1);
  
  sine1.amplitude(1);
  sine1.frequency(200);

  bitcrusher1.bits(5);

  freeverb1.roomsize(0.5);
  freeverb1.damping(0.3);

  envelope1.release(1500);
  envelope1.sustain(0.25);

  delay(1000);
  
  button0.update();
  button1.update();
  button2.update();
  button3.update();
  button4.update();
  button5.update();
  
  a1 = analogRead(A1);

  mixer1.gain(0, 1);
  mixer1.gain(1, 0);
  mixer1.gain(2, 0);
}

void loop() {
  // put your main code here, to run repeatedly:
  filters();
  keyCheck();

  button0.update();
  button1.update();
  button2.update();
  button3.update();
  button4.update();
  button5.update();

  if (button0.fallingEdge()) {
    Serial.print("Control waveform: ");
    if (waveform_type == WAVEFORM_SAWTOOTH) {
      waveform_type = WAVEFORM_SINE;
      Serial.println("Sine");
    } else if (waveform_type == WAVEFORM_SINE) {
      waveform_type = WAVEFORM_SQUARE;
      Serial.println("Square");
    } else if (waveform_type == WAVEFORM_SQUARE) {
      waveform_type = WAVEFORM_TRIANGLE;
      Serial.println("Triangle");
    } else if (waveform_type == WAVEFORM_TRIANGLE) {
      waveform_type = WAVEFORM_TRIANGLE_VARIABLE;
      Serial.println("Variable Triangle");
    } else if (waveform_type == WAVEFORM_TRIANGLE_VARIABLE) {
      waveform_type = WAVEFORM_ARBITRARY;
      Serial.println("Arbitrary");
    } else if (waveform_type == WAVEFORM_ARBITRARY) {
      waveform_type = WAVEFORM_SAMPLE_HOLD;
      Serial.println("Sample Hold");
    } else if (waveform_type == WAVEFORM_SAMPLE_HOLD) {
      waveform_type = WAVEFORM_PULSE;
      Serial.println("Pulse"); 
    } else if (waveform_type == WAVEFORM_PULSE) {
      waveform_type = WAVEFORM_SAWTOOTH;
      Serial.println("Sawtooth");
    }
    waveform1.begin(waveform_type);
  }
  if (button5.fallingEdge())
  {
    envelope1.noteOn();
  }
  if (button5.risingEdge())
  {
    envelope1.noteOff();
  }
  waveform1.frequency(analogRead(A1));
  waveform2.frequency(analogRead(A0));
  isGoing = false;
  isGoing2 = false;

}

void filters()
{
    if(isGoing == false)
  {
    isGoing = true;
    if(button1.risingEdge() && button2.risingEdge())
    {
      mixer1.gain(0, 1);
      mixer1.gain(1, 0);
      mixer1.gain(2, 0);
      Serial.println("no effects");
    }
    if(button1.risingEdge())
    {
      mixer1.gain(0, 0);
      mixer1.gain(1, 1);
      mixer1.gain(2, 0);
      Serial.println("bitcrusher");
    }
    else if(button1.fallingEdge())
    {
      mixer1.gain(0, 0);
      mixer1.gain(1, 0);
      mixer1.gain(2, 1);
      Serial.println("reverb");
    }
    isGoing = false;
  }
}

void keyCheck()
{
  if(isGoing2 == false)
  {
  char key = keypad1.getKey();

  if (key != NO_KEY)
  {
      if (key == '1')
      {
        envelope1.noteOn();
        Serial.println("you pressed 1");
      }
      else if (key == '2')
      {
        envelope1.noteOn();
        Serial.println("you pressed 2");
      }
      else if (key == '3')
      {
        envelope1.noteOn();
        Serial.println("you pressed 3");
      
    }
  }
  isGoing2 = false;
  }
}

I have a feeling this is a code issue as the hardware works just fine (the button matrix is this: https://www.frys.com/product/9225879?site=sr:SEARCH:MAIN_RSLT_PG by the way) and since I'm still mediocre at coding, I wonder if I could get some reccommendations.

Additionally I'm also wondering how I read multiple buttons pushed at the same time on the button matrix as I can't seem to figure that out either.

Thanks for your time !
 
Looks like a pin usage conflict.

byte colPins[COLS] = {19, 18, 17};
byte rowPins[ROWS] = {23, 22, 21, 20};

The audio shield needs pins 9, 11, 13, 18, 19, 22, 23 for the digital audio communication, 18 & 19 for configuration.

You need to connect your keypad to different pins, since 18, 19, 22, 23 are used by the audio. Also avoid pin 15.

Details on which pins the audio shield uses are here:

https://www.pjrc.com/store/teensy3_audio.html

If you're not using the SD card, pins 7, 10, 14 are ok to use.

Avoid pins 6 & 10, even if not using the SD card & optional memory chip, since those pins have pullup resistors (scroll to the end of that page for the schematic).


I'm also wondering how I read multiple buttons pushed at the same time on the button matrix

This is always dicey with a matrix that lacks diodes built in. The term to search is "ghost keys". But with that hardware caveat in mind, in Arduino click File > Examples > Keypad > MultiKey to get started.
 
Status
Not open for further replies.
Back
Top