搵翻N年前用BLDC玩機械人的板。
再Load個sketch落去試下。
重work喎。
冇用push button。稍為改下個sketch。
基礎版
=====
//const int buttonPin1 = 4;// the number of the direction pushbutton pin
//const int buttonPin2 = 5;// the number of the direction pushbutton pin
const int ledPin = 13; // the number of the status LED pin (not the flash LED)
const int motorPin1 =9;
const int motorPin2 =10;
const int motorPin3 =11;
const int motorPins[]={6,7,8};
const int motorPinSteps[3][6]={
{1,1,1,0,0,0},
{1,0,0,0,1,1},
{0,0,1,1,1,0}};
int motorDelay=8; // together with pot controls the RPM
boolean direct = true; // direction true=forward, false=backward
int increment;
int currentStepA=0;
///////////////////////////////////////////////////////////////////////////////////
void setup() {
//pinMode(buttonPin1, INPUT);
//digitalWrite(buttonPin1, HIGH); //internal pull up resistor
//pinMode(buttonPin2, INPUT);
//digitalWrite(buttonPin2, HIGH); //internal pull up resistor
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
//
move();
delay(300);
}//end setup()
//////////////////////////////////////////////////////////////////////////////////
//
//
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
currentStepA++;
if (currentStepA > 5) currentStepA=0;
move();
delay( motorDelay);
}//end loop()
///////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////
void move()
{
digitalWrite(motorPin1,motorPinSteps[0][currentStepA]);
digitalWrite(motorPin2,motorPinSteps[1][currentStepA]);
digitalWrite(motorPin3,motorPinSteps[2][currentStepA]);
}//end move()
////////////////////////////////////////////////////////////////////////
sine wave 版
========
//const int buttonPin1 = 4;// the number of the direction pushbutton pin
//const int buttonPin2 = 5;// the number of the direction pushbutton pin
const int ledPin = 13; // the number of the status LED pin (not the flash LED)
const int motorPin1 =9; //pwm
const int motorPin2 =10; //pwm
const int motorPin3 =11; //pwm
int pwmSinA[] = {127,119,110,102,94,86,78,71,64,56,50,43,37,32,26,21,17,13,10,7,4,2,1,0,0,0,1,2,4,7,10,13,17,21,26,32,37,43,50,56,64,71,78,86,94,102,110,119,127,135,144,152,160,168,176,183,191,198,204,211,217,222,228,233,237,241,244,247,250,252,253,254,254,254,253,252,250,247,244,241,237,233,228,222,217,211,204,198,191,183,176,168,160,152,144,135}; // array of PWM duty values for 8-bit timer - sine function
int pwmSinB[] = {237,241,244,247,250,252,253,254,254,254,253,252,250,247,244,241,237,233,228,222,217,211,204,198,191,183,176,168,160,152,144,135,127,119,110,102,94,86,78,71,64,56,50,43,37,32,26,21,17,13,10,7,4,2,1,0,0,0,1,2,4,7,10,13,17,21,26,32,37,43,50,56,64,71,78,86,94,102,110,119,127,135,144,152,160,168,176,183,191,198,204,211,217,222,228,233}; // array of PWM duty values for 8-bit timer - sine function
int pwmSinC[] = {17,21,26,32,37,43,50,56,64,71,78,86,94,102,110,119,127,135,144,152,160,168,176,183,191,198,204,211,217,222,228,233,237,241,244,247,250,252,253,254,254,254,253,252,250,247,244,241,237,233,228,222,217,211,204,198,191,183,176,168,160,152,144,135,127,119,110,102,94,86,78,71,64,56,50,43,37,32,26,21,17,13,10,7,4,2,1,0,0,0,1,2,4,7,10,13}; // array of PWM duty values for 8-bit timer - sine function
int motorDelay=400; //controls the RPM
int direction = 1; // direction 1=cw, 0=ccw
int currentStep=0;
///////////////////////////////////////////////////////////////////////////////////
void setup() {
//pinMode(buttonPin1, INPUT);
//digitalWrite(buttonPin1, HIGH); //internal pull up resistor
//pinMode(buttonPin2, INPUT);
//digitalWrite(buttonPin2, HIGH); //internal pull up resistor
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
//
move();
delay(100);
}//end setup()
//////////////////////////////////////////////////////////////////////////////////
//
//
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
currentStep++;
if (currentStep > 95) currentStep=0;
move();
delayMicroseconds(motorDelay);
}//end loop()
///////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////
void move()
{
analogWrite(motorPin1, pwmSinA[currentStep]);
analogWrite(motorPin2, pwmSinB[currentStep]);
analogWrite(motorPin3, pwmSinC[currentStep]);
}//end move()
////////////////////////////////////////////////////////////////////////
若然唔work, 可能係用嘅BLDC唔同。
試下增大 delay() 或 delayMicroseconds() 嘅值。
或者調換下接線順序。 |