kero0zero:
hi , i need a code to control my brushless Dc motor with potentiometer using ESC and arduino uno borad .
actually im beginner and tried many codes but motor didnat work , i think the problem about maping using , potentiometer min. and max are 0 to 1023 , but motor input or PWM i don
t know the values and i don`t think it will be 0 & 179 like servo motor
kero0zero:
hi , i need a code to control my brushless Dc motor with potentiometer using ESC and arduino uno borad .
actually im beginner and tried many codes but motor didnat work , i think the problem about maping using , potentiometer min. and max are 0 to 1023 , but motor input or PWM i don
t know the values and i don`t think it will be 0 & 179 like servo motorsamthing dis
//
// Slow and precise BLDC motor driver using SPWM and SVPWM modulation
// Part of code used from http://elabz.com/
// (c) 2015 Ignas Gramba www.berryjam.eu
//int sensor = A0;
int pitchOrient = 0;
int step_to_move;
int step_left;
int previous;// Pins to control pitch motor
const int pitchMotor1 = 9;
const int pitchMotor2 = 10;
const int pitchMotor3 = 11;// SPWM (Sine Wave)
//const int pwmSin[] = {127, 138, 149, 160, 170, 181, 191, 200, 209, 217, 224, 231, 237, 242, 246, 250, 252, 254, 254, 254, 252, 250, 246, 242, 237, 231, 224, 217, 209, 200, 191, 181, 170, 160, 149, 138, 127, 116, 105, 94, 84, 73, 64, 54, 45, 37, 30, 23, 17, 12, 8, 4, 2, 0, 0, 0, 2, 4, 8, 12, 17, 23, 30, 37, 45, 54, 64, 73, 84, 94, 105, 116 };const int pwmSin[]={127,111,94,78,64,50,37,26,17,9,4,1,0,1,4,9,17,26,37,50,64,78,
94,111,127,144,160,176,191,205,218,229,238,245,251,254,255,
254,251,245,238,229,218,205,191,176,160,144,127};//const int pwmSin[]={128,167,202,231,249,255,249,231,202167,128,88,53,24,6,0,6,24,53,8,128};
/// SVPWM (Space Vector Wave)
//const int pwmSin[] = {128, 147, 166, 185, 203, 221, 238, 243, 248, 251, 253, 255, 255, 255, 253, 251, 248, 243, 238, 243, 248, 251, 253, 255, 255, 255, 253, 251, 248, 243, 238, 221, 203, 185, 166, 147, 128, 109, 90, 71, 53, 35, 18, 13, 8, 5, 3, 1, 1, 1, 3, 5, 8, 13, 18, 13, 8, 5, 3, 1, 1, 1, 3, 5, 8, 13, 18, 35, 53, 71, 90, 109};int currentStepA;
int currentStepB;
int currentStepC;
int sineArraySize;
int val = 0;int increment = 0;
boolean direct = 1; // direction true=forward, false=backward//////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
setPwmFrequency(pitchMotor1); // Increase PWM frequency to 32 kHz (make unaudible)
setPwmFrequency(pitchMotor2);
setPwmFrequency(pitchMotor3);pinMode(sensor, INPUT);
pinMode(pitchMotor1, OUTPUT);
pinMode(pitchMotor2, OUTPUT);
pinMode(pitchMotor3, OUTPUT);sineArraySize = sizeof(pwmSin)/sizeof(int); // Find lookup table size
int phaseShift = sineArraySize / 3; // Find phase shift and initial A, B C phase values
currentStepA = 0;
currentStepB = currentStepA + phaseShift;
currentStepC = currentStepB + phaseShift;sineArraySize--; // Convert from array Size to last PWM array number
}//////////////////////////////////////////////////////////////////////////////
void loop() {
val = analogRead(sensor);
val = map(val, 0, 1023, -250, 250);
step_to_move = val; // steps to move
steps(step_to_move - previous); //move steps equel sensor reading
previous = step_to_move; // remember the previous steps
/* Serial.print("val ");
Serial.print(val);
Serial.print("\t");
Serial.print("step_to_move ");
Serial.print(step_to_move);
Serial.print("\t");
Serial.print(" step_left ");
Serial.println(step_left);/// Control speed by this delay*/
delay(1);} // end loop
int steps(int step_to_move){
step_left = abs(step_to_move);
if (step_to_move > pitchOrient) increment = 1;
if (step_to_move < pitchOrient) increment = -1;while (step_left > pitchOrient){
Move();
step_left--;
}} //end steps
void Move(){
//if(step_to_move > pitchOrient) increment = 1;
//if(step_to_move < pitchOrient) increment = -1;//Check for lookup table overflow and return to opposite end if necessary
currentStepA = currentStepA + increment;
if(currentStepA > sineArraySize) currentStepA = 0;
if(currentStepA < 0) currentStepA = sineArraySize;currentStepB = currentStepB + increment;
if(currentStepB > sineArraySize) currentStepB = 0;
if(currentStepB < 0) currentStepB = sineArraySize;currentStepC = currentStepC + increment;
if(currentStepC > sineArraySize) currentStepC = 0;
if(currentStepC < 0) currentStepC = sineArraySize;analogWrite(pitchMotor1, pwmSin[currentStepA]);
analogWrite(pitchMotor2, pwmSin[currentStepB]);
analogWrite(pitchMotor3, pwmSin[currentStepC]);} //end move*/
void setPwmFrequency(int pin) {
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | 0x01;
} else {
TCCR1B = TCCR1B & 0b11111000 | 0x01;
}
}
else if(pin == 3 || pin == 11) {
TCCR2B = TCCR2B & 0b11111000 | 0x01;
}
} //end setPwmFrequentcyfreind Rob Kloet
Split from...
http://forum.arduino.cc/index.php?topic=137736.0
Hello @Robertus. What can we do for you?
want to help samwane with a problem but don't no how to post.
Robertus.
There is no "samwane" in the other thread. There is no "samwane" on this forum. I doubt you meant "animal".
You appear to be doing a fine job of posting.
Making statements that contradict your actions / reality are going to be a waste of time. I suggest you try asking questions.