Library Problems?

Peetem

Member
OK -

The code looks good to me - I'm trying to build a midi controller but get compiler problems. The libraries are all installed. But I get these complier errors:

Code:
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino: In function 'void setup()':
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino:39:3: error: 'MIDI' was not declared in this scope
   MIDI.begin(MIDI_CHANNEL_OFF);
   ^~~~
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino:39:3: note: suggested alternative: 'CID'
   MIDI.begin(MIDI_CHANNEL_OFF);
   ^~~~
   CID
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino:74:3: error: 'loadBank' was not declared in this scope
   loadBank(activeBank);
   ^~~~~~~~
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino:74:3: note: suggested alternative: 'saveBank'
   loadBank(activeBank);
   ^~~~~~~~
   saveBank
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino: In function 'void loop()':
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino:91:3: error: 'MIDI' was not declared in this scope
   MIDI.read();
   ^~~~
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino:91:3: note: suggested alternative: 'CID'
   MIDI.read();
   ^~~~
   CID
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino: In function 'void checkButtons()':
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino:99:7: error: 'MIDI' was not declared in this scope
       MIDI.sendControlChange(i + 1, ccValue, midiChannel);
       ^~~~
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino:99:7: note: suggested alternative: 'CID'
       MIDI.sendControlChange(i + 1, ccValue, midiChannel);
       ^~~~
       CID
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino:110:7: error: 'MIDI' was not declared in this scope
       MIDI.sendProgramChange(pcValue, midiChannel);
       ^~~~
/Users/Family/Documents/Arduino/Complex_Midi_Controller/Controller_Design_NOT_WORKING/Controller_Design_NOT_WORKING.ino:110:7: note: suggested alternative: 'CID'
       MIDI.sendProgramChange(pcValue, midiChannel);
       ^~~~
       CID

exit status 1

Compilation error: 'MIDI' was not declared in this scope


Here's the code:

Code:
#include <MIDI.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>

// MIDI pins
const int ccButtonPins[] = {22, 23, 24, 25, 26, 27, 28, 29};
const int pcButtonPins[] = {30, 31, 32, 33};
const int ledPins[] = {38, 39, 40, 41, 42, 43, 44, 45};
const int bankLedPins[] = {46, 47, 48, 49};
const int bankUpButtonPin = 50;
const int bankDownButtonPin = 51;
const int saveButtonPin = 52;
const int deleteButtonPin = 53;
const int lcdAddress = 0x27;  // Change this if your I2C address is different
const int lcdColumns = 20;
const int lcdRows = 4;

// MIDI channel
const int midiChannel = 1;

// Bank selection
const int numBanks = 4;
int activeBank = 0;

// LCD display
LiquidCrystal_I2C lcd(lcdAddress, lcdColumns, lcdRows);

// SD card
const int chipSelectPin = 53;

// Flags
bool saveMode = false;
bool deleteMode = false;
bool confirmSave = false;
bool confirmDelete = false;

