converting standalone midi sequencer to midi in

Hello,

i have a midi sequencer which used an “internal clock”, now i want to sync this to my other midi sequencer.
I followd this guide little-scale: How to Deal with MIDI Clock Signals in Arduino.
And I used the attached curcuit to create the midi in port.

Right now the sequencer does not start, im not even shure if I can just exchange my “timing IF” with the
code from the example. Please have a look and maybe you have an idea

int switchPins[] = {38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53};
int LEDpins[] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37};
int currentSwitchState[] = {0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0};
int currentSwitchState2[] = {0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0};
byte channel[] = {0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7};
byte channel2[] = {0x90, 0x91, 0xB92, 0x93, 0x94, 0x95, 0x96, 0x97};
byte note[] = {0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A};
int volume[] = {0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0};
int loopnr = 0;
int AnalogValue[] = {0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0};
int lastNotePlayed[] = {0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0};     
int lastSwitchState[] ={0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0};


const int timePin1 = 9;  
const int timePin2 = 10;
const int playPin = 8;
const int modePin = 11;
const int dualPin = 13;
const int holdPin = 12;

const int middleC = 60;    // Middle C (MIDI note value 60) is the lowest note we'll play

byte kanal = 0xB1;

int currentTimeState = 0;
int currentTimeStateDown = 0;


int playState = 0;
int modeState = 0;
int holdState = 0;
int dualState = 0;
int dualnr = 0;
int lastplayState = 0;
int playing = 0;

int aktiv = 0;
int buttonCounter = 0;
int delaypause = 1000;
unsigned long timercount = 0;
unsigned long timer_start = 0;
unsigned long wartezeit = 5000; //1000ms = 1 sekunde
unsigned long currentTime = 250;
unsigned long Timer = 0;
int step = 0;
int step2 = 8;
int startdual = 0;
boolean button_up = true;
boolean button_down = true;

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


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);
  //  Set MIDI baud rate:
  Serial.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();
}

void loop() {

//timing settings  begin
  if (digitalRead(timePin1)== HIGH) {
    if (button_up == true) {
      currentTimeState = digitalRead(timePin1);
      if (currentTimeState == 1) {
        currentTime = currentTime + 5;
      }
    }
    button_up = false;
} else {
button_up = true; // wenn digitalRead nicht LOW war
}

  if (digitalRead(timePin2)== HIGH) {
    if (button_down == true) {
      currentTimeStateDown = digitalRead(timePin2);
      if (currentTimeStateDown == 1) {       
        if (currentTime <= 10) {
          currentTime = 10;
        } else {     
          currentTime = currentTime - 5;
        }
      }
   }
   button_down = false;
} else {
button_down = true; // wenn digitalRead nicht LOW war
}
//timing settings end

      
//play button action
  playState = digitalRead(playPin);
  
  if (playState != lastplayState) {
    
    // change the state of the led when someone pressed the button
    if (playState == 1) { 
      if(playing==1) {
        playing=0;
        noteOn(0xB0, 0x78, 0x00);
      } else { 
        playing=1;         
      }
    }
    // remember the current state of the button
    lastplayState = playState;
  }

 //midi in stuff

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)) {
  //  Sync();
//  }
  
//} 
//midi in end

 
//Main sequence. old standalone sequencer
//  if (playing == 1) {
//begin midi in here
//    if ((millis() - Timer) > currentTime) {

      Timer = millis();
      currentSwitchState[step] = digitalRead(switchPins[step]);

     //mopdes are:
     // normal sequencer
     // loopmode, 8 droneloops, upper row is pitch, lower row volume 
     modeState = digitalRead(modePin);
     
     if (modeState == 0) {
    
        
        dualState = digitalRead(dualPin);
        if (dualState == 1) {
           currentSwitchState[step] = digitalRead(switchPins[step]);
           playstep2(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;
      }
     }

//    }
//}   
//midi in if
}
  
} 
//midi in end    

}

//****************************************************************
//****************************************************************
// Functions
//****************************************************************
//****************************************************************


void noteOn(byte cmd, byte data1, byte  data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}

// Blinks an LED 3 times
void blink(int howManyTimes){
  int i;
  for (i=0; i< howManyTimes; i++) {
    digitalWrite(LEDpins[0], HIGH);
    digitalWrite(LEDpins[8], HIGH);
    delay(100);
    digitalWrite(LEDpins[0], LOW);
    digitalWrite(LEDpins[8], LOW);
    delay(100);
  }
  for (int i=0; i<16; i++) {
      digitalWrite(LEDpins[i], HIGH);
    }
    delay(1500);
      for (int i=0; i<16; i++) {
      digitalWrite(LEDpins[i], LOW);
    }
}


void playstep(int stepnr) {
  
  if (currentSwitchState[stepnr] == 1) {
    
    AnalogValue[stepnr] = analogRead(stepnr);
    note[stepnr] = AnalogValue[stepnr]/8 ;
     holdState = digitalRead(holdPin);
    if (holdState == 1) {
         noteOn(0xB0, 0x7B, 0x00);  // all notes off     
    }  
    noteOn(0x90, note[stepnr], 0x7F); // play note

    for (int i=0; i<=15; i++) {
      digitalWrite(LEDpins[i], i==stepnr);
    }
  }
}

void loopstep(int stepnr) {
 
//unfinished
}

void playstep2(int stepnr, int dualnr, int startdual) {
  

  if (currentSwitchState[stepnr] == 1) {
    
    AnalogValue[stepnr] = analogRead(stepnr);
    note[stepnr] = AnalogValue[stepnr]/8 ;
     holdState = digitalRead(holdPin);
    if (holdState == 1) {
         noteOn(0xB0, 0x7B, 0x00);  // all notes off     
    }  
    noteOn(0x90, note[stepnr], 0x7F); // play note

    for (int i=startdual; i<=dualnr; i++) {
      digitalWrite(LEDpins[i], i==stepnr);
    }
  }
}

when I connect the midi In i cant upload any more sketches, does that mean my curciut is wrong?

thnx

Hi,

for you Midi In prob, it depend how you circuit is wired but normally when doing midi stuff with a Arduino the midi in and out is using the Arduino serial input/ouput which is the same used for programming the arduino. you should disconnect your Midi-in "circuit before uploading your sketch. If you use for ex. the sparkfun midi shield there is a little switch for programming or running the program.

I hope it helps

Ah ic, i thought something like that, I have to add a switch to my curccuit as well. I also forgot the picture, i did talk to someone here about that drawing, so I gues my code is somewhere wrong

Do I have to declare the Serial In port somewhere? I only have the Serial.beginn().

As I know the Arduino got only on "official" serial port .. you can use other port to create another serial port .. but it is another story. So you don't need to define the serial port. serial.begin should be enouth to open the serial port .. did you try to connect your midi out of the arduino to your computer midi In and use a midi monitor to check what the midi command sent by the arduino ..