Go Down

Topic: MIDI mapping Slider/Knob Values (Read 591 times) previous topic - next topic

Grumpy_Mike

What brain libiary are you using. I have had a look and found two on the first three returns from google.

I am concerned about the line:-
Code: [Select]
Brain brain(Serial)
It implies that your brain sensor is communicating with the Arduino through the serial port. How is your sensor connected? If it is to pins 0 & 1 then you can not send MIDI data over the USB and send it to hairless as well as talking to the brain sensor. It is also likely that the brain software is changing the hardware baud rate.

PieterP

What brain libiary are you using. I have had a look and found two on the first three returns from google.
Maybe this is helpful? https://github.com/kitschpatrol/Brain
The library does indeed use the Serial port. That's why I recommended using SoftwareSerial.
You can't use the hardware UART for the brain sensor and for MIDI at the same time.
Either use SoftwareSerial for the brain, or get an Arduino that has more than one hardware UART, or one hardware UART and USB.
If you get a board like the Leonardo, you can use USB MIDI (much easier than Hairless), and the hardware UART can be used by the brain sensor.
Please explain what it is you are actually trying to do. You talk about controlling sliders and knobs, then you say you want to turn on / off a slider, and now you're posting code for changing the pitch of a note. It doesn't make any sense to me.

Pieter

blabberbytes

Quote
With an UNO, you can NOT send MIDI and other data to the computer at the same time, especially if you have something connected to pins 0 and 1.
I've taken your advice and ordered an Arduino Leonardo

Quote
Please explain what it is you are actually trying to do. You talk about controlling sliders and knobs, then you say you want to turn on / off a slider, and now you're posting code for changing the pitch of a note. It doesn't make any sense to me.

Pieter
Sorry for all the confusion. What I am trying to accomplish is taking the data from brain.readMeditation(); and midi map it to a slider.

PieterP

Code: (untested ! ) [Select]
// Code for Arduino Leonardo

#include "MIDIUSB.h"
#include <Brain.h>

HardwareSerial& BrainSerial = Serial1;

Brain brain(BrainSerial); // Brain "sensor" is connected to hardware UART

const uint8_t CC = 0xB0; // Control Change header
const uint8_t channel = 1; // MIDI channel 1
const uint8_t controller = 1; // CC controller number 1

void setup() {
  BrainSerial.begin(9600); // Start serial brain connection
  Serial.begin(115200); // Start "Serial" communication with the computer
}

void loop() {
  static uint8_t previousMidiValue = 0;
  if (brain.update()) { // if there's a new "brain" value
    Serial.println(brain.readCSV()); // print brain data to Serial Monitor ?
    uint8_t meditationValue = brain.readMeditation(); // get the meditation value [0, 100]
    uint8_t midiValue = meditationValue * 127 / 100; // map to 7-bit MIDI value [0, 127]
    if (midiValue != previousMidiValue) { // if it's a different value than last time
      sendMIDI(CC, channel, controller, midiValue); // send it as a Control Change value over MIDI
      previousMidiValue = midiValue;
    }
  }
}

void sendMIDI(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2) {
  c--; // Channels are zero-based
  m &= 0xF0;       // bitmask high nibble
  m |= 0b10000000; // set msb
  c &= 0xF;        // bitmask low nibble
  d1 &= 0x7F;      // clear msb
  d2 &= 0x7F;      // clear msb
  midiEventPacket_t msg = {m >> 4, m | c, d1, d2}; // Create 4-byte MIDI USB packet
  MidiUSB.sendMIDI(msg); // Send it
  MidiUSB.flush(); // Wait untill it's sent
}

Go Up