Go Down

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

UKHeliBob

Post the code with more than one pot and the errors that you got.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

debbio

#31
Apr 25, 2015, 06:23 pm Last Edit: Apr 25, 2015, 06:25 pm by debbio
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));
  }
 

UKHeliBob

Code: [Select]
const int POT_THRESHOLD2 = 3// in +What must variable declarations always have at the end ?
Answer - a semi-colon.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

debbio

Yeah but it doesn't still work.. have you tried it? it change the value of both the pots

UKHeliBob

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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

debbio

#35
Apr 25, 2015, 09:29 pm Last Edit: Apr 26, 2015, 11:37 am by debbio
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?

debbio

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);
}

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

debbio

The second possibility is what I think the best One.. Can you help me? I don't understand the logic of for loop

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

debbio

Thanks!! I will try in the next days! Thank you so much!!

debbio

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

Go Up