|
本帖最后由 sunzewei 于 2015-1-17 22:36 编辑
大家好,本人arduino初学者,想利用红外遥控来控制小车,但是第一步就出现问题了。。
我用L298P电机扩展板来驱动小车的四个电机, 已经实现电机的控制。红外部分也已经基本测试完,但是将两部分加在一起时,出现电机只有一边转,另一边不转的情况。
后来反复测试了一下代码,发现是只要在 “void setup() ”里面加入“ irrecv.enableIRIn(); // 启动红外解码”这一句,电机就会出现这种一边转另一边不转的情况,但是这句又是红外遥控必须的。
有没有哪位知道这是怎么回事? 测了一下电压,发现不转的那边线路的电压基本为0,为什么启动红外解码会使一边电机无法供电? 求解救。。
下面是代码。。(就是只有一边转的情况下的, 只要将代码里面的那句 irrecv.enableIRIn(); 删掉,电机就两边都能转。。)
#include <IRremote.h> // 使用IRRemote函数库
const int irReceiverPin = 2;
IRrecv irrecv(irReceiverPin); // 设置irReceiverPin定义的端口为红外信号接收端口
decode_results results; // 定义results变量为红外结果存放位置
/**set control port**/
const int E1Pin = 10;
const int M1Pin = 12;
const int E2Pin = 11;
const int M2Pin = 13;
/**inner definition**/
typedef struct
{
byte enPin;
byte directionPin;
}MotorContrl;
const int M1 = 0;
const int M2 = 1;
const int MotorNum = 2;
const MotorContrl MotorPin[] ={ {E1Pin,M1Pin}, {E2Pin,M2Pin} } ;
const int Forward = LOW;
const int Backward = HIGH;
/**program**/
void setup()
{
initMotor();
irrecv.enableIRIn(); // 启动红外解码
}
void loop()
{
int value;
setMotorDirection( M2, Forward );
setMotorSpeed( M2, 50 );
setMotorDirection( M1, Forward );
setMotorSpeed( M1, 50 );
}
/**functions**/
void initMotor( )
{
int i;
for ( i = 0; i < MotorNum; i++ )
{
digitalWrite(MotorPin.enPin, LOW);
pinMode(MotorPin.enPin, OUTPUT);
pinMode(MotorPin.directionPin, OUTPUT);
}
}
/** motorNumber: M1, M2
direction: Forward, Backward **/
void setMotorDirection( int motorNumber, int direction )
{
digitalWrite( MotorPin[motorNumber].directionPin, direction);
}
/** speed: 0-100 * */
inline void setMotorSpeed( int motorNumber, int speed )
{
analogWrite(MotorPin[motorNumber].enPin, 255.0 * (speed/100.0) ); //PWM
} |
|