Arduino Forum

Products => Arduino Due => Topic started by: debbio on Apr 19, 2015, 11:28 am

Title: Pots to midi with hairless
Post by: debbio on Apr 19, 2015, 11:28 am
Hi all! I'm new in the forum and I'm a noob in Arduino but I'm here to learn the possible! ;))
I'm trying to create a MIDI simple controller with only two pots that have to control a program on my computer.. I want to launch hairless in background so that I can use the serial mode on Arduino.. But here are the problems: how can I make the pots communicate with serial in a MIDI code? I can only show one pot at time and I see the values on serial.. But how can traslate them in a MIDI language so that the program can read it?
I've an Arduino Due and I use archlinux ;)

Thank you so much

Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 19, 2015, 11:32 am
Let's have some examples of values read from the pots and the MIDI messages to be output.

The values input from the pots will vary between 0 and 1023 so you could use different ranges to generate different messages if that is what you have in mind.
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 19, 2015, 11:47 am
Yes it's clear but I cannot understand how the grammar of these messages has to be... What kind of MIDI message has to be sent for a specific pot's value?
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 19, 2015, 11:58 am
Quote
but I cannot understand how the grammar of these messages has to be
That is a different question.  A Google search for "midi message format" returns over 67,000 results of which is the first MIDI messages (http://www.midi.org/techspecs/midimessages.php)

Which ones do you want to send ?
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 19, 2015, 12:20 pm
Yes I'm using google too ;)
But I can't found out how to set up the code in Arduino IDE..
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 19, 2015, 01:26 pm
I wish you would make your mind up what your problem is.

Quote
how can I make the pots communicate with serial in a MIDI code?
Quote
I cannot understand how the grammar of these messages has to be..
Quote
I can't found out how to set up the code in Arduino IDE..
Before you can write the Arduino program you must know what MIDI messages you want to send.  Actually sending the messages should be easy.  Did you come across http://www.arduino.cc/en/Tutorial/Midi (http://www.arduino.cc/en/Tutorial/Midi) in your search ?
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 19, 2015, 04:03 pm
I want to send some data from the pots to my serial port and in the sketch create something to let this value to be seen as midi values (or something that the program can see). I can't figure out what kind of code I've to create..
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 19, 2015, 04:38 pm
Quote
I want to send some data from the pots to my serial port and in the sketch create something to let this value to be seen as midi values (or something that the program can see). I can't figure out what kind of code I've to create
I have underlined the things you don't seem to know.  As you can see it is difficult to provide help when there are so many unknowns.

What is it exactly you want to do ?  Suppose the user changes the position of one of the pots attached to the Arduino, what should happen ?  Should the volume or pitch of the note change, for instance ?  If not, what should happen ?
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 19, 2015, 04:44 pm
You're right.. I don't know exactly what kind of message I've to send to be seen from the program on PC.. I think it's not important which one is.. maybe volume change? By reading here http://little-scale.blogspot.it/2011/08/simple-examples-of-sending-midi-data.html I've seen I've to send a CONTROL CHANGE (defined by B in a midi message), but the data can be volume or somithing different... Am I wrong?
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 19, 2015, 04:51 pm
This probably should work.. In the next days I'will try

Code: [Select]
byte val = 0;

void setup() {
Serial.begin(57600); // open serial port
}

void loop() {
for(int i = 0; i < 3; i ++) {
val = analogRead(i) / 8; // read value of a potentiometer
// (which is analog input 0 - 1)
// depending on which for() loop we are at

// let's send a control change message

Serial.print(0xB0 + i, BYTE); // MIDI control change; channel number
// the channel number depends on which for() loop we ar at
Serial.print(1, BYTE); // MIDI controller #1
Serial.print(val, BYTE); // MIDI controller value from the potentiometer
delay(1); // delay for a brief moment
}
}
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 19, 2015, 04:57 pm
The page you linked to is very old and the Arduino code is out of data because
Code: [Select]
Serial.print(something, BYTE);
has been replaced by
Code: [Select]
Serial.write(something);
since version 1.0.0 of the IDE but that is easy to change in the code.

If you want to control a MIDI device you absolutely must know what you want the MIDI device to do otherwise the exercise is pointless.
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 22, 2015, 11:56 am
Improving my code.. but on SerialMonitor I can't see nothing...

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(4);
   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) {
    MIDI.sendControlChange(0xB0,cc,1);
    // update lastAnalogValue variable
    lastAnalogValue = cc;
  }   //  endif
 
}
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 22, 2015, 12:28 pm
Quote
but on SerialMonitor I can't see nothing...
I am not surprised as you are not printing anything to Serial.  What were you expecting to see ?
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 22, 2015, 12:56 pm
Improving... but no results...

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(4);
   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) {
    MIDI.sendControlChange(0xB0,cc,1);
    // update lastAnalogValue variable
    lastAnalogValue = cc;
  }
 Serial.print("0xB0,");

 Serial.print(cc);

 Serial.print(",1,");
 
