MIDI Controller code issues Teensy 3.5

Hey all,

Im having some issues with my MIDI Controller code. My goal for is to have 6 pots that send CC, the CC values should be able to be changed with the press of one of two buttons. The buttons should change the CC of the pots by accessing a multi dimensional array consisting of CC values which should change the CC that all 6 pots are controlling essentially changing the page of CC values. This will be indicated by an OLED screen. The issues im having are that my pots seemingly aren't being read, or sending CC. And my buttons aren't adding or removing one from my counter, instead random values are churning out. I would really appreciate some advice on how to fix these issues and possibly get some guidance on getting this project in working order.

Here is the code

#include <MIDIUSB.h>
#include <MIDI.h>
#include "U8glib.h"
U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9);
void printArray ( const int [][ 6 ] ); // prototype
const int rows = 4;
const int columns = 6;
int buttonleft = 0;
int buttonright = 1;
 int buttonPushCounter = 0;
boolean buttonState = LOW;         // current state of the button
boolean lastButtonState = LOW;     // previous state of the button
const int NPots = 6;
int screenpos = 0;
const int potPin[NPots] = {A9, A8, A7, A6, A5, A4};
int potVal[NPots];
int potCState[NPots] = {0}; // Current state of the pot; 
int potPState[NPots] = {0}; // Previous state of the pot; 
int PTime[NPots] = {0};
int timer[NPots] = {0};
int TIMEOUT = 20 ;
int potVar = 0;
int varThreshold = 1;
int pot1cc = 0 ;  
int pot2cc = 1 ;
int pot3cc = 2 ;
int pot4cc = 3 ;
int pot5cc = 4 ;
int pot6cc = 5 ; 
int midiCState[NPots] = {0}; // Current state of the midi value; delete 0 if 0 pots
int midiPState[NPots] = {0}; // Previous state of the midi value; delete 0 if 0 pots// microKorg CC's 27= LFO1 freq, 76= LFO2 freq, 12= MOD fx LFOspeed 93= MOD fx depth, 13= Delay time, 94= Delay depth 
int ccpos [11][6] = { { 1, 2, 3, 4, 5, 6 }, { 7, 8, 9, 10, 11, 12 }, { 13, 14, 15, 16, 17, 18 }, { 19, 20, 21, 22, 23, 24}, {25, 26, 27, 28, 29, 30}, {31, 32, 33, 34, 35, 36}, {37, 38, 39, 40, 41, 42}, {43, 44, 45, 46, 47, 48}, {49, 50, 51, 52, 53, 54}, {55, 56, 57, 58, 59, 60}, {27, 76, 12, 93, 13, 94} };
char *menu[] = {"pg 01", "pg 02", "pg 03", "pg 04", "pg 05", "pg 06", "pg 07", "pg 08", "pg 09", "pg 10", "pg 11",};

byte midiCh = 1; //* MIDI channel to be used
byte note = 12; //* Lowest note to be used; 36 = C2; 60 = Middle C
byte cc = 1; //* Lowest MIDI CC to be used

void draw(void) {
 // graphic commands to redraw the complete screen should be placed here
 u8g.setFont(u8g_font_courB24);
 //u8g.setFont(u8g_font_osb21);
 u8g.drawStr( 15, 40, menu[screenpos]);
}


void setup() {
  //set up pots
for(int i = 0; i < NPots; i++)
{
  pinMode(potPin[i], INPUT);
}
pinMode(buttonleft, INPUT_PULLUP);  //sets up buttons
pinMode(buttonright, INPUT_PULLUP);



}

void loop() {
//read the pots and check if theyre different form last time
//if its different send out MIDI
Serial.begin(9600);
 for (int i = 0; i < NPots; i++)
 {
  potVal[i] = analogRead(potPin[i]);
  if(potVal[i] != potPState[i])
  midiCState[i] = map(potCState[i], 0, 1023, 0, 127); // Maps the reading of the potCState to a value usable in midi
  potVar = abs(potCState[i] - potPState[i]); // Calculates the absolute value between the difference between the current and previous state of the pot
   if (potVar > varThreshold) { // Opens the gate if the potentiometer variation is greater than the threshold
      PTime[i] = millis(); // Stores the previous time
   }
timer[i] = millis() - PTime[i]; // Resets the timer 11000 - 11000 = 0ms
    
    boolean potMoving; 
    if (timer[i] < TIMEOUT) { // If the timer is less than the maximum allowed time it means that the potentiometer is still moving
      potMoving = true;
    }
    else {
      potMoving = false;
    }

    if (potMoving == true) { // If the potentiometer is still moving, send the change control
      if (midiPState[i] != midiCState[i]) {
  
    controlChange(1, ccpos [screenpos][i], potVal[i]);
    MidiUSB.flush();
   }
 }
 }


 // read the state of the pushbutton value:

  buttonState = digitalRead(buttonright);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
        // if the current state is HIGH then the button
      // went from off to on:
      screenpos++;  // add one to counter
      
      if (screenpos > 11) 
      {
        screenpos = 0;
      }
      Serial.println(screenpos);
     
    }
  }
   buttonState = digitalRead(buttonleft);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
        // if the current state is HIGH then the button
      // went from off to on:
      screenpos--;  // remove one from counter
      
      if (screenpos < 1) 
      {
        screenpos = 11;
      }
    }
  }
  
}
// Arduino MIDI functions MIDIUSB Library
void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  MidiUSB.sendMIDI(event);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.