Hi Guys, ive got this code which takes inputs from 3 piezos and 2 potentimeters accross pins A0-A4 on a teensy 2.0 arduino, the problem is that when i tap a piezo its triggering control change information as well as midi note on /off does anyone know why this might be? cheers
//PIEZOS
const byte PiezoPins[] = {A2, A3, A4, A5,};
//same
const byte note[sizeof(PiezoPins)] = {50, 51, 52, 53,};
//POTENTIOMETERS
const byte potPins [] = {A0, A1};
const byte notetwo[sizeof(potPins) ] = {48, 49,};
void setup() {
// MIDI rate
Serial.begin(31250);
pinMode(11,INPUT_PULLUP);
}
void loop() {
//PIEZOS
//name change, threshold of what?
const int sensorThreshold = 500; //anything over fifty means we've hit the piezo
for(byte i = 0; i < sizeof(PiezoPins); i++){
int sensorReading = analogRead(PiezoPins[i]);
if(sensorReading >= sensorThreshold){
usbMIDI.sendNoteOn(note[i], map(sensorReading, 0, 1023, 0, 127), 1);
delay(10);
usbMIDI.sendNoteOff(note[i], 0, 1);
delay(5);
}
}
const int potThreshold = 8;
for(byte i = 0; i < sizeof(potPins); i++){
int sensorReading = analogRead(potPins[i]);
if(sensorReading >= potThreshold){
usbMIDI.sendControlChange(notetwo[i], map(sensorReading, 0, 1023, 0, 127), 2);
delay(10);
}
}
}
if(sensorReading >= potThreshold){
Every time through loop() it is going to send control change data for any pot that reads >= 8. I suspect that what you want to do is send data only for pots that have CHANGED by more than 8. You will have to store the previous values for comparison. If you map the data first (0-127) you can store the previous value in a byte. Since that effectively divides the value by 8 you can use a pot threshold of 1. That means ANY change is sent so you don't need a 'threshold' value at all.
//PIEZOS
const byte PiezoPins[] = {A2, A3, A4, A5,};
//same
const byte note[sizeof(PiezoPins)] = {50, 51, 52, 53,};
//POTENTIOMETERS
const byte potPins [] = {A0, A1};
const byte notetwo[sizeof(potPins) ] = {48, 49,};
byte prevPotValue[sizeof potPins / sizeof potPins[0]];
void setup() {
// MIDI rate
Serial.begin(31250);
pinMode(11, INPUT_PULLUP);
}
void loop() {
//PIEZOS
//name change, threshold of what?
const int sensorThreshold = 500; //anything over fifty means we've hit the piezo
for (byte i = 0; i < sizeof(PiezoPins); i++) {
int sensorReading = analogRead(PiezoPins[i]);
if (sensorReading >= sensorThreshold) {
usbMIDI.sendNoteOn(note[i], map(sensorReading, 0, 1023, 0, 127), 1);
delay(10);
usbMIDI.sendNoteOff(note[i], 0, 1);
delay(5);
}
}
//POTENTIOMETERS
for (byte i = 0; i < sizeof(potPins); i++) {
byte sensorReading = map(analogRead(potPins[i]), 0, 1023, 0, 127);
if (sensorReading != prevPotValue[i]) {
usbMIDI.sendControlChange(notetwo[i], sensorReading, 2);
prevPotValue[i] = sensorReading;
delay(10);
}
}
}