You are on page 1of 12

BRUSLESS MOTOR CONTROLLER

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

unsigned long stepLength = 40000;


int minStepLength = 1400;
int steps = 5;

void setup() {
pinMode(led, OUTPUT);
pinMode(phase1, OUTPUT);
pinMode(phase2, OUTPUT);
pinMode(phase3, OUTPUT);
digitalWrite(led, LOW);
}

void loop() {
switchStep(1);
switchStep(2);
switchStep(3);

if(stepLength > minStepLength)


{
stepLength = stepLength - steps;
} else {
// set the minimum pulse length
stepLength=minStepLength;
}
if (stepLength < 39950) {
digitalWrite(led, HIGH); // second gear
steps = 300;
}

if (stepLength < 20000) {


digitalWrite(led, LOW); // third gear
steps = 50;
}

if (stepLength < 3000) {


digitalWrite(led, HIGH); // fourth gear
steps = 2;
}
}

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);
}
}
const int phase1pin = 2;

const int phase2pin = 3;


const int phase3pin = 4;
const int delayTime = 6000; // microsecs

void setup(){
Serial.begin(9600);
pinMode(phase1pin, OUTPUT);
pinMode(phase2pin, OUTPUT);
pinMode(phase3pin, OUTPUT);
}

void loop(){
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);
break;
case 2:
digitalWrite(phase1pin, LOW);
digitalWrite(phase2pin, HIGH);
digitalWrite(phase3pin, LOW);
delayMicroseconds(delayTime);
break;
case 3:
digitalWrite(phase1pin, LOW);
digitalWrite(phase2pin, LOW);
digitalWrite(phase3pin, HIGH);
delayMicroseconds(delayTime);
break;
}
}

const int phase1pin = 2;

const int phase2pin = 3;


const int phase3pin = 4;
float holdTime = 50000; // microsecs
const unsigned long minHoldTime = 1300;

unsigned long p1start,


p1end,
p2start,
p2end,
p3start,
p3end;

void setup(){
Serial.begin(9600);
pinMode(phase1pin, OUTPUT);
pinMode(phase2pin, OUTPUT);
pinMode(phase3pin, OUTPUT);
p1start = micros();
digitalWrite(phase1pin, HIGH);
}

void chkP1(){
unsigned long currentTime = micros();
unsigned long td = currentTime - p1start;
unsigned long refractory = 2.25*holdTime;
if(digitalRead(phase1pin)){
if(td > holdTime){
digitalWrite(phase1pin, LOW);
p1end = currentTime;
}
}else if(td > refractory){
digitalWrite(phase1pin, HIGH);
p1start = currentTime;
}
}

void chkP2(){
unsigned long currentTime = micros();
unsigned long td = currentTime - p1start;
if(digitalRead(phase2pin)){
if(td > 1.75*holdTime || td < 0.75*holdTime){
digitalWrite(phase2pin, LOW);
p2end = currentTime;
}
}else if(td > 0.75*holdTime && td < 1.75*holdTime){
digitalWrite(phase2pin, HIGH);
p2start = currentTime;
}
}

void chkP3(){
unsigned long currentTime = micros();
unsigned long td = currentTime - p1start;
if(digitalRead(phase3pin)){
if(td > 0.25*holdTime && p3start < p1start){
digitalWrite(phase3pin, LOW);
p3end = currentTime;
}
}else if(td > 1.5*holdTime){
digitalWrite(phase3pin, HIGH);
p3start = currentTime;
}
}

void loop(){
chkP1();
chkP2();
chkP3();
delayMicroseconds(100);
if(holdTime >= minHoldTime){
holdTime -= 0.5;
}
}
YANG LAEN:

The code is as follow:

/*
***** BLDC DRIVER *****
*/
int wait = 10;
int p1 = 2;
int p2 = 3;
int p3 = 4;
char inChar;

void setup() {
pinMode(p1, OUTPUT);
pinMode(p2, OUTPUT);
pinMode(p3, OUTPUT);
Serial.begin(9600);
}

// the loop routine runs over and over again forever:


void loop() {

if (Serial.available()){
inChar = (char)Serial.read();
if (inChar == '-'){
wait -=1;
}
else{
wait +=1;
}
Serial.println(wait);
}

digitalWrite(p1, 1);
digitalWrite(p2, 1);
digitalWrite(p3, 0);
delay(wait);
digitalWrite(p1, 1);
digitalWrite(p2, 0);
digitalWrite(p3, 0);
delay(wait);
digitalWrite(p1, 1);
digitalWrite(p2, 0);
digitalWrite(p3, 1);
delay(wait);
digitalWrite(p1, 0);
digitalWrite(p2, 0);
digitalWrite(p3, 1);
delay(wait);
digitalWrite(p1, 0);
digitalWrite(p2, 1);
digitalWrite(p3, 1);
delay(wait);
digitalWrite(p1, 0);
digitalWrite(p2, 1);
digitalWrite(p3, 0);
delay(wait);
}

