Skip to content

Instantly share code, notes, and snippets.

@gregyjames
Forked from arion/midi.ino
Last active June 22, 2018 08:19
Show Gist options
  • Save gregyjames/88b135723767d6f8a98137f7d096d20b to your computer and use it in GitHub Desktop.
Save gregyjames/88b135723767d6f8a98137f7d096d20b to your computer and use it in GitHub Desktop.
Arduino Launchpad
#include <MIDI.h>
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
#include <math.h>
SoftwareSerial mySerial(11, 10); // RX, TX
MIDI_CREATE_DEFAULT_INSTANCE();
bool isMp3Mode = false;
const byte ROWS = 4; // - Number of rows of the matrix keyboard
const byte COLS = 4; // - Number of columns of the matrix keyboard
byte rowPins[ROWS] = {2, 3, 4, 5}; // - Row Pins
byte colPins[COLS] = {6, 7, 8, 9}; //--Column Pins
byte buttonState[COLS][ROWS] = { // - Array of button states
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}
};
byte kpdNote[COLS][ROWS] = { //Array of Notes
{51, 47, 43, 39},
{50, 46, 42, 38},
{49, 45, 41, 37},
{48, 44, 40, 36}
};
byte dval = 0;
int pval = 0;
int potPrVal[4] = {0, 0, 0, 0}; // - Array of potentiometers
byte potPins[4] = {1, 2, 3, 4}; //--Pins of potentiometers
void setup() {
MIDI.begin(); //Initializing the MIDI interface
for(byte i = 0; i < COLS; i++){ //--Configure the lines of the metric columns as outputs--
pinMode(colPins[i], OUTPUT); //--and submit to them a log. 1-----------------------
digitalWrite(colPins[i], HIGH); // ----------------------------------------------
}
for(byte i = 0; i < ROWS; i++) { //--Configure the columns of the metric columns as inputs---------
pinMode(rowPins[i], INPUT); //--and include built-in μ pull-up resistors--
digitalWrite(rowPins[i], HIGH); //------------------------------------------------------
}
pinMode(mp3Pin, INPUT);
digitalWrite(mp3Pin, HIGH);
Serial.begin(115200);
mySerial.begin(9600);
mp3_set_serial(mySerial); //--set softwareSerial for DFPlayer-mini mp3 module
delay(1); //--delay 1ms to set volume
mp3_set_volume(20); //--value 0~30
}
void loop() {
for(byte chn = 0; chn < 4; chn++) //-Read cycle of potentiometer values-----------
{
pval = analogRead(potPins[chn]);
if (abs(pval-potPrVal[chn]) > 10) { //--If the current value is ex. from the past
MIDI.sendControlChange(1, chn, pval);
potPrVal[chn] = pval;
if (chn == 3) {
}
}
}
//-------------------------------------
for(byte col = 0; col < COLS; col++) //-Matrix keyboard read cycle-----
{
digitalWrite(colPins[col], LOW); //--On the read column we set 0---
for(byte row = 0; row < ROWS; row++) //--Read each column line by row--
{ //--And at the pressed button we transfer the note--
dval = digitalRead(rowPins[row]);
if ( dval == LOW && buttonState[col][row] == HIGH ) {
MIDI.sendNoteOn(kpdNote[col][row], 127, 1);
}
if ( dval == HIGH && buttonState[col][row] == LOW ) {
MIDI.sendNoteOff(kpdNote[col][row], 0, 1);
}
buttonState[col][row] = dval;
}
digitalWrite(colPins[col], HIGH);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment