HDD motor control sketch fails

Heya all! I'm trying to control the motor of an old Samsung hard-disk. It's got 3 coils and I was trying to follow this guy's tutorial. He also provided some code here. After building the circuit I tried countless times fine-tuning the "gear" delays, but it seemed that the motor just stopped spinning after the third gear( I could also hear from the coils that the pulses increased fast in frequency at that point). I scrapped most of the code and tried to increase manually the speed by using a potentiometer.

This is the code:

int phase1 = 2;
int phase2 = 3;
int phase3 = 4;
int led = 13;

unsigned long stepLength = 0;
int readVal = 0;

void setup(){
   pinMode(led, OUTPUT);  
  pinMode(phase1, OUTPUT);
  pinMode(phase2, OUTPUT);
  pinMode(phase3, OUTPUT);
  digitalWrite(led, LOW); 
  pinMode(A0, INPUT);
  Serial.begin(4800);
}

void loop(){
  switchStep(1);
  switchStep(2);
  switchStep(3);
  
  readVal = analogRead(A0);
  stepLength = map(readVal, 0, 1023, 1000, 40000);
  Serial.print(stepLength);
  Serial.print("\n");
  
}

void switchStep(int stage)
{
  switch(stage)
  {
  case 1:
    digitalWrite(phase1, HIGH);
    digitalWrite(phase2, LOW);
    digitalWrite(phase3, LOW);
    myDelay(stepLength);
    break;
    
  case 2:
    digitalWrite(phase1, LOW);
    digitalWrite(phase2, HIGH);
    digitalWrite(phase3, LOW);
    myDelay(stepLength);
    break;
    
  default:
    digitalWrite(phase1, LOW);
    digitalWrite(phase2, LOW);
    digitalWrite(phase3, HIGH);
    myDelay(stepLength);
    break;
  }  


}

void myDelay(unsigned long p) {
if (p > 16380) {
 delay (p/1000);
 } else {
  delayMicroseconds(p);
  }
}

the switchStep and myDelay functions are written by that guy. I don't quite understand why he has the p>16380 condition, what's the logic behind that?

Still no good results. I've put a serial.print on the mapped value. There's something about the value 3000 -> that's where the motor suddenly is desynchronized and stops spinning. What's happening here?

If the motor moves at all you must have some stuff right.

Try running with a fixed value for stepLength and alter it by editing the code until you get a feel for what range of values works.

Don't use the myDelay() function. Just use delayMicroseconds(stepLength)

If a value doesn't work double it (or halve it) for the next try until you get some sign of action. Then try halfway between the values you recently tried. Try pulses at 8 millisec intervals - 8000 microsecs.

...R

Hi, you say that you can get the motor to spin, if you stop turning the pot, does the motor continue to increase in speed or does it keep constant speed?

Tom....... :)

It doesn't accelerate when I stop turning the pot. It just, after some point, looses the ability to keep up with those impulses, I don't know why. Is there any thorough guide I could read on the subject? I'm using TIP122 transistors to drive the coils and 1N4148 diodes for protecting against inductive spikes. Also, 300ohm resistors on the base of the transistors.

Hi,

http://www.instructables.com/id/BLDC-Motor-Control-with-Arduino-salvaged-HD-motor/?ALLSTEPS

http://theamateurprogrammer.blogspot.com.au/2014/02/revitalizing-old-hard-drive-motors.html

I found these projects with google, might be worth a read.

Tom.... :)