I was using this code for Sending control change using an ultrasonic sensor and sending noteOn/noteOff message to the MIDI software which was working perfectly. Recently I have added two push switch to octave up and octave down using the switches() function. But now my MIDI monitor is not showing any output.
#include <MPR121.h>
#include <Control_Surface.h>
#include <MPR121_Datastream.h>
#include <Wire.h>
#include <MIDIUSB.h>
USBMIDI_Interface midi;
//NEW CODE
int x=0, last_d, last_x;
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
long duration;
uint8_t data;
int distance, last;
MIDIAddress myAddress =
{
MIDI_CC::General_Purpose_Controller_1, CHANNEL_1
};
//X-Axis of my Joystick
PBPotentiometer potentiometer = {
A5, // Analog pin connected to potentiometer
CHANNEL_1, // MIDI Channel 1
};
// Y-Axis of my Joystick
CCPotentiometer potentiometer2 = {
A1, // Analog pin connected to potentiometer
{MIDI_CC::Channel_Volume, CHANNEL_1}, // Channel volume of channel 1
};
// touch constants
const uint32_t BAUD_RATE = 115200;
const uint8_t MPR121_ADDR = 0x5A; // 0x5A is the MPR121 I2C address
const uint8_t MPR121_INT = 4; // pin 4 is the MPR121 interrupt
// MPR121 datastream behaviour constants
const bool MPR121_DATASTREAM_ENABLE = false;
// MIDI behaviour constants
const bool SWITCH_OFF = false; // if set to "true", touching an electrode once turns the note on, touching it again turns it off
// if set to "false", the note is only on while the electrode is touched
const uint8_t NOTES[] = {59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48}; // piano notes from C3 to B3 in semitones
bool note_status[12] = {false, false, false, false, false, false, false, false, false, false, false, false};
const uint8_t CHANNEL = 0; // default channel is 0
void setup() {
Control_Surface.begin(); // Initialize Control Surface
Serial.begin(BAUD_RATE);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(LED_BUILTIN, OUTPUT);
pinMode(2, INPUT);
pinMode(6, INPUT);
MPR_Begin();
}
void loop() {
dist_ance();
switches();
Serial.println(distance);
if (distance != last_d || x != last_x)
{
convert(distance);
data = data + x;
if (data > 127) data = 127;
Control_Surface.MIDI().sendCC(myAddress, data);
}
last = distance;
last_x = x;
Control_Surface.loop();
sendnotemessage();
}
void dist_ance()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
if (distance > 50)
distance = 50;
}
void convert(int dis)
{
int y = 50 - dis;
data = map(y, 0, 50, 0, 127);
}
void switches ()
{
if(digitalRead(2)==HIGH)
{
while(digitalRead(2)==HIGH){
// statement
}
x = x+12;
}
if(digitalRead(6)==HIGH)
{
while(digitalRead(6)==HIGH){
// statement
}
x = x-12;
}
}
void sendnotemessage()
{
MPR121.updateAll();
for (int i = 0; i < 12; i++)
{
if (MPR121.isNewTouch(i))
{
if (!SWITCH_OFF)
{
noteOn(CHANNEL, NOTES[i], 64); // maximum velocity
MidiUSB.flush();//To make data transfer immediately
}
else
{
if (note_status[i])
{
noteOff(CHANNEL, NOTES[i], 64); // maximum velocity
MidiUSB.flush();//To make data transfer immediately
}
else
{
noteOn(CHANNEL, NOTES[i], 64); // maximum velocity
MidiUSB.flush();//To make data transfer immediately
}
note_status[i] = !note_status[i]; // toggle note status
}
}
else if (MPR121.isNewRelease(i))
{
// if we have a new release, turn off the onboard LED and
// send a "note off" message (unless we're in toggle mode)
if (!SWITCH_OFF)
{
noteOff(CHANNEL, NOTES[i], 64); // maximum velocity
MidiUSB.flush();
}
}
if (MPR121_DATASTREAM_ENABLE)
{
MPR121_Datastream.update();
}
}
}
void noteOn(byte channel, byte pitch, byte velocity)
{
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
MidiUSB.flush();
}
void noteOff(byte channel, byte pitch, byte velocity)
{
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
MidiUSB.flush();
}
void controlChange(byte channel, byte control, byte value)
{
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
MidiUSB.flush();
}
void MPR_Begin()
{
if (!MPR121.begin(MPR121_ADDR)) {
Serial.println("error setting up MPR121");
switch (MPR121.getError()) {
case NO_ERROR:
Serial.println("no error");
break;
case ADDRESS_UNKNOWN:
Serial.println("incorrect address");
break;
case READBACK_FAIL:
Serial.println("readback failure");
break;
case OVERCURRENT_FLAG:
Serial.println("overcurrent on REXT pin");
break;
case OUT_OF_RANGE:
Serial.println("electrode out of range");
break;
case NOT_INITED:
Serial.println("not initialised");
break;
default:
Serial.println("unknown error");
break;
}
while (1);
}
MPR121.setInterruptPin(MPR121_INT);
if (MPR121_DATASTREAM_ENABLE)
{
MPR121.restoreSavedThresholds();
MPR121_Datastream.begin(&Serial);
}
else
{
MPR121.setTouchThreshold(40); // this is the touch threshold - setting it low makes it more like a proximity trigger, default value is 40 for touch
MPR121.setReleaseThreshold(20); // this is the release threshold - must ALWAYS be smaller than the touch threshold, default value is 20 for touch
}
MPR121.setFFI(FFI_10);
MPR121.setSFI(SFI_10);
MPR121.setGlobalCDT(CDT_4US); // reasonable for larger capacitances
digitalWrite(LED_BUILTIN, HIGH); // switch on user LED while auto calibrating electrodes
delay(1000);
MPR121.autoSetElectrodes(); // autoset all electrode settings
digitalWrite(LED_BUILTIN, LOW);
}