Hello! Im a beginner and im having trouble with writing the first sketch for my instrument project.
Its a midi controller with 9 switches and a potentiometer.
I want pin 14 and 15 to be the note transpose function (+ / -) which will be read by the buttons later to modify pitch of the note that the button plays. Keyroot = pitch/note for the button and also the root for the other buttons (to make them play in a certain scale depending on how you transpose)
First I had trouble with the transpose function, but now I have seem to got the transpose function working except the code is having trouble reading different parts of itself:
From serial monitor:
" number of minus pushes: 13
current key is
91off
on
number of PLUS pushes: 14
current key is:
92off
on
number of minus pushes: 13
current key is:
91off "
Which is correct, but the midi note doesn't get transposed?
I tried changing the default key and it didn't work so there is some miscommunication I guess?
When I started on this code I made this "root key" pattern that was working before I started on transpose coding.
Any tips welcome to make this faster or anything else.
Thank you.
Code:
#include <MIDI.h>
#include <SoftwareSerial.h>
#include <MIDIUSB.h>
#include <PitchToNote.h>
#define NUM_BUTTONS 8
//Transpose Ints
// Constants
int transpose1Pin = 14; // the pin that the pushtranspose is attached to
int transpose2Pin = 15; // the pin that the pushtranspose is attached to
// Variables
int transposePushCounter = 0; // counter for the number of transpose presses
int transpose1State = 0; // current state of the transpose
int transpose2State = 0; // current state of the transpose
int lasttranspose1State = 0; // previous state of the transpose
int lasttranspose2State = 0; // previous state of the transpose
//int transpose = transposePushCounter;
uint8_t defaultkey = 48; //48
uint8_t currentkey = 0;
uint8_t keyroot1 = currentkey;
uint8_t keyroot2 = keyroot1 + 2;
uint8_t keyroot3 = keyroot2 + 2;
uint8_t keyroot4 = keyroot3 + 1;
uint8_t keyroot5 = keyroot4 + 2;
uint8_t keyroot6 = keyroot5 + 2;
uint8_t keyroot7 = keyroot6 + 1;
uint8_t keyroot8 = keyroot7 + 1;
const uint8_t button1 = 2;
const uint8_t button2 = 3;
const uint8_t button3 = 4;
const uint8_t button4 = 5;
const uint8_t button5 = 6;
const uint8_t button6 = 7;
const uint8_t button7 = 8;
const uint8_t intensityPot = 0; //A0 input
const uint8_t buttons[NUM_BUTTONS] = {button1, button2, button3, button4, button5, button6, button7};
const byte noteFunction[NUM_BUTTONS] = {keyroot1, keyroot2, keyroot3, keyroot4, keyroot5, keyroot6, keyroot7};
const int LEDpin = 13; // Indicator LED
//software serial
SoftwareSerial midiSerial(1, 0); // digital pins that we'll use for soft serial RX & TX
void setup() {
// set the states of the I/O pins:
pinMode(LEDpin, OUTPUT);
// initialize the transpose pin as a input:
pinMode(transpose1Pin, INPUT_PULLUP);
pinMode(transpose2Pin, INPUT_PULLUP);
// Set MIDI baud rate:
Serial.begin(9600);
midiSerial.begin(31250);
for (int i = 0; i < NUM_BUTTONS; i++)
pinMode(buttons[i], INPUT_PULLUP);
}
uint8_t notesTime[NUM_BUTTONS];
uint8_t pressedButtons = 0x00;
uint8_t previousButtons = 0x00;
uint8_t intensity;
// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
}
void loop() {
//TRANSPOSE 1
// read the pushtranspose input pin:
transpose1State = digitalRead(transpose1Pin);
// compare the transposeState to its previous state
if (transpose1State != lasttranspose1State) {
// if the state has changed, increment the counter
if (transpose1State == LOW) {
// if the current state is HIGH then the transpose went from off to on:
transposePushCounter--;
Serial.println("on");
Serial.print("number of minus pushes: ");
Serial.println(transposePushCounter);
int currentkey = defaultkey + transposePushCounter;
Serial.println("current key is:");
Serial.print(currentkey);
} else {
// if the current state is LOWa then the transpose went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
// delay(50);
}
// save the current state as the last state, for next time through the loop
lasttranspose1State = transpose1State;
//TRANSPOSE 2
transpose2State = digitalRead(transpose2Pin);
if (transpose2State != lasttranspose2State) {
if (transpose2State == LOW) {
transposePushCounter++;
Serial.println("on");
Serial.print("number of PLUS pushes: ");
Serial.println(transposePushCounter);
uint8_t currentkey = defaultkey + transposePushCounter;
Serial.println("current key is:");
Serial.print(currentkey);
} else {
Serial.println("off");
}
lasttranspose2State = transpose2State;
}
readButtons();
readIntensity();
playNotes();
}
//code for note buttons/potentiometer (Working)
void readButtons()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
if (digitalRead(buttons[i]) == LOW)
{
bitWrite(pressedButtons, i, 1);
delay(50);
}
else
bitWrite(pressedButtons, i, 0);
}
}
void readIntensity()
{
int val = analogRead(intensityPot);
intensity = (int) (map(val, 0, 1023, 0, 127));
}
void playNotes()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
if (bitRead(pressedButtons, i) != bitRead(previousButtons, i))
{
if (bitRead(pressedButtons, i))
{
bitWrite(previousButtons, i , 1);
noteOn(0, noteFunction[i], intensity);
MidiUSB.flush();
Serial.print ("Current key:");
Serial.println (currentkey);
} else
{
bitWrite(previousButtons, i , 0);
noteOff(0, noteFunction[i], 0);
MidiUSB.flush();
}
}
}
}
// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}