delay(20);
}   //  endif
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 22, 2015, 01:05 pm
How is the MIDI device connected to the Arduino ?
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 22, 2015, 01:18 pm
It's a potentiometer 10kohm.. The arduino Due is connected through Programming port to pc



PS: it was written in the first post
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 22, 2015, 03:07 pm
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 ?
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 22, 2015, 04:51 pm
Yes! Values are 6400 both serial and hairless
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 22, 2015, 05:01 pm
Yes! Values are 6400 both serial and hairless
I am not sure what you mean by this.  Can you please explain ?

Title: Re: Pots to midi with hairless
Post by: debbio on Apr 22, 2015, 05:21 pm
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
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 22, 2015, 05:53 pm
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 (http://www.instructables.com/id/Send-and-Receive-MIDI-with-Arduino/?ALLSTEPS)
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 22, 2015, 07:15 pm
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?
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 22, 2015, 07:28 pm
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.
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 22, 2015, 10:46 pm
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);}
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 22, 2015, 11:07 pm
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.
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 23, 2015, 10:02 am
I found this:
http://www.codeproject.com/Articles/38203/Arduino-Based-MIDI-Expression-Pedal (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
}
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 23, 2015, 11:20 am
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.
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 23, 2015, 09:38 pm
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
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 25, 2015, 01:03 am
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
}
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 25, 2015, 04:25 pm
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?
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 25, 2015, 05:42 pm
Post the code with more than one pot and the errors that you got.
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 25, 2015, 06:23 pm
When I try to upload this is the error:
Code: [Select]


sketch_apr25b.ino:18:2: error: expected ',' or ';' before 'const'
sketch_apr25b.ino:27:22: error: 'HISTORY_BUFFER_LENGTH' was not declared in this scope
sketch_apr25b.ino: In function 'void setup()':
sketch_apr25b.ino:52:20: error: 'HISTORY_BUFFER_LENGTH' was not declared in this scope
sketch_apr25b.ino:54:9: error: 's_history' was not declared in this scope
sketch_apr25b.ino: In function 'void loop()':
sketch_apr25b.ino:127:20: error: 'HISTORY_BUFFER_LENGTH' was not declared in this scope
sketch_apr25b.ino:129:12: error: 's_history' was not declared in this scope
sketch_apr25b.ino:133:13: error: 's_history' was not declared in this scope
sketch_apr25b.ino:133:57: error: 'HISTORY_BUFFER_LENGTH' was not declared in this scope
Errore durante la compilazione



The code is:


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 note off on midi command
 const int analogInPin = A0;
 const int analogInPin2 = A1;//pin del potenziometro 1
 const int POT_THRESHOLD = 3;
 const int POT_THRESHOLD2 = 3// in +
 const int HISTORY_BUFFER_LENGTH = 6;// in +
 int velocity = 0;
 int velocity2 = 0;//velocity of MIDI notes, must be between 0 and 127
 //higher velocity usually makes MIDI instruments louder
 int sensorValue = 0;
 int ControlChange = 176;
 int ControlChange2 =180; //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

//TIMER
long previousMillis = 0;
long interval = 50;




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(129,60,0);}}

  
  
   val2 = digitalRead(3);
  if (val2 != buttonState2){
    if (val2 ==LOW){
      MIDImessage(146, 61,100);
  
}
else{MIDImessage(130, 61,0);}
  }
  
  val3 = digitalRead(4);
  if (val3 != buttonState3){
    if (val3 == LOW){
       MIDImessage(147, 62,100);
    }
    else{MIDImessage(131, 62,0);}
  }
 
  unsigned long currentMillis = millis();//delay(50);
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;  
 
  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);
  
  
  
   //POT2
    static int s_nLastPotValue2 = 0; //in +
  static int s_nLastMappedValue2 = 0;//in +
   int nCurrentPotValue2 = analogRead(A1);//in +
    if(abs(nCurrentPotValue2 - s_nLastPotValue2) < POT_THRESHOLD2)//in +
        return;//in +
    s_nLastPotValue2 = nCurrentPotValue2;//in +

    int nMappedValue2 = map(nCurrentPotValue, 0, 1023, 0, 255); //in + // Map the value to 0-255
    if(nMappedValue2 == s_nLastMappedValue2)//in +
        return;//in +
    
  
    s_nLastMappedValue2 = nMappedValue2;//in +
  
  
   // sensorValue = analogRead (analogInPin);
  
    velocity2 = map (nCurrentPotValue2, 0, 1023, 0, 127);
    MIDImessage(180, 51, velocity2);//turn note on
    delay(20);
  
  
  
  
  
    blinkLed();
    
  }
 //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
}


void blinkLed(){ //led function

  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 170) {
    fadeAmount = -fadeAmount ;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(10);
}





I think I've to create something like this... but for only 2 pots and not 6:
Code: [Select]
//#include <MIDI.h>
#include <usbDevices.h>

#define numPots 6  // numer of pots
#define b 1        // this is the delta needed in

unsigned int currentPot[6] = {0,0,0,0,0,0};   
unsigned int pot[6] = {0,0,0,0,0,0};
byte controlChange = 0xBC; //0xB0 or 176

void setup() {
usb.init(midi);
// MIDI.begin();
//Serial.begin(31250);
}                         
void loop() {
  for(unsigned char i=0; i<numPots; i++) {
    currentPot[i] = analogRead(i) / 8;   
    if(abs(currentPot[i]-pot[i]) > b) {
      sendMidi(controlChange, i+1, currentPot[i]);
      pot[i] = currentPot[i];
    }
  }
}

void sendMidi(byte controlChange, unsigned int controlNum, int val) {
//MIDI.sendControlChange(controlNum, val);

Serial.write(controlChange);
Serial.write(controlNum);
Serial.write(val);
  // Serial.write(unsigned char(controlChange));
  //   Serial.write(unsigned char(controlNum));
  //     Serial.write(unsigned char(val));
  }
 
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 25, 2015, 06:33 pm
Code: [Select]
const int POT_THRESHOLD2 = 3// in +What must variable declarations always have at the end ?
Answer - a semi-colon.
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 25, 2015, 06:44 pm
Yeah but it doesn't still work.. have you tried it? it change the value of both the pots
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 25, 2015, 06:57 pm
Code: [Select]
 int nMappedValue2 = map(nCurrentPotValue, 0, 1023, 0, 255); //in + // Map the value to 0-255
This is in your POT2 section of the code so shouldn't nCurrentPotValue be nCurrentPotValue2 in the map() function ?
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 25, 2015, 09:29 pm
It doesn't change nothing... The code should be modified to accept at least 2 analog in ...
But I don't understand how to do it


PS:
Code: [Select]
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 +


