Pages: [1]   Go Down
Author Topic: midi in debugging  (Read 622 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HEllo,
im creating a midi in circuit for my sequencer, the question ist
how can I check if my circuit is right and the code also.
I use this circuit adn the code below:
http://www.instructables.com/file/FUJ0J5BFPBKZZB1/

The arduino is a mega and the midi in ist connected to RX0.

Code:
byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;
//midi in end

void setup() {
  //  set the states of the I/O pins:
  for (int x = 0;x<16;x++) {
    pinMode(LEDpins[x], OUTPUT);
    pinMode(switchPins[x], INPUT);
  }
                   
  pinMode(timePin1, INPUT);
  pinMode(timePin2, INPUT);
  pinMode(playPin, INPUT);
   pinMode(holdPin, INPUT);
    pinMode(dualPin, INPUT);
    pinMode(midiin, INPUT);
  //  Set MIDI baud rate:
  Serial.begin(31250);
  Serial1.begin(31250);
  // turn all notes off
  noteOn(0xB00, 0x7B, 0x00);
  noteOn(0xB01, 0x7B, 0x00);
  noteOn(0xB02, 0x7B, 0x00);
  noteOn(0xB03, 0x7B, 0x00);
  noteOn(0xB04, 0x7B, 0x00);
  noteOn(0xB05, 0x7B, 0x00);
  noteOn(0xB06, 0x7B, 0x00);
  noteOn(0xB07, 0x7B, 0x00);
  blink(3);
  timer_start = millis();
  //------------------------------
  // Control Voltage Section start
  byte clr;
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  digitalWrite(SLAVESELECT,HIGH); //disable device
 
  SPCR = (1<<SPE)|(1<<MSTR);
  //SPCR = B01010000;
  clr=SPSR;
  clr=SPDR;
  delay(10);
  // Control Voltage Section end
  //------------------------------
  //---analog read timing
  sbi(ADCSRA,ADPS2) ;
  cbi(ADCSRA,ADPS1) ;
  sbi(ADCSRA,ADPS0) ;
  //-----------------------------
}

void loop() {

 
 
//Main sequence
 
//begin midi in here
if(Serial.available() > 0) {
  data = Serial.read();
  if(data == midi_start) {
    play_flag = 1;
  }
  else if(data == midi_continue) {
    play_flag = 1;
  }
  else if(data == midi_stop) {
    play_flag = 0;
  }
  else if((data == midi_clock) && (play_flag == 1)) {
  //
  currentSwitchState[step] = digitalRead(switchPins[step]);

     //mopdes are:
     // normal sequencer
     
     modeState = digitalRead(modePin);
     
     if (modeState == 0) {
   
       
        dualState = digitalRead(dualPin);
        if (dualState == 1) {
           currentSwitchState[step] = digitalRead(switchPins[step]);
           playstep1(step, 8, 0);
           if (step < 8) {
              step++;
           } else {
              step = 0;
           }
           currentSwitchState[step2] = digitalRead(switchPins[step2]);
           playstep2(step2, 15, 8);
            if (step2 < 15) {
              step2++;
           } else {
              step2 = 8;
           }
        } else {
           playstep(step);
           if (step < 15) {
              step++;
           } else {
              step = 0;
        }
        }
     } else {
       loopstep(step);
        if (step < 8) {
        step++;
      } else {
        step = 0;
      }
     }

    }
  //else { blink(1); }
   //
   
    }

   

}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, i got a signal now and the sequencer runs according to the sequencer, but its far to fast, how can and have I to devide the incoming signal?

Code:
if(Serial.available() > 0) {
  data = Serial.read();
  if(data == midi_start) {
    play_flag = 1;
  }
  else if(data == midi_continue) {
    play_flag = 1;
  }
  else if(data == midi_stop) {
    play_flag = 0;
  }
  else if((data == midi_clock) && (play_flag == 1)) {
  //
//here are my steps...

EDIT:

when I only do every 4 ticks something the timing seems right:

Code:
if(counter < 5) {
  counter++;
  } else {
  counter = 0; 
  currentSwitchState[step] = digitalRead(switchPins[step]);
« Last Edit: January 09, 2012, 07:36:41 am by Rupertt » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Rupert

I recently got into this Arduino thang and have developed a MIDI processor using a Mega1280.

I had read most of the MIDI topics thoroughly, and settled on the Mega because

1] I need the program space to store the databases's to control my particular synth and
2] the Mega not only has a plethora of pins, but up to 4 UARTs.

From what I can read the virtual serial ones produce snags if you dont physically disconnect the midi from the RX/TX since these are shared for the USB flashing too.

Questions if I may.

Why do you use Serial as MIDI instead of Serial1 (pins 18/19) and Serial2?

These can prevent the nause of either interfering with the program flashing and/or your MIDI stream getting early corruption.

Also, I found Serial1.available() was necessary but insufficient to prevent a different pathology ... one you will want to avoid if you are getting it.

If you read, and nothing is available, read() returns -1.
If you are assigning the value to a byte, this will get interpreted as 255 or 0xff -- bad news this is also MIDI for RESET.
If however you read an int, and ignore -1, then convert valid values back to an byte I've found this reduces spurious data.

Apologies if you are not hitting this issue. I know I was but once I ignored the -1's my midi streams settled down _almost_ nicely.

Now to look up buffering on UARTs to see how to handle large Sysex transfers without dropping bytes.

Regards

Steve H
Logged

Pages: [1]   Go Up
Jump to: