遇到一个诡异的问题,谁帮我分析一下代码
这是个红外遥控监控云台的代码,可以设置云台转动速度3个档位诡异的问题就出现在函数FunSetSpeed(float SpeedValue,int LedV)中:
……
void FunSetSpeed(float SpeedValue,int LedV)
{
FunStop();
SpeedTo=SpeedValue;//这句如果注释掉就没问题,顺利执行
digitalWrite(SPeedLed1,LOW);
digitalWrite(SPeedLed2,LOW);
……
SpeedTo=SpeedValue;这句代码是要将速度值赋用于运算,执行到这并没有问题,但是会导致接收到的遥控器发射码异常,基于发射码的判断就失效了,而且这个错误总是发生在FunMac(3)函数调用之后。
完整的详细代码如下
#include <IRremote.h>// 使用IRRemote函数库
//======================================
const int ReceiverPin = 13;// 红外接收器的 OUTPUT 引脚接
const int ReLed=12;//信号接收指示灯引脚
const int MPinOutA0=5;//machineA+
const int MPinOutA1=6;//machineA-
const int MPinOutB0=10;//machineB+
const int MPinOutB1=11;//machineB-
const int SPeedLed1=7;//转速档位指示灯引脚
const int SPeedLed2=8;//转速档位指示灯引脚
const int SPeedLed3=9;//转速档位指示灯引脚
const int TakePic=2;//拍照信号指示灯引脚
//======================================
const long KEY_UP = 0xFFC23D;//1
const long KEY_DOWN = 0xFF22DD;//2
const long KEY_LEFT = 0xFFA25D;//3
const long KEY_RIGHT = 0xFFE21D;//4
const long KEY_SPEED1 = 0xFF30CF;//5
const long KEY_SPEED2 = 0xFF18E7;//5
const long KEY_SPEED3 = 0xFF7A85;//5
const long KEY_TAKEPIC = 0xFF02FD;//7
const long KEY_STOP = 0xFF629D;//7
const long KEY_NULL = 0xFFFFFFFF;//bak
//======================================
float Speed1=1.53;//1X
float Speed2=3.06;//2X
float Speed3=50;//FULL
//======================================
float SpeedTo=0;
//int LastKey=0;
IRrecv irrecv(ReceiverPin); // 设置irReceiverPin定义的端口为红外信号接收端口
decode_results KeyResults; // 定义KeyResults变量为红外结果存放位置
void setup() {
pinMode(ReLed, OUTPUT);
pinMode(MPinOutA0, OUTPUT);
pinMode(MPinOutA1, OUTPUT);
pinMode(MPinOutB0, OUTPUT);
pinMode(MPinOutB1, OUTPUT);
pinMode(SPeedLed1, OUTPUT);
pinMode(SPeedLed2, OUTPUT);
pinMode(SPeedLed3, OUTPUT);
pinMode(TakePic, OUTPUT);
//======================================
digitalWrite(ReLed,LOW);
digitalWrite(MPinOutA0,LOW);
digitalWrite(MPinOutA1,LOW);
digitalWrite(MPinOutB0,LOW);
digitalWrite(MPinOutB1,LOW);
digitalWrite(SPeedLed1,LOW);
digitalWrite(SPeedLed2,LOW);
digitalWrite(SPeedLed3,LOW);
digitalWrite(TakePic,LOW);
//======================================
Serial.begin(9600);//debug
irrecv.enableIRIn(); // 启动红外解码
}
//===============================================
void loop()
{
if(irrecv.decode(&KeyResults))
{
ReFlash();//Led3
IRCodeGet(KeyResults.value);
irrecv.resume();
}
else
{
delay(500);
}
}
void IRCodeGet(long lkey)
{
Serial.print("LKey:"); //debug
Serial.print(lkey,HEX); //debug
Serial.println("|"); //debug
switch(lkey)
{
case KEY_TAKEPIC:
FunTakePic(200);
break;
case KEY_STOP:
FunStop();
break;
//===================================
case KEY_UP:
FunMac(1);
break;
case KEY_DOWN:
FunMac(2);
break;
case KEY_LEFT:
FunMac(3);
break;
case KEY_RIGHT:
FunMac(4);
break;
//===================================
case KEY_SPEED1:
FunSetSpeed(Speed1,1);
break;
case KEY_SPEED2:
FunSetSpeed(Speed2,2);
break;
case KEY_SPEED3:
FunSetSpeed(Speed3,3);
break;
//===================================
case KEY_NULL:
;
break;
//FunMac(LastKey);
}
}
void ReFlash()
{
for(int i=1;i<2;i++)
{
digitalWrite(ReLed,HIGH);
delay(50);//
digitalWrite(ReLed,LOW);
delay(50);//
}
}
void FunTakePic(int delValue)
{
digitalWrite(TakePic,HIGH);
delay(delValue);//
digitalWrite(TakePic,LOW);
//LastKey=0;
}
void FunStop()
{
analogWrite(MPinOutA0,LOW);
analogWrite(MPinOutA1,LOW);
analogWrite(MPinOutB0,LOW);
analogWrite(MPinOutB1,LOW);
//LastKey=0;
}
void FunMac(int inDirect)
{
Serial.print("Direction:"); //debug
Serial.print(intDirect,HEX); //debug
Serial.print("|"); //debug
FunStop();
if(inDirect==1)
{
//LastKey=1;
analogWrite(MPinOutA0,LOW);
analogWrite(MPinOutA1,SpeedTo*50);
}
else if(inDirect==2)
{
//LastKey=2;
analogWrite(MPinOutA0,SpeedTo*50);
analogWrite(MPinOutA1,LOW);
}
else if(inDirect==3)
{
//LastKey=3;
analogWrite(MPinOutB0,LOW);
analogWrite(MPinOutB1,SpeedTo*50);
}
else if(inDirect==4)
{
//LastKey=4;
analogWrite(MPinOutB0,SpeedTo*50);
analogWrite(MPinOutB1,LOW);
}
else
{
;
}
}
void FunSetSpeed(float SpeedValue,int LedV)
{
FunStop();
SpeedTo=SpeedValue;
digitalWrite(SPeedLed1,LOW);
digitalWrite(SPeedLed2,LOW);
digitalWrite(SPeedLed3,LOW);
if(LedV==1)
{
digitalWrite(SPeedLed1,HIGH);
}
else if(LedV==2)
{
digitalWrite(SPeedLed2,HIGH);
}
else if(LedV==3)
{
digitalWrite(SPeedLed3,HIGH);
}
else
{
;
}
//LastKey=0;
} 经过不断试验,我发现11脚有问题,不支持PWM输出,有点蒙了。书上写的,11脚是支持PWM的。 你要不换一块板子试试
页:
[1]