Go Down

Topic: Saving Midi Channel into Arduino using a button and an external Midi device (Read 712 times) previous topic - next topic

techsezprods

Sep 09, 2019, 03:34 pm Last Edit: Sep 09, 2019, 04:12 pm by techsezprods Reason: mistake
Hi everybody

This is my first message here, so I would like to apologize for my poor english and if I break some rules of forum posting.

I'm trying to code a MIDI interface to put MIDI into old synthesizers.
I wrote some sketches that work pretty well on monophonic synths (DAC for CV/gate) and polyphonics synths (using shift registers for multiple gate).
I used to set up the incoming midi channel with a BCD coding wheel so the arduino knows how to deal with the incoming channels of the midi devices connected to his input MIDI port.

Now, I would like to go further and no longer use the coding wheel by replacing if with a kind of learn button.
When you press the "Learn" button, the arduino goes in programming mode, then you press a key on the external MIDI keyboard so a midi message containing MIDI Channel is sent to the arduino. Then the program stores the Channel data into the Eeprom that can be used as default MIDI channel at the next power on.
I must specify that I have already used this method with continuous controller messages but without storing data into the arduino Eeprom and without sorting the MIDI channel.

there must be something I did wrong in the sub program called "pmode" because the arduino does not react when I send a midi note and the led stays on.
I get to the end of my skills and if someone can give me ideas, his help will be really appreciated.

Here is my code :

Code: [Select]
/*  Interface MIDI pour Yamaha SY-2
   Gate OUT Arduino pin 12
   Arduino Aftertouch Pin 11 vers "vactrol"
   DAC OUT vers relais sur clavier
*/

#include <Adafruit_MCP4725.h>
#include <MIDI.h>
#include <EEPROM.h>

// Entrées Sorties

#define Learn 7 // Entrée Switch Programmation
#define After 11 //Sortie PWM vers Vactrol
#define Led 13 // Sortie Led Aide Débogage
#define Gate 12 // Sortie Gate
Adafruit_MCP4725 dac;
// Pin SDA (Serial Data) DAC connectée sur A4
// Pin SCL (Serial Clock) DAC connectée sur A5

// Variables

float outVoltmV;
int learnv; // Variable mode Programmation
int learnstate; // variable état Switch Learn
uint16_t dacValue;
int noteHZVshift = 0;
byte lastNote;
int Aftout;
int addr = 0; //adresse 0 EEprom
byte ReadChannel; // Valeur Canal Midi lue en mode Prog
byte WriteChannel; // Valeur Canal Midi stockée en EEprom
MIDI_CREATE_DEFAULT_INSTANCE();

void setup()
{

  // Déclaration Directions I/O
  pinMode(Gate, OUTPUT);
  pinMode(After, OUTPUT);
  pinMode(Learn, INPUT_PULLUP);
  digitalWrite(Gate, LOW);
 

  // Déclaration des Callbacks MIDI

  MIDI.setHandleNoteOn(handleNoteOn);
  MIDI.setHandleNoteOff(HandleNoteOff);
  MIDI.setHandleControlChange(handleControlChange);
  MIDI.setHandleAfterTouchChannel(Aft);
  MIDI.begin(CanalMidi());

  // Déclaration Adresse DAC

  dac.begin(0x60);
}



void handleNoteOn(byte channel, byte note, byte velocity)
{
  if (velocity == 0)
  {
    HandleNoteOff(channel, note, velocity);
    return;
  }
  lastNote = note;
  outVoltmV = 125.0 * exp(0.0578 * (note + noteHZVshift));
  dacValue = constrain(map(outVoltmV, 0, 5000, 0, 4095), 0, 4095);
  dac.setVoltage(dacValue, false);
  digitalWrite(Gate, HIGH);
}

void HandleNoteOff(byte channel, byte note, byte velocity) {
  if (note == lastNote) {
    digitalWrite(Gate, LOW);
  }
}

void handleControlChange(byte channel, byte number, byte value) {

  
}
void Aft(byte channel, byte pressure) {
  Aftout = map(pressure, 0, 127, 0, 255);
  analogWrite(After, Aftout);
}

void loop() {
  MIDI.read();
  learnstate = digitalRead(Learn);
  if (learnstate == 0) {
    learnv = learnv + 1; //incrémente valeur bouton
    delay(500); //pause anti rebond du bouton
  }
  if (learnv != 0) {
    digitalWrite (Led, HIGH);
    pmode();
  }
}
void pmode () {
  MIDI.begin(MIDI_CHANNEL_OMNI);
  int type, channel, number;
  if (MIDI.read()) {
    byte type = MIDI.getType();
  switch (type)
  {
    case midi::NoteOn:
    ReadChannel = MIDI.getChannel();
    EEPROM.write(addr, ReadChannel);
    learnv = 0;
    digitalWrite (Led, LOW);
      
      break;
  }
  return;
  }
}
byte CanalMidi(){
  WriteChannel = EEPROM.read(addr);
  return (WriteChannel);
}


Thanks by advance

Go Up