Go Down

Topic: Pots to midi with hairless (Read 2898 times) previous topic - next topic

debbio

#15
Apr 22, 2015, 01:18 pm Last Edit: Apr 22, 2015, 01:20 pm by debbio
It's a potentiometer 10kohm.. The arduino Due is connected through Programming port to pc



PS: it was written in the first post

UKHeliBob

A potentiometer is not a MIDI device, whatever its value.

If I understand correctly you want to read the value of the potentiometer then, depending on the value, send different MIDI commands to the PC serial interface to be read an interpreted by the Hairless MIDI to serial bridge ?  Is that right ?

What speed is the Hairless interface running at and what serial port is it using ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

debbio

Yes! Values are 6400 both serial and hairless

UKHeliBob

Yes! Values are 6400 both serial and hairless
I am not sure what you mean by this.  Can you please explain ?

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

debbio

Sorry.. The serial monitor is set to a speed rate of 9600 and so hairless on my PC.. The port used by hairless is /dev/ttyACM0

UKHeliBob

I know little or nothing about MIDI so please bear with me.

How does the Arduino know what port and speed to use when communication with Hairless ?
Doesn't MIDI use some odd baud rate ?
Do you need to use the MIDI library on the Arduino if Hairless is just expecting a formatted message over serial ?

This page looks like it might help you, at least with sending MIDI messages to Hairless http://www.instructables.com/id/Send-and-Receive-MIDI-with-Arduino/?ALLSTEPS
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

debbio

This is why you didn't understand my first questions..
So.. Hairless-midiserial let you communicate Arduino with PC through the messages it sends to serial monitor.. When you open hairless you have to choose the right source for the Arduino messages... You have to syncronize the reading by putting the same rate speed for serial and hairless... But you have to send to serial a precise code which is a MIDI code.. Now.. My question is: which is this right MIDI code to control knobs and other software elements? How can I send this message as MIDI message to serial monitor? What is the right code?

UKHeliBob

Quote
My question is: which is this right MIDI code to control knobs and other software elements?
Have you done a Google search to find the format and content of MIDI messages ?  There are thousands of sites with with the details.  Searching for midi message format example returns 192,000 matches for example


Quote
How can I send this message as MIDI message to serial monitor?
Did you look at the link I posted in my previous reply ?  The page has examples of how to do it.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

debbio

This is the new code... Can someone verify it, please?

Code: [Select]
#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();
// Variables:
int cc = 0;
int AnalogValue = 0; // define variables for the controller data
int lastAnalogValue = 0; // define the "lastValue" variables

void setup() {
 
   
  //  launch MIDI
  MIDI.begin(1);
   Serial.begin(9600);
}

void loop() {
 
AnalogValue = analogRead(0);
  //  convert to a range from 0 to 127:
  cc = AnalogValue/8;
  // check if analog input has changed
  if (lastAnalogValue != cc) {
   
    // update lastAnalogValue variable
    lastAnalogValue = cc;
  }
  }   //  endif
 void MIDImessage(int command, int MIDInote, int MIDIvelocity){
 Serial.write(0xB0);
 Serial.write(50);
 Serial.write(2);}

UKHeliBob

I still don't understand  why you need to use the MIDI library to send a MIDI message over the serial link.  Of course, it would probably help if the program actually called the MIDImessage() function.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

debbio

I found this:
http://www.codeproject.com/Articles/38203/Arduino-Based-MIDI-Expression-Pedal

It's exactly what I want but the code is old and have to be modernized: expression like BYTE are no more available... Can someone help me, please?

Code: [Select]
//#define DEBUG                 1

// Constants
const int LED_PIN = 13;             // LED connected to digital pin 13
const int POT_PIN = 0;              // Pot connected to analog pin 0
const int POT_THRESHOLD = 7;        // Threshold amount to guard against false values
const int MIDI_CHANNEL = 0;         // MIDI Channel 1

#ifdef DEBUG
const int DEBUG_RATE = 9600;        // Serial debugging communicates at 9600 baud
const int SERIAL_PORT_RATE = DEBUG_RATE;
#else
const int MIDI_BAUD_RATE = 31250;   // MIDI communicates at 31250 baud
const int SERIAL_PORT_RATE = MIDI_BAUD_RATE;
#endif


void setup()
{
    pinMode(LED_PIN, OUTPUT);          // Sets the digital pin as output
    digitalWrite(LED_PIN, HIGH);       // Turn the LED on
    Serial.begin(SERIAL_PORT_RATE);     // Starts communication with the serial port
}

void loop()
{
    static int s_nLastPotValue = 0;
    static int s_nLastMappedValue = 0;

    int nCurrentPotValue = analogRead(POT_PIN);
    if(abs(nCurrentPotValue - s_nLastPotValue) < POT_THRESHOLD)
        return;
    s_nLastPotValue = nCurrentPotValue;

    int nMappedValue = map(nCurrentPotValue, 0, 1023, 0, 127); // Map the value to 0-127
    if(nMappedValue == s_nLastMappedValue)
        return;
    s_nLastMappedValue = nMappedValue;

    MidiVolume(MIDI_CHANNEL, nMappedValue);
}

