MCookie-Motor/zh
Zhangfengfeng(讨论 | 贡献)2017年4月12日 (三) 03:04的版本
Language | English |
---|
mCookie-Motor是一款直流电机(马达)控制器,具有脉宽调制,可通过PWM控制电机速度。必须使用BM模块供电。
特色
规格
开发直流电机接线
//D6,D8控制1A,1B的电机
#define OUT1A 6
#define OUT1B 8
//D5,D7控制2A,2B的电机
#define OUT2A 5
#define OUT2B 7
通断控制
#define OUT1A 6
#define OUT1B 8
#define OUT2A 5
#define OUT2B 7
void setup()
{
pinMode(OUT1A, OUTPUT);
pinMode(OUT1B, OUTPUT);
pinMode(OUT2A, OUTPUT);
pinMode(OUT2B, OUTPUT);
}
void loop()
{
head();
delay(2000);
back();
delay(1000);
stop();
delay(500);
}
void head()
{
digitalWrite(OUT1A, HIGH);
digitalWrite(OUT1B, LOW);
digitalWrite(OUT2A, HIGH);
digitalWrite(OUT2B, LOW);
}
void back()
{
digitalWrite(OUT1A, LOW);
digitalWrite(OUT1B, HIGH);
digitalWrite(OUT2A, LOW);
digitalWrite(OUT2B, HIGH);
}
void stop()
{
digitalWrite(OUT1A, LOW);
digitalWrite(OUT1B, LOW);
digitalWrite(OUT2A, LOW);
digitalWrite(OUT2B, LOW);
}
PWM调速控制//D6,D8控制1A,1B的电机
#define OUT1A 6
#define OUT1B 8
//D5,D7控制2A,2B的电机
#define OUT2A 5
#define OUT2B 7
void setup()
{
pinMode(OUT1A, OUTPUT);
pinMode(OUT1B, OUTPUT);
pinMode(OUT2A, OUTPUT);
pinMode(OUT2B, OUTPUT);
}
void loop()
{
for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5)
//循环语句,控制PWM速度增加,控制fadeValue的变化大小可以控制亮度级别
{
analogWrite(OUT1A, fadeValue); //将速度级别写入电机
digitalWrite(OUT1B, LOW);
analogWrite(OUT2A, fadeValue);
digitalWrite(OUT2B, LOW);
delay(100); //速度度级别维持的时间,单位毫秒
}
for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5)
//循环语句,控制PWM速度减小,控制fadeValue的变化大小可以控制亮度级别
{
digitalWrite(OUT1A, LOW);
analogWrite(OUT1B, fadeValue); //将速度级别写入电机
digitalWrite(OUT2A, LOW);
analogWrite(OUT2B, fadeValue);
delay(100); //速度度级别级别维持的时间,单位毫秒
}
delay(1000);
}
图库
|