I am building a simple arduino based midi controller to send cc midi messages. However I am able to send messages only through A0 (analog input).. Any help on this will be appreciated,..
#include <MIDI.h>
int pot[] = {A0,A1,A2};
int AnaPinsNum = 3;
int potIn[] = {0,0,0};
int analogValue = 0;
int lastAnalogValue[] = {0,0,0};
void setup()
{
MIDI.begin(4);
// 115200 hairless MIDI
Serial.begin(115200);
int i;
for (i = 0; i < 3; i++);
}
void loop() {
int i;
for (i = 0; i < AnaPinsNum; i++)
potIn[i] = analogRead(pot[i])/8;
// potentiometer could be too sensitive and
// give different (+-1) values.
// send CC only when the difference between last value
// is more than 1
if ((potIn[i]-lastAnalogValue[i]) > 1 || (potIn[i]-lastAnalogValue[i]) < -1) {
// value changed?
if (potIn[i] != lastAnalogValue[i]) {
// send serial value (ControlNumber 1, ControlValue = analogValue, Channel 1)
// more info: http://arduinomidilib.sourceforge.net/a00001.html
MIDI.sendControlChange(1, potIn[i], 1);
lastAnalogValue[i] = potIn[i];
}
}
}
1 Answer 1
Your logic is flawed here:
int i;
for (i = 0; i < AnaPinsNum; i++)
potIn[i] = analogRead(pot[i]) / 8;
// potentiometer could be too sensitive and
// give different (+-1) values.
// send CC only when the difference between last value
// is more than 1
if ((potIn[i] - lastAnalogValue[i]) > 1 || (potIn[i] - lastAnalogValue[i]) < -1) {
// value changed?
if (potIn[i] != lastAnalogValue[i]) {
// send serial value (ControlNumber 1, ControlValue = analogValue, Channel 1)
// more info: http://arduinomidilib.sourceforge.net/a00001.html
MIDI.sendControlChange(1, potIn[i], 1);
lastAnalogValue[i] = potIn[i];
}
}
The first three lines read all three analog pins:
int i;
for (i = 0; i < AnaPinsNum; i++)
potIn[i] = analogRead(pot[i]) / 8;
However then, outside the loop, you are using the value of i
which will now be 4, and outside the array bounds. You need to bracket the whole lot, eg.
int i;
for (i = 0; i < AnaPinsNum; i++)
{ // <------------------------------- add this
potIn[i] = analogRead(pot[i]) / 8;
// potentiometer could be too sensitive and
// give different (+-1) values.
// send CC only when the difference between last value
// is more than 1
if ((potIn[i] - lastAnalogValue[i]) > 1 || (potIn[i] - lastAnalogValue[i]) < -1)
{
// value changed?
if (potIn[i] != lastAnalogValue[i])
{
// send serial value (ControlNumber 1, ControlValue = analogValue, Channel 1)
// more info: http://arduinomidilib.sourceforge.net/a00001.html
MIDI.sendControlChange(1, potIn[i], 1);
lastAnalogValue[i] = potIn[i];
} // end of not same as before
} // end of value changed
} // <----------------- add this
If pot one is sending out Controller number 1 and channel number 1, 2nd pot should send Controller number 2 and channel no 2.
If I understand your question correctly:
Change:
MIDI.sendControlChange(1, potIn[i], 1);
to:
MIDI.sendControlChange(i, potIn[i], i);
-
Thanks Nick Gammon for the help..I am able to solve the input issue but I am still unable to send different MIDI messages through different input.. For ex if 1st pot is sending value 49 as expression value the next pot should send 50...like different expression value/channel message etc...in the increment of 1Ashish– Ashish2015年08月27日 05:47:25 +00:00Commented Aug 27, 2015 at 5:47
-
if 1st pot is sending value 49 as expression value
- I'm sorry, I don't understand that.2015年08月27日 05:51:47 +00:00Commented Aug 27, 2015 at 5:51 -
I want to send values on different channels, or different controllers, instead of all on the same channel/controller.. Right now all the pots are sending out same Messages. I want different pots different messages..Ashish– Ashish2015年08月27日 06:15:50 +00:00Commented Aug 27, 2015 at 6:15
-
If pot one is sending out Controller number 1 and channel number 1, 2nd pot should send Controller number 2 and channel no 2..Ashish– Ashish2015年08月27日 06:20:39 +00:00Commented Aug 27, 2015 at 6:20
-
See amended answer.2015年08月27日 09:00:42 +00:00Commented Aug 27, 2015 at 9:00
for (i = 0; i < 3; i++);
- what is that line intended to do?