回覆 9# aazz
呢個係用L298嘅例子。 用查表法簡化 sin 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
const int motorDelay=300; //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()
{
if (digitalRead(buttonPin1) == LOW)
{
currentStep++;
if (currentStep > 95) currentStep=0;
move();
}
if (digitalRead(buttonPin2) == LOW)
{
currentStep--;
if (currentStep < 0) currentStep=95;
move();
}
delay(motorDelay);
}//end loop()
///////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////
void move()
{
analogWrite(motorPin1, pwmSinA[currentStep]);
analogWrite(motorPin2, pwmSinB[currentStep]);
analogWrite(motorPin3, pwmSinC[currentStep]);
}//end move()
//////////////////////////////////////////////////////////////////////// |