PAKE SPEED CONTROL


const int
phase1pin
= 3;
const int phase2pin = 5;
const int phase3pin = 6;
float holdTime;
float targetSpeed;

unsigned long p1start,


p1end,
p2stafghfgfghrt,
p2end,
p3start,
p3end;

// Change these until you find your min and max speeds.
float min = 3200;
float max = 50000;
void setup() {
Serial.begin(115200);
pinMode(phase1pin, OUTPUT);
pinMode(phase2pin, OUTPUT);
pinMode(phase3pin, OUTPUT);
}
String direction;
bool running = false;
float status;
void chkP1(int pin){
unsigned long currentTime = micros();
unsigned long td = currentTime - p1start;
unsigned long refractory = 2.25*holdTime;
if(digitalRead(pin)){
if(td > holdTime){
digitalWrite(pin, LOW);
p1end = currentTime;
}
}else if(td > refractory){
digitalWrite(pin, HIGH);
p1start = currentTime;
}
}

void chkP2(int pin){


unsigned long currentTime = micros();
unsigned long td = currentTime - p1start;
if(digitalRead(pin)){
if(td > 1.75*holdTime || td < 0.75*holdTime){
digitalWrite(pin, LOW);
p2end = currentTime;
}
}else if(td > 0.75*holdTime && td < 1.75*holdTime){
digitalWrite(pin, HIGH);
p2start = currentTime;
}
}

void chkP3(int pin){


unsigned long currentTime = micros();
unsigned long td = currentTime - p1start;
if(digitalRead(pin)){
if(td > 0.25*holdTime && p3start < p1start){
digitalWrite(pin, LOW);
p3end = currentTime;
}
}else if(td > 1.5*holdTime){
digitalWrite(pin, HIGH);
p3start = currentTime;
}
}
void setupMotor(float input_targetSpeed, String new_direction)
{
// Converts the percent value into something usable
targetSpeed = max-((max-min)*(input_targetSpeed*0.01));
// See if the direction has changed
if(new_direction != direction)
{
holdTime = 20000;
direction = new_direction;
// Wait for it to stop, weird things can happen if it's still moving.
delay(500);
// Initialize first phase
if(direction == "forward")
{
digitalWrite(phase1pin, HIGH);
}
else if(direction == "reverse")
{
digitalWrite(phase3pin, HIGH);
}
}

running = true;
}

void loop(){
if(Serial.available() > 0)
{
// Read serial input ex. "100 reverse" or "50 forward"
float input_targetSpeed = Serial.readStringUntil(' ').toInt();

//Serial.read(); //next character is space, so skip it using this


String new_direction = Serial.readStringUntil('\n');
setupMotor(input_targetSpeed, new_direction);
}

if(running) {

if(direction == "forward")
{
chkP1(phase3pin);
chkP2(phase2pin);
chkP3(phase1pin);
}
else if(direction == "reverse")
{
chkP1(phase1pin);
chkP2(phase2pin);
chkP3(phase3pin);
}

delayMicroseconds(100);

if(holdTime > targetSpeed)


{
holdTime -= 0.5;
}
else if(holdTime < targetSpeed)
{
holdTime += 0.25;
}
// Monitors the current speed
status = -(holdTime-max)/((max-min)*(0.01));

if(status == 100)
{
// setupMotor(100, "reverse");
}
}

EDIT:

Look this variable p2start, is not work.

fix it

unsigned long p1start,


p1end,
p2stafghfgfghrt,
p2end,
p3start,
p3end;

ATAU:
unsigned long p1start,
p1end,
p2start,
p2end,
p3start,
p3end;

I'm trying to make it run as fast as possible and I start with this code
(arduino uno):

int pin1 = 2;
int pin2 = 3;
int pin3 = 4;

int delay_time = 100;

void setup() {
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
}

void loop() {
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
delay(delay_time);
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, LOW);
delay(delay_time);

digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, HIGH);
delay(delay_time);
}
The motor runs when delay_time bigger than 50 (it run very slowly of course),
with delay_time smaller it just "vibrate". So how can I make that runs at 7200
rpm?