Someone can explain me this part of code? if I want to create a second history for analog 2.. how should I do it?
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 26, 2015, 12:32 pm
VICTORYYYYY!!!!!  :smiley-lol:  :smiley-lol:  :smiley-lol:  :smiley-lol:  :smiley-lol:
The problem was with return function... I changed all return with if-else.. and it works!



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 note off on midi command
 const int analogInPin = A0;
 const int analogInPin2 = A1;//pin del potenziometro 1
 const int POT_THRESHOLD = 3;
 const int POT_THRESHOLD2 = 3;// in +
 const int HISTORY_BUFFER_LENGTH = 6;// in +
 int velocity = 0;
 int velocity2 = 0;//velocity of MIDI notes, must be between 0 and 127
 //higher velocity usually makes MIDI instruments louder
 int sensorValue = 0;
 int ControlChange = 176;
 int ControlChange2 =180; //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];
static int s_history2[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

//TIMER
long previousMillis = 0;
long interval = 50;




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 +
     for(int o=0; o<HISTORY_BUFFER_LENGTH; o++)// in +
    {// in +
        s_history2[o] = -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(129,60,0);}}

 
 
   val2 = digitalRead(3);
  if (val2 != buttonState2){
    if (val2 ==LOW){
      MIDImessage(146, 61,100);
 
}
else{MIDImessage(130, 61,0);}
  }
 
  val3 = digitalRead(4);
  if (val3 != buttonState3){
    if (val3 == LOW){
       MIDImessage(147, 62,100);
    }
    else{MIDImessage(131, 62,0);}
  }
 
  unsigned long currentMillis = millis();//delay(50);
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis; 
 
  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 +
        else{//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 +
        else{//in +
   
    for(int i=0; i<HISTORY_BUFFER_LENGTH; i++)//in +
    {//in +
        if(s_history[i] == nMappedValue){}//in +
            else{
    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 +
 
 
    velocity = map (nCurrentPotValue, 0, 1023, 0, 127);
    MIDImessage(176, 50, velocity);//turn note on//in +
    }}}//in +

    delay(10);
 
 
  //POT2
 
 
  static int s_nLastPotValue2 = 0; //in +
  static int s_nLastMappedValue2 = 0;//in +
   int nCurrentPotValue2 = analogRead(A1);//in +
    if(abs(nCurrentPotValue2 - s_nLastPotValue2) < POT_THRESHOLD2){}//in +
        else{//in +
    s_nLastPotValue2 = nCurrentPotValue2;}//in +

    int nMappedValue2 = map(nCurrentPotValue2, 0, 1023, 0, 255); //in + // Map the value to 0-255
    if(nMappedValue2 == s_nLastMappedValue2){}//in +
        else{//in +
   
    for(int o=0; o<HISTORY_BUFFER_LENGTH; o++)//in +
    {//in +
        if(s_history2[o] == nMappedValue2){}//in +
           else{
             memcpy(&s_history[0], &s_history[1], sizeof(int) * (HISTORY_BUFFER_LENGTH - 1));//in +
    s_history2[HISTORY_BUFFER_LENGTH - 1] = nMappedValue2;//in +
    s_nLastMappedValue2 = nMappedValue2;//in +
 
 
 
    velocity2 = map (nCurrentPotValue2, 0, 1023, 0, 127);
    MIDImessage(180, 51, velocity2);//turn note on//in +
    }}}//in +

   
    delay(10);
 
 
 
 
 
    blinkLed();
   
  }
 //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
}


void blinkLed(){ //led function

  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 170) {
    fadeAmount = -fadeAmount ;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(10);
}
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 26, 2015, 01:48 pm
Now that it works improve it by making the pot reading and data transmission into a function that you can call with parameters, thus avoiding repetition of what is basically the same code.

Or

Use arrays to hold the data and iterate through it using a for loop.  This would make it easier to expand the number of post if required.
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 26, 2015, 02:51 pm
The second possibility is what I think the best One.. Can you help me? I don't understand the logic of for loop
Title: Re: Pots to midi with hairless
Post by: UKHeliBob on Apr 26, 2015, 03:24 pm
I have not studied your code in detail but this is the sort of thing I had in mind using arrays and a for loop.
Code: [Select]

