Floppy drive music with Arduino UNO

I'm Attila and i have a little problem with my school project, which is making floppy drives play music. I know that there is a lot of codes on the internet which works, but I wanted to create my own, I wanted to understand every part of the code. So I've made the sketch, it can play the super mario bros theme song, but the "change direction" part doesn't work well. If I start the progrem, the floppy's reader head starts moving, and it doesn't reach the 'dirCountMax' steps, and it changes direction for no reason. It causes the reader heads to knock into the case, and it's very annoying, plus in that position it can't get further, so it doesn't play any note, til it turns around. I've tried a lot of things, one thing worked, adding a 10ms delay in the loop function, but this isn't an option, because of the note timing, if I add 10ms delay, the actual notes aren't notes anymore, just noise.
This is my first arduino project, I don't know what could be the problem, so I'm here to ask you guys a little help, some tips or something, that can fix my program. Thanks to everyone who read my issue.
I'm a new menber on this forum, so I can't add the actual file, but here if the change direction part:

//change direction
  if(dirCountA == dirCountMax){
     dirPinA_state =!dirPinA_state;
     if(dirPinA_state == HIGH){
        PORTD &= ~_BV(PD3);
        PORTD |=_BV(PD3);
     dirCountA = 0;

What type of controller are you using for the floppy drive? What are you using for a floppy disk data separator? Post a schematic, not a frizzy thing. How much experience to you have in electronics? What file structure are you using on the floppy? What size is the floppy? How many active sides? You say it causes the reader heads to knock, that tells me your interface needs a lot of work.

I'm using an Arduino UNO. The floppy drives aren't used the regular way, just the stepper motors are used, which pushes or pulls the reader head, this is what actually makes the sound. If you steps the steppers with a frequency that is for example 440Hz for A3, it'll "play" the A3 note. When I got home, i'll try share a drive link or something like that, so you can check the whole code. Thx for the help!