Need some help with MIDI setup

Hi,

I’m extremely new to Arduino and programming in general. My partner got me a Korg littleBits for my birthday and I thought it would be fun to automate it via Ableton. I found a program that would allow me to do so; however, I’m unable to get it to compile as is.

Here’s the code:

#include <Adafruit_CircuitPlayground.h>

#define NOTE_PIN 5
#define NOTE_TRIGGER_PIN 4
#define CC_PIN 6
#define CLOCK_PIN 3
#define SPEEDFACTOR 64
#define MIN_NOTE 24
#define MAX_NOTE MIN_NOTE+61
#define NOTES_BUFFER 4
#define MIDIUSB
#define MIDIEvent

unsigned long count = 0;

const byte START = 250;
const byte STOP = 252;
const byte TICK = 248;
const byte CONTINUE = 251;

const byte NOTEON = 0x09;
const byte NOTEOFF = 0x08;
const byte CC = 0x0B;
const byte CLOCK = 0xF;

unsigned short notePointer = 0;
byte notes[NOTES_BUFFER];

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial || millis() > 3000) {
    ; // wait for serial port to connect, or 3 seconds pass
  }

  // put your setup code here, to run once:
  pinMode(NOTE_PIN, OUTPUT);
  digitalWrite(NOTE_PIN, LOW);
  pinMode(NOTE_TRIGGER_PIN, OUTPUT);
  digitalWrite(NOTE_TRIGGER_PIN, LOW);
  pinMode(CLOCK_PIN, OUTPUT);
  digitalWrite(CLOCK_PIN, HIGH);
  
  for(int i = 0; i > NOTES_BUFFER; i++)
    notes[i] = 0;
}

void loop() {
  
  while((MIDIUSB.available()) > 0){
     MIDIEvent e = MIDIUSB.read();

    if(e.type == NOTEON){
      //Note to analog value
      //min note = 24
      if(e.m2 >= MIN_NOTE && e.m2 <= MAX_NOTE){
        // remember which note we're playing
        addNote(e.m2);
        outputNote();
        //send a trigger pulse
        digitalWrite(NOTE_TRIGGER_PIN, HIGH);
      }
    }
    if(e.type == NOTEOFF){
      removeNote(e.m2);
      outputNote();
      digitalWrite(NOTE_TRIGGER_PIN, LOW);  
    }
    if(e.type == CC && e.m2 == 1){ 
      // cc 1
      analogWrite(CC_PIN, e.m3 * 2);
    }
    // Clock pulses
    if(e.type == CLOCK){
      switch(e.m1){
      case TICK:
        {
          if((count % 24) == 0){
            digitalWrite(CLOCK_PIN, LOW);
            delay(1);
            digitalWrite(CLOCK_PIN, HIGH);
          }
          count++;
          break;
        }
      case START:
        {
          count = 0;
        }
      case STOP:
        {
          //ssssh
          digitalWrite(CLOCK_PIN, LOW);
        }
      }
    }
  }//end while midi available
}//end loop

void outputNote(){
  //stretch the note number across the correct range
  byte note = (notePointer - 1) % NOTES_BUFFER;
  byte analogVal;
  if( note == 0 ){
    analogVal = 0;
  } 
  else {
    analogVal = map(notes[note], MIN_NOTE, MAX_NOTE, 0, 255);
  }
  Serial.print(note);
  Serial.print(" : ");
  Serial.println(analogVal);
  //output as a voltage
  analogWrite(NOTE_PIN, analogVal);
}

void addNote(byte note){
  // just in case
  removeNote(note);
  notes[notePointer] = note;
  notePointer = (notePointer + 1) % NOTES_BUFFER;
}

void removeNote(byte note){
  boolean found = false;
  for(int i = 0; i < NOTES_BUFFER; i++){
    int index = (i + notePointer) % NOTES_BUFFER;
    if(notes[index] == note){
      found = true;
    }
    if(found){
      notes[index] = notes[(index + 1) % NOTES_BUFFER];
    }
  }
  if(found){
    notePointer = (notePointer - 1) % NOTES_BUFFER;
    notes[notePointer] = 0;
  }
}