for (int pot = 0; pot < 2; pot++)
{
  s_nLastPotValue[pot] = 0; //declared as a global array
  s_nLastMappedValue[pot] = 0; //as above
  int nCurrentPotValue[pot] = analogRead(potPins[pot]);  //potPins held in a global array
  if (abs(nCurrentPotValue[pot] - s_nLastPotValue[pot]) < POT_THRESHOLD) {} //in +
  else { //in +
    s_nLastPotValue[pot] = nCurrentPotValue[pot];
  }//in +

  int nMappedValue[pot] = map(nCurrentPotValue[pot], 0, 1023, 0, 255); //in + // Map the value to 0-255
  if (nMappedValue[pot] == s_nLastMappedValue)[pot] {} //in +
    else { //in +

      for (int i = 0; i < HISTORY_BUFFER_LENGTH; i++) //in +
      { //in +
        if (s_history[i] == nMappedValue[pot]) {} //in +
        else {
          memcpy(&s_history[0], &s_history[1], sizeof(int) * (HISTORY_BUFFER_LENGTH - 1));//in +
          s_history[HISTORY_BUFFER_LENGTH - 1] = nMappedValue[pot];//in +
          s_nLastMappedValue[pot] = nMappedValue[pot];//in +


          velocity = map (nCurrentPotValue, 0, 1023, 0, 127);
          MIDImessage(176, 50, velocity);//turn note on//in +
        }
      }
    }//in +
  delay(10);
}

This is your code (good or bad !) modified to use arrays of values.  The same code is executed twice (or more if you change the for loop and array sizes) using the value of pot from the for loop to access the relevant values held in arrays.  I cannot guarantee that it will work but see what you make of it.
Title: Re: Pots to midi with hairless
Post by: debbio on Apr 26, 2015, 09:55 pm
Thanks!! I will try in the next days! Thank you so much!!
Title: Re: Pots to midi with hairless
Post by: debbio on May 09, 2015, 11:31 pm
Ok this is my conclusion:
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.

*/

byte control_now[2];
byte control_past[2];
byte n;
const int POT_THRESHOLD = 1;// in +
byte velocity[2];
int control_past0 = 0;
int control_past1 = 0;
 
 //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 note off on midi command

//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

//TIMER
long previousMillis = 0;
long interval = 50;




void setup() {
  //  Set MIDI baud rate:
  Serial.begin(9600);
   
   
   
   //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(129,60,0);}}

 
 
   val2 = digitalRead(3);
  if (val2 != buttonState2){
    if (val2 ==LOW){
      MIDImessage(145, 61,100);
 
}
else{MIDImessage(129, 61,0);}
  }
 
  val3 = digitalRead(4);
  if (val3 != buttonState3){
    if (val3 == LOW){
       MIDImessage(145, 62,100);
    }
    else{MIDImessage(129, 62,0);}
  }
 
  unsigned long currentMillis = millis();//delay(50);
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   
 
  buttonState1 = val1;
buttonState2 = val2;
buttonState3 = val3;
  }
 
 
  for (n =0; n<2; n++){ 
   
int control_past0 = 0;
int control_past1 = 0;
   control_now[n]   =  map( analogRead(n), 0, 1023, 0, 127);//in +
    if(abs(control_now[n] - control_past[n]) < POT_THRESHOLD)//in +
       {}
        else{//in +
  control_past[n] = control_now[n];//in +

   
   velocity[2] = control_now[n];
 
    MIDImessage(176, 50+n, velocity[2]);//turn note on
    delay(5);
   
   
   
  }}
  blinkLed();}
 //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
}


void blinkLed(){ //led function

  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 170) {
    fadeAmount = -fadeAmount ;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(5);
}


The problem is that if I change the Threshold to 3 it cannot reach the value of 127 and I don't understand why