void MidiVolume(byte channel, byte volume)
{
#ifdef DEBUG
    Serial.println(volume, DEC);
#else
    Serial.print(0xB0 | (channel & 0x0F), BYTE);    // Control change command
    Serial.print(0x07, BYTE);                       // Volume command
    Serial.print(volume & 0x7F, BYTE);              // Volume 0-127
#endif
}

UKHeliBob

Replace all of the
Code: [Select]
Serial.print(something, BYTE);
commands with
Code: [Select]
   Serial.write(something);

As I suggested, the program does not use the MIDI library but does use the odd MIDI baud rate.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

debbio

It works but I cannot change the value on the program... it seems that the midi command doesn't send a change value of the data but a channel's change value... Mmm

debbio

After a lot of days... This is the best I've done!! There is still some problem of rate.. but.. it works!!
Has someone any idea how to improve the code?


Code: [Select]
/*
MIDI On/Off Messages
By Amanda Ghassaei
July 2012
http://www.instructables.com/id/Send-and-Receive-MIDI-with-Arduino/

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.

*/
int noteOFF = 128;//128
 const int analogInPin = A0;
 const int POT_THRESHOLD = 3;// in +
 const int HISTORY_BUFFER_LENGTH = 6;// in +
 int velocity = 0;//velocity of MIDI notes, must be between 0 and 127
 //higher velocity usually makes MIDI instruments louder
 int sensorValue = 0;
 int ControlChange = 176;//176 = in binary, controlchange command
 int NoteOn =145;
 //int noteOFF = 128;//128 = 10000000 in binary, note off command
static int s_history[HISTORY_BUFFER_LENGTH];// in +

//BUTTONS

int buttonState1;
int val1;
int buttonState2;
int val2;
int buttonState3;
int val3;
int led = 13;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;//led




void setup() {
  //  Set MIDI baud rate:
  Serial.begin(57600);
   // Initialize he buffer
    for(int i=0; i<HISTORY_BUFFER_LENGTH; i++)// in +
    {// in +
        s_history[i] = -1;// in +
    }// in +
   
   
   //BUTTON
   pinMode(2, INPUT_PULLUP);
   pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(led, OUTPUT);//led
   buttonState1 = digitalRead(2);//HIGH
   buttonState2 = digitalRead(3); //HIGH
buttonState3 = digitalRead(4); //HIGH
     
   
}

void loop() {
 
    val1 =digitalRead(2);
    if(val1 != buttonState1){
    if (val1 ==LOW) {
    //Send an ASCII 'A',
    MIDImessage(145, 60 ,100);
   
 
  }}
else{MIDImessage(145, 60 ,0);}

 
 
   val2 = digitalRead(3);
  if (val2 != buttonState2){
    if (val2 ==LOW){
      MIDImessage(146, 61,100);
 
}
else{MIDImessage(146, 61,0);}
  }
 
  val3 = digitalRead(4);
  if (val3 != buttonState3){
    if (val3 == LOW){
       MIDImessage(147, 62,100);
    }
    else{MIDImessage(147, 62,0);}
  }
  delay(50);
  buttonState1 = val1;
buttonState2 = val2;
buttonState3 = val3;
 
 
 
   
   
  static int s_nLastPotValue = 0; //in +
  static int s_nLastMappedValue = 0;//in +
   int nCurrentPotValue = analogRead(A0);//in +
    if(abs(nCurrentPotValue - s_nLastPotValue) < POT_THRESHOLD)//in +
        return;//in +
    s_nLastPotValue = nCurrentPotValue;//in +

    int nMappedValue = map(nCurrentPotValue, 0, 1023, 0, 255); //in + // Map the value to 0-255
    if(nMappedValue == s_nLastMappedValue)//in +
        return;//in +
   
    for(int i=0; i<HISTORY_BUFFER_LENGTH; i++)//in +
    {//in +
        if(s_history[i] == nMappedValue)//in +
            return;//in +
    }//in +

    memcpy(&s_history[0], &s_history[1], sizeof(int) * (HISTORY_BUFFER_LENGTH - 1));//in +
    s_history[HISTORY_BUFFER_LENGTH - 1] = nMappedValue;//in +
    s_nLastMappedValue = nMappedValue;//in +
   
   
   // sensorValue = analogRead (analogInPin);
   
    velocity = map (nCurrentPotValue, 0, 1023, 0, 127);
    MIDImessage(176, 50, velocity);//turn note on
    delay(20);
    //MIDImessage(noteOFF, 50, velocity);//turn note off
    //wait 200ms until triggering next n
     
  }
 //send MIDI message
void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
  Serial.write(command);//send note on or note off command
  Serial.write(MIDInote);//send pitch data
  Serial.write(MIDIvelocity);//send velocity data
}

debbio

Now I'm wondering how to add another potentiometer..the last code give me different errors.. It change the value of speed of the two pots at the same time.. Any idea?

Go Up