我需要运行我的BLDC电机。我偶然发现了这个代码,它可以让我通过按下键盘上的数字来移动我的BLDC电机。这个代码使我能够按下键盘编号并将电机设置为所需的转速。但我的马达似乎根本没有跑。请告诉我问题,也许是为什么我的BLDC电机不能运行的建议。
以下是代码:
// include the library code:
#include <LiquidCrystal.h>
#include <EEPROM.h>
#include <Keypad.h>
const byte ROWS = 5; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
char key_value='0';
char key_value1='0';
int RPM_Pin = 10; // LED connected to digital pin 9
char key_start_stop='#';
char RPM_Timer='A';
String timer="00";
String timer1="00";
String timer2="00";
int toggle=1;
int toggle1=0;
int T=10;
long v_time=0;
String RPM="0000";
String RPM1="0000";
String RPM2="0000";
int toggle_RPM=0;
int settings=0;
boolean settings_bit=false;
void setup()
{
pinMode(RPM_Pin, OUTPUT);
digitalWrite(RPM_Pin, LOW);
analogWrite(RPM_Pin, 0);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(10, 0);// lcd cursor set to row1 & col1
lcd.print("00; T1");
lcd.setCursor(12, 2);// lcd cursor set to row1 & col1
lcd.print("0000");
Serial.begin(9600);
}
void loop()
{
char customKey = customKeypad.getKey();
if (customKey)
{
Serial.println(customKey);
if (customKey=='*' || customKey=='#' || customKey=='A' || customKey=='B' || customKey=='C')
{
if (customKey=='*')
{
key_start_stop = customKey;
T=timer.toInt();
key_value=RPM[2];
key_value1=RPM[3];
toggle_RPM=0;
if (settings==1)
{
settings_bit=true;
}
else
{
settings_bit=false;
}
}
else if (customKey=='#')
{
key_start_stop = customKey;
toggle1=1;
settings=0;
T=0;
}
else if (customKey=='A')
{
RPM_Timer=customKey;
lcd.setCursor(12, 0);// lcd cursor set to row1 & col12
lcd.print("; T");
}
else if (customKey=='B')
{
RPM_Timer=customKey;
lcd.setCursor(12, 0);
lcd.print("; R");
}
else if (customKey=='C')
{
RPM_Timer='A';
settings++;
if (settings>=3)
{
settings=0;
}
if (settings==1)
{
lcd.setCursor(10, 0);
lcd.print(timer1);
lcd.setCursor(12, 0);
lcd.print("; T2");
lcd.setCursor(12, 1);
lcd.print(RPM1[3]);
lcd.print(RPM1[2]);
lcd.print(RPM1[1]);
lcd.print(RPM1[0]);
}
else if (settings==2)
{
lcd.setCursor(10, 0);
lcd.print(timer2);
lcd.setCursor(12, 0);
lcd.print("; T3");
lcd.setCursor(12, 1);
lcd.print(RPM2[3]);
lcd.print(RPM2[2]);
lcd.print(RPM2[1]);
lcd.print(RPM2[0]);
}
else if (settings==0)
{
lcd.setCursor(10, 0);
lcd.print(timer);
lcd.setCursor(12, 0);
lcd.print("; T1");
lcd.setCursor(12, 1);
lcd.print(RPM[3]);
lcd.print(RPM[2]);
lcd.print(RPM[1]);
lcd.print(RPM[0]);
}
}
}
else if(RPM_Timer=='A' && key_start_stop=='#' && customKey!='C')
{
if (settings==0)
{
lcd.setCursor(12, 0);
lcd.print("; T1");
toggle_RPM=0;
if (toggle==0 )
{
timer[1]=customKey;
toggle=1;
lcd.setCursor(10, 0);
lcd.print(timer);
}
else
{
timer[0]=customKey;
toggle=0;
lcd.setCursor(10, 0);
lcd.print(timer);
}
}
else if (settings==1)
{
lcd.setCursor(12, 0);
lcd.print("; T2");
toggle_RPM=0;
if (toggle==0 )
{
timer1[1]=customKey;
toggle=1;
lcd.setCursor(10, 0);
lcd.print(timer1);
}
else
{
timer1[0]=customKey;
toggle=0;
lcd.setCursor(10, 0);
lcd.print(timer1);
}
}
else
{
settings=2;
lcd.setCursor(12, 0);
lcd.print("; T3");
toggle_RPM=0;
if (toggle==0 )
{
timer2[1]=customKey;
toggle=1;
lcd.setCursor(10, 0);
lcd.print(timer2);
}
else
{
timer2[0]=customKey;
toggle=0;
lcd.setCursor(10, 0);
lcd.print(timer2);
}
}
}
else if(RPM_Timer=='B' && key_start_stop=='#')
{
// Serial.println(RPM);
if (settings==0)
{
lcd.setCursor(12, 0);
lcd.print("; R1");
if (toggle_RPM>=4)
toggle_RPM=0;
if (toggle_RPM==0 )
{
RPM[3]=customKey;
lcd.setCursor(12, 1);
lcd.print(RPM[3]);
}
else if (toggle_RPM==1 )
{
RPM[2]=customKey;
lcd.setCursor(13, 1);
lcd.print(RPM[2]);
}
else if (toggle_RPM==2 )
{
RPM[1]=customKey;
lcd.setCursor(14, 1);
lcd.print(RPM[1]);
}
else
{
RPM[0]=customKey;
lcd.setCursor(15, 1);
lcd.print(RPM[0]);
}
toggle_RPM++;
}
else if (settings==1)
{
lcd.setCursor(12, 0);
lcd.print("; R2");
if (toggle_RPM>=4)
toggle_RPM=0;
if (toggle_RPM==0 )
{
RPM1[3]=customKey;
lcd.setCursor(12, 1);
lcd.print(RPM1[3]);
}
else if (toggle_RPM==1 )
{
RPM1[2]=customKey;
lcd.setCursor(13, 1);
lcd.print(RPM1[2]);
}
else if (toggle_RPM==2 )
{
RPM1[1]=customKey;
lcd.setCursor(14, 1);
lcd.print(RPM1[1]);
}
else
{
RPM1[0]=customKey;
lcd.setCursor(15, 1);
lcd.print(RPM1[0]);
}
toggle_RPM++;
}
else
{
settings=2;
lcd.setCursor(12, 0);
lcd.print("; R3");
if (toggle_RPM>=4)
toggle_RPM=0;
if (toggle_RPM==0 )
{
RPM2[3]=customKey;
lcd.setCursor(12, 1);
lcd.print(RPM2[3]);
}
else if (toggle_RPM==1 )
{
RPM2[2]=customKey;
lcd.setCursor(13, 1);
lcd.print(RPM2[2]);
}
else if (toggle_RPM==2 )
{
RPM2[1]=customKey;
lcd.setCursor(14, 1);
lcd.print(RPM2[1]);
}
else
{
RPM2[0]=customKey;
lcd.setCursor(15, 1);
lcd.print(RPM2[0]);
}
toggle_RPM++;
}
}
}
lcd.setCursor(0, 2);
lcd.print("FAN RPM IS :");
if(key_start_stop=='*' && T>0)
{
lcd.setCursor(0, 0);
lcd.print("FAN IS ON...T:");
if (T>9)
{
lcd.print(T);
}
else
{
lcd.print("0");
lcd.print(T);
}
lcd.setCursor(12, 2);
if(key_value1!='1')
{
switch (key_value)
{
case '2':
analogWrite(RPM_Pin, 51);
lcd.print("0200");
break;
case '3':
analogWrite(RPM_Pin, 77);
lcd.print("0300");
break;
case '4':
analogWrite(RPM_Pin, 102);
lcd.print("0400");
break;
case '5':
analogWrite(RPM_Pin, 128);
lcd.print("0500");
break;
case '6':
analogWrite(RPM_Pin, 153);
lcd.print("0600");
break;
case '7':
analogWrite(RPM_Pin, 179);
lcd.print("0700");
break;
case '8':
analogWrite(RPM_Pin, 204);
lcd.print("0800");
break;
case '9':
analogWrite(RPM_Pin, 230);
lcd.print("0900");
break;
default:
analogWrite(RPM_Pin, 35);
lcd.print("0100");
break;
}
}
else
{
analogWrite(RPM_Pin, 255);
lcd.print("1000");
}
}
else
{
lcd.setCursor(0, 0);// lcd cursor set to row1 & col1
lcd.print("FAN OFF.T:");
if (toggle1==1)
{
lcd.setCursor(10, 0);// lcd cursor set to row1 & col15
lcd.print(timer);
toggle1=0;
// settings=0;
if(RPM_Timer=='A')
{
lcd.setCursor(12, 0);// lcd cursor set to row1 & col15
lcd.print("; T0");
}
else
{
lcd.setCursor(12, 0);// lcd cursor set to row1 & col15
lcd.print("; R0");
}
}
analogWrite(RPM_Pin, 0);
digitalWrite(RPM_Pin, LOW);
}
if (v_time>=80)
{
v_time=0;
timer_virtual();
}
else
{
v_time++;
}
}
//================================================================================//
//
//================================================================================//
void timer_virtual()
{
if (key_start_stop=='*' && T>0)
{
T--;
}
else if (key_start_stop=='*')
{
//key_start_stop='#';
settings--;
// Serial.println(settings);
if (settings==1 || settings_bit==true)
{
// timer="00";
key_start_stop=='*';
T=timer1.toInt();
key_value=RPM1[2];
key_value1=RPM1[3];
settings_bit=false;
}
else if (settings==0)
{
// timer1="00";
key_start_stop=='*';
T=timer2.toInt();
key_value=RPM2[2];
key_value1=RPM2[3];
// Serial.println(settings);
}
else
{
key_start_stop='#';
settings=0;
toggle1=1;
}
}
}
答案 0 :(得分:0)
当您使用Arduino时,BLDC将无法使用analogWrite函数运行您必须使用servo.write(val)
BLSC的功能。
#include <Servo.h>
Servo myservo; // create servo object to control a servo
pos = 50;
void setup()
{
myservo.attach(9); // attaches the servo on D9 to the servo object
}
void loop()
{
// The min and max value is 0 and 180
//Change pos variable according to your requirement
myservo.write(pos);
delay(40);
}