// Program to control 3-phase BLDC Motor const int phase1pin = 1; const int phase2pin = 2; const int phase3pin = 3; const int potPin = A2; int rpm = 5000; long int delayTime=(20000000)/rpm; float duty=0.25; void setup(){ //Serial.begin(9600); pinMode(phase1pin, OUTPUT); pinMode(phase2pin, OUTPUT); pinMode(phase3pin, OUTPUT); } void loop(){ rpm=600+4*analogRead(potPin); delayTime=(20000000)/rpm; //Serial.println(rpm); switchStep(1); switchStep(2); switchStep(3); } void switchStep(int stage){ switch(stage){ case 1: digitalWrite(phase1pin, HIGH); digitalWrite(phase2pin, LOW); digitalWrite(phase3pin, LOW); delayMicroseconds(delayTime*duty); digitalWrite(phase1pin, LOW); delayMicroseconds(delayTime*(1-duty)); break; case 2: digitalWrite(phase1pin, LOW); digitalWrite(phase2pin, HIGH); digitalWrite(phase3pin, LOW); delayMicroseconds(delayTime*duty); digitalWrite(phase2pin, LOW); delayMicroseconds(delayTime*(1-duty)); break; case 3: digitalWrite(phase1pin, LOW); digitalWrite(phase2pin, LOW); digitalWrite(phase3pin, HIGH); delayMicroseconds(delayTime*duty); digitalWrite(phase3pin, LOW); delayMicroseconds(delayTime*(1-duty)); break; } }