void printNotes(){
  Serial.print("Notes[\t");
  for(int i = 0; i < NOTES_BUFFER; i++){
    Serial.print(notes[i]);
    if(i == notePointer){
      Serial.print("*");
    }
    Serial.print("\t");
  }
  Serial.println("]");
  Serial.println(notePointer);
}

I keep getting errors at lines 50 and 51:

 while((MIDIUSB.available()) > 0){
     MIDIEvent e = MIDIUSB.read();

I’m too new to C to understand what questions I need to be asking or what keywords I need to be Googling. So this has been pretty frustrating. I ordered a book on getting started with Arduino sketches and I have one on C coming in the mail, but I’d really like to see if I could get this resolved now.

The errors I keep getting are as follows:

/Users/Owen/Documents/MIDIUSB/examples/MIDIUSB_read/MIDIUSB_read.ino: In function ‘void loop()’:
MIDIUSB_read:50: error: expected primary-expression before ‘.’ token
while((MIDIUSB.available()) > 0){
^
MIDIUSB_read:51: error: ‘e’ was not declared in this scope
MIDIEvent e = MIDIUSB.read();
^
MIDIUSB_read:51: error: expected primary-expression before ‘.’ token
MIDIEvent e = MIDIUSB.read();
^
Multiple libraries were found for “Adafruit_CircuitPlayground.h”
Used: /private/var/folders/sz/m3g18bn52kgg36125g3bvzxr0000gn/T/AppTranslocation/E7314249-C0E7-4B52-A4EF-55A0CC82B5D7/d/Arduino.app/Contents/Java/libraries/Adafruit_CircuitPlayground
Not used: /Users/Owen/Documents/Arduino/libraries/arduino_931892
Not used: /Users/Owen/Documents/Arduino/libraries/arduino_783062
Not used: /Users/Owen/Documents/Arduino/libraries/Adafruit_Circuit_Playground
Using library Adafruit_CircuitPlayground at version 1.6.4 in folder: /private/var/folders/sz/m3g18bn52kgg36125g3bvzxr0000gn/T/AppTranslocation/E7314249-C0E7-4B52-A4EF-55A0CC82B5D7/d/Arduino.app/Contents/Java/libraries/Adafruit_CircuitPlayground
Using library Wire at version 1.0 in folder: /private/var/folders/sz/m3g18bn52kgg36125g3bvzxr0000gn/T/AppTranslocation/E7314249-C0E7-4B52-A4EF-55A0CC82B5D7/d/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/Wire
Using library SPI at version 1.0 in folder: /private/var/folders/sz/m3g18bn52kgg36125g3bvzxr0000gn/T/AppTranslocation/E7314249-C0E7-4B52-A4EF-55A0CC82B5D7/d/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SPI
exit status 1
expected primary-expression before ‘.’ token

I’ve been running searches as if my life depends on it to figure out how to resolve the “expected primary expression…” and " ‘e’ not declared in this scope" errors to no avail.

Additional info:
The board currently selected is Leonardo, not the requisite Leonardo(arcore). I’m not sure if that would affect the program’s ability to compile, but it might be useful to be aware of that. I need to reinstall the arcore library, but I forget how to as it was some time ago and I’ve been too intimidated to retry.

Thank you so much in advance for your help in enabling my bleeps and bloops.

Please explain what you want the sketch to do.
How does MIDI relate to the Korg synthesizer? This "littleBits" kit doesn't seem to have MIDI in or out.
How do you want to communicate with Ableton? Should the Arduino send notes to Ableton? Should it send synthesizer parameters to Ableton synths/plugins? Should Ableton send MIDI notes to the Arduino to play?

The code you posted seems to be for the Adafruit Circuit Playground board. You never mentioned that board.

The error messages you are getting are for the MIDIUSB_read example. At least that's the file you have modified. Why don't you use the original MIDIUSB_read example to get started?

You don't need Arcore for MIDI over USB. Just use the MIDIUSB library together with the normal Arduino Leonardo core.
If you have installed third-party libraries and/or cores, it might be a good idea to just delete them, and start with a clean Arduino IDE installation. The only library you need is MIDIUSB.

Note that Arduino uses C++, not C. They are not the same language!

Pieter

  while (!Serial || millis() > 3000) {
    ; // wait for serial port to connect, or 3 seconds pass
  }

I think you want “&& millis() < 3000”. The way it is written, if the USB connection doesn’t become active within 3 seconds the loop will run for 47 days (until millis() rolls over to be < 3000).

PieterP:
Please explain what you want the sketch to do.
How does MIDI relate to the Korg synthesizer? This “littleBits” kit doesn’t seem to have MIDI in or out.
How do you want to communicate with Ableton? Should the Arduino send notes to Ableton? Should it send synthesizer parameters to Ableton synths/plugins? Should Ableton send MIDI notes to the Arduino to play?

Like I said, I’m super new to Arduino/programming at large, so this sketch was just pulled from a post on instructables (http://www.instructables.com/id/USB-MIDI-Littlebits-synth/). The littleBits doesn’t have MIDI In or Out, but apparently you can use a jumper cable on one of the magnetic connector clips to connect it to the Arduino. The linked post can explain it with more authority than I can.

PieterP:
The error messages you are getting are for the MIDIUSB_read example. At least that’s the file you have modified. Why don’t you use the original MIDIUSB_read example to get started?

Again, I don’t have enough knowledge of the program to even think to do something like that

PieterP:
You don’t need Arcore for MIDI over USB. Just use the MIDIUSB library together with the normal Arduino Leonardo core.
If you have installed third-party libraries and/or cores, it might be a good idea to just delete them, and start with a clean Arduino IDE installation. The only library you need is MIDIUSB.

Alright. I’m pretty sure they got deleted a while back, but I’ll double check.

PieterP:
Note that Arduino uses C++, not C. They are not the same language!

Oh weird, the book I ordered said it just used C. Maybe it just presupposed I wasn’t an absolute beginner, who knows.

Anyway, thank you for the thoughtful response!

johnwasser:

  while (!Serial || millis() > 3000) {

; // wait for serial port to connect, or 3 seconds pass
 }




I think you want "&& millis() < 3000". The way it is written, if the USB connection doesn't become active within 3 seconds the loop will run for 47 days (until millis() rolls over to be < 3000).

Alright, thanks!

PieterP:
Note that Arduino uses C++, not C. They are not the same language!

That statement is incomplete, or at best misleading. It's not like they are totally different languages. If you have a C++ environment then you can write all the C code you want and be just fine. C++ built onto C. You can write C code for your arduino all day long and it works just fine. Think of C as a subset of C++. But they're not completely different languages like say C++ and python.

Delta_G:
That statement is incomplete, or at best misleading. It's not like they are totally different languages. If you have a C++ environment then you can write all the C code you want and be just fine. C++ built onto C. You can write C code for your arduino all day long and it works just fine. Think of C as a subset of C++. But they're not completely different languages like say C++ and python.

The point I was trying to make, is that most Arduino sketches are written using syntax that is valid in C++, but not in C.
A book on C doesn't cover C++ specific things like object-oriented code, that is used all the time in Arduino programs. Of course, one could write a C program for Arduino, but most core libraries are written in C++, so you probably won't be able to use those.

Of course, knowing C can be very useful when using Arduino, since a lot of the syntax is indeed the same or very similar.

Pieter

I'm still pretty stuck on how to get this running if anyone could offer any suggestions.

LFOwen:
I'm still pretty stuck on how to get this running if anyone could offer any suggestions.

You haven't told us what you actually want the Arduino to do.
The Instructable you linked to seems to use the Arduino as some kind of crude, MIDI-controlled square wave generator / synth.

I'd recommend starting from scratch. Trying to understand that entire program is probably not going to end well. You'll learn much more if you build it yourself, piece by piece.

First try to get an audio/oscillator output working. Note that the I'ble uses pins 5 & 6. They operate at 490Hz. You might want to use pins 3 & 11, these run at 980Hz by default.
I can't really recommend a library, but don't use the "Adafruit_CircuitPlayground" library when there are more specific libraries available.

Then try reading MIDI events from Ableton and print them to the Serial monitor. The MIDIUSB works great. Work your way through the included examples first.

Finally, combine the two previous parts, and let certain MIDI events change the output, for example.

Pieter