|
背景:在昨天实现安卓手机通过蓝牙操控小车,今天打算用红外遥控器实现操控小车。定义红外遥控器的数字2为前进、4为左转、5为停止、6为右转、8为后退。
硬件:arduino uno 、arduino sensor shield v5.0、L298驱动x2 、7.2V电源包、红外遥控器、红外接收器、蓝牙HC05从机
问题:USB供电的情况下,通过arduino IDE工具中的串口监视器测试没有任何问题。接入7.2V电源发现在使用红外遥控器操控时,只有数字2生效,其余按键失灵,小车的状态一直处于前进状态。
程序代码
#include <IRremote.h>
//Left Front
const int RECV_PIN=0;
IRrecv irrecv(RECV_PIN);
decode_results results;
const int motorA1=10;
const int motorA2=11;
//Left Back
const int motorB1=2;
const int motorB2=3;
//Right Back
const int motorC1=4;
const int motorC2=5;
//Right Front
const int motorD1=6;
const int motorD2=7;
int delayTime=1000;
void setup()
{
pinMode(motorA1,OUTPUT);
pinMode(motorA2,OUTPUT);
pinMode(motorB1,OUTPUT);
pinMode(motorB2,OUTPUT);
pinMode(motorC1,OUTPUT);
pinMode(motorC2,OUTPUT);
pinMode(motorD1,OUTPUT);
pinMode(motorD2,OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop()
{
if(irrecv.decode(&results))
{
Serial.println(results.value);
if(results.value==0xFF18E7)
{
Serial.println("Forward");
moveFront();
}
else if(results.value==0xFF4AB5)
{
Serial.println("Backwards");
moveBack();
}
else if(results.value==0xFF10EF)
{
Serial.println("Left");
moveLeft();
}
else if(results.value==0xFF5AA5)
{
Serial.println("Right");
moveRight();
}else if(results.value==0xFF38C7)
{
Serial.println("Stop");
stopMove();
}
delay(delayTime);
irrecv.resume();
}
}
void moveFront() //
{
digitalWrite(motorA1,HIGH);
digitalWrite(motorA2,LOW);
digitalWrite(motorB1,HIGH);
digitalWrite(motorB2,LOW);
digitalWrite(motorC1,HIGH);
digitalWrite(motorC2,LOW);
digitalWrite(motorD1,HIGH);
digitalWrite(motorD2,LOW);
}
void moveBack()
{
digitalWrite(motorA1,LOW);
digitalWrite(motorA2,HIGH);
digitalWrite(motorB1,LOW);
digitalWrite(motorB2,HIGH);
digitalWrite(motorC1,LOW);
digitalWrite(motorC2,HIGH);
digitalWrite(motorD1,LOW);
digitalWrite(motorD2,HIGH);
}
void moveRight()
{
digitalWrite(motorA1,HIGH);
digitalWrite(motorA2,LOW);
digitalWrite(motorB1,HIGH);
digitalWrite(motorB2,LOW);
digitalWrite(motorC1,LOW);
digitalWrite(motorC2,LOW);
digitalWrite(motorD1,LOW);
digitalWrite(motorD2,LOW);
}
void moveLeft()
{
digitalWrite(motorA1,LOW);
digitalWrite(motorA2,LOW);
digitalWrite(motorB1,LOW);
digitalWrite(motorB2,LOW);
digitalWrite(motorC1,HIGH);
digitalWrite(motorC2,LOW);
digitalWrite(motorD1,HIGH);
digitalWrite(motorD2,LOW);
}
void stopMove()
{
digitalWrite(motorA1,LOW);
digitalWrite(motorA2,LOW);
digitalWrite(motorB1,LOW);
digitalWrite(motorB2,LOW);
digitalWrite(motorC1,LOW);
digitalWrite(motorC2,LOW);
digitalWrite(motorD1,LOW);
digitalWrite(motorD2,LOW);
}
测试输出代码:
16718055
Forward
16730805
Backwards
16726215
Stop
16734885
Right
16716015
Left
16716015
Left
16716015
Left
16726215
Stop
不知道从哪下手了。
明天打算在7.2V外部电源供电的情况下,将测试数据通过蓝牙再将数据回传给我电脑,还没想好怎么实现。
有论坛各位帮忙看看哪里有问题
|
|