void setup() {
  // Initialize MIDI
  MIDI.begin(MIDI_CHANNEL_OFF);

  // Set button pins as inputs
  for (int i = 0; i < 8; i++) {
    pinMode(ccButtonPins[i], INPUT_PULLUP);
  }
  for (int i = 0; i < 4; i++) {
    pinMode(pcButtonPins[i], INPUT_PULLUP);
  }
  pinMode(bankUpButtonPin, INPUT_PULLUP);
  pinMode(bankDownButtonPin, INPUT_PULLUP);
  pinMode(saveButtonPin, INPUT_PULLUP);
  pinMode(deleteButtonPin, INPUT_PULLUP);

  // Set LED pins as outputs
  for (int i = 0; i < 8; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  for (int i = 0; i < 4; i++) {
    pinMode(bankLedPins[i], OUTPUT);
  }

  // Initialize LCD display
  lcd.begin(lcdColumns, lcdRows);
  lcd.clear();

  // Initialize SD card
  if (!SD.begin(chipSelectPin)) {
    lcd.print("SD Card Error");
    while (true) {
      // Stay in an infinite loop if SD card initialization fails
    }
  }

  // Load initial bank configuration
  loadBank(activeBank);

  // Display initial bank
  displayBank();
}

void loop() {
  // Check for button presses
  checkButtons();

  // Handle save and delete confirmation
  handleConfirmation();

  // Update the active bank
  updateBank();

  // Process MIDI messages
  MIDI.read();
}

void checkButtons() {
  // Check CC buttons
  for (int i = 0; i < 8; i++) {
    if (digitalRead(ccButtonPins[i]) == LOW) {
      int ccValue = map(i, 0, 7, 0, 127);
      MIDI.sendControlChange(i + 1, ccValue, midiChannel);
      digitalWrite(ledPins[i], HIGH);
      delay(50);  // Button debounce
      digitalWrite(ledPins[i], LOW);
    }
  }

  // Check PC buttons
  for (int i = 0; i < 4; i++) {
    if (digitalRead(pcButtonPins[i]) == LOW) {
      int pcValue = i;
      MIDI.sendProgramChange(pcValue, midiChannel);
      digitalWrite(ledPins[i + 8], HIGH);
      delay(50);  // Button debounce
      digitalWrite(ledPins[i + 8], LOW);
    }
  }

  // Check bank selection buttons
  if (digitalRead(bankUpButtonPin) == LOW) {
    activeBank++;
    if (activeBank >= numBanks) {
      activeBank = 0;
    }
    displayBank();
    delay(300);  // Button debounce
  }

  if (digitalRead(bankDownButtonPin) == LOW) {
    activeBank--;
    if (activeBank < 0) {
      activeBank = numBanks - 1;
    }
    displayBank();
    delay(300);  // Button debounce
  }

  // Check save button
  if (digitalRead(saveButtonPin) == LOW) {
    if (saveMode) {
      if (!confirmSave) {
        lcd.setCursor(0, 1);
        lcd.print("Confirm Save?");
        confirmSave = true;
        delay(1000);  // Button debounce
      } else {
        lcd.setCursor(0, 1);
        lcd.print("Saving...      ");
        saveBank(activeBank);
        delay(1000);  // Give user time to read the message
        lcd.clear();
        saveMode = false;
        confirmSave = false;
      }
    } else {
      lcd.setCursor(0, 1);
      lcd.print("Save Bank ");
      lcd.print(activeBank + 1);
      saveMode = true;
      delay(1000);  // Button debounce
    }
  }

  // Check delete button
  if (digitalRead(deleteButtonPin) == LOW) {
    if (deleteMode) {
      if (!confirmDelete) {
        lcd.setCursor(0, 1);
        lcd.print("Confirm Delete?");
        confirmDelete = true;
        delay(1000);  // Button debounce
      } else {
        lcd.setCursor(0, 1);
        lcd.print("Deleting...    ");
        deleteBank(activeBank);
        delay(1000);  // Give user time to read the message
        lcd.clear();
        deleteMode = false;
        confirmDelete = false;
      }
    } else {
      lcd.setCursor(0, 1);
      lcd.print("Delete Bank ");
      lcd.print(activeBank + 1);
      deleteMode = true;
      delay(1000);  // Button debounce
    }
  }
}

void handleConfirmation() {
  if (confirmSave || confirmDelete) {
    if (digitalRead(saveButtonPin) == LOW || digitalRead(deleteButtonPin) == LOW) {
      confirmSave = false;
      confirmDelete = false;
      lcd.clear();
      delay(1000);  // Button debounce
    }
  }
}

void updateBank() {
  static int prevBank = -1;
  if (prevBank != activeBank) {
    prevBank = activeBank;
    displayBank();
  }
}

void displayBank() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Bank: ");
  lcd.print(activeBank + 1);
  lcd.setCursor(0, 1);
  lcd.print("CC: ");
  for (int i = 0; i < 8; i++) {
    if (buttonInBank(i, activeBank)) {
      lcd.print("1 ");
    } else {
      lcd.print("0 ");
    }
  }
  lcd.setCursor(0, 2);
  lcd.print("PC: ");
  for (int i = 0; i < 4; i++) {
    if (buttonInBank(i + 8, activeBank)) {
      lcd.print("1 ");
    } else {
      lcd.print("0 ");
    }
  }
}

void saveBank(int bank) {
  File bankFile = SD.open(getBankFilename(bank), FILE_WRITE);
  if (bankFile) {
    for (int i = 0; i < 8; i++) {
      if (buttonInBank(i, bank)) {
        bankFile.println("CC " + String(i + 1));
      }
    }
    for (int i = 0; i < 4; i++) {
      if (buttonInBank(i + 8, bank)) {
        bankFile.println("PC " + String(i));
      }
    }
    bankFile.close();
    lcd.setCursor(0, 1);
    lcd.print("Bank Saved");
  } else {
    lcd.setCursor(0, 1);
    lcd.print("Save Error");
  }
}

void deleteBank(int bank) {
  if (SD.remove(getBankFilename(bank))) {
    lcd.setCursor(0, 1);
    lcd.print("Bank Deleted");
  } else {
    lcd.setCursor(0, 1);
    lcd.print("Delete Error");
  }
}

bool buttonInBank(int button, int bank) {
  File bankFile = SD.open(getBankFilename(bank), FILE_READ);
  if (bankFile) {
    while (bankFile.available()) {
      String line = bankFile.readStringUntil('\n');
      if (line.startsWith("CC")) {
        int ccButton = line.substring(3).toInt();
        if (button == ccButton - 1) {
          bankFile.close();
          return true;
        }
      } else if (line.startsWith("PC")) {
        int pcButton = line.substring(3).toInt();
        if (button == pcButton + 8) {
          bankFile.close();
          return true;
        }
      }
    }
    bankFile.close();
  }
  return false;
}

String getBankFilename(int bank) {
  return "bank" + String(bank + 1) + ".txt";
}

I'm using an Arduino Mega and the latest versions of the libraries. Until this point I've been using a Teensy for another controller and it works great. So I understand how to load the libraries. However, the Arduino IDE is new to me. But, I see the libraries, and it says they are installed.

Any help would be super appreciated!
 
You meed to instantiate MIDI. Add this line after the includes:
Code:
MIDI_CREATE_DEFAULT_INSTANCE();

Pete
 
Back
Top