之前在试验红外的时候,便已出现错误,在搞定后发现小车还是不动。具体如下:
landzo c1小车,例程里,红外接收脚是8,接收不到,其他引脚可以,我程序是8脚,也接在D8上,接收无反应,接在7脚,蓝灯会闪烁,偶尔能接收到。
程序改在7脚,也接在7脚,就都OK.小车可以控制,其他脚也可以,就是教程里的8脚不可以。
开始一直以为是程序问题,小车控制的不对,后来用小车自由行的例程,小车可以正常跑起来,控制也是一样的。现在是在D7脚上跑,小车很活泼。
第一个是之前舵机试验,后面两个是小车自转。
这里说一下例程和PDF的说明不太一致。例程里说,分为前、后、左、右、停止、旋转的,我试了一下,发现我的只能是后退,左转弯,旋转。不过这个难不倒咱,根据第一个例程,可以找到各个键的编码,然后对每个编码想要实现的功能进行修改,小车想怎么转就怎么转。
下面是得到编码的程序,自带的串口工具很方便。
#include
int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;
long unsigned int return_decode()
{
if (irrecv.decode(&results)) {
irrecv.resume(); // 接收下一个值
return results.value;
}
else
{
return 0;
irrecv.resume(); // 接收下一个值
}
}
void setup()
{
Serial.begin(9600);
pinMode(7,INPUT);//设置红外接收引脚为输入
irrecv.enableIRIn(); //初始化红外遥控
}
void loop()
{
Serial.print("message");
Serial.print(" ");
Serial.print( return_decode());
Serial.print(" ");
Serial.println();
delay( 500 );
}
下面是小车控制的,大家刻字机改动,想怎么转,一点点改动,看着小车按自己所想动作很有成就感。大家可自己动手,动静随心。
#include
unsigned long _ABVAR_1_control = 0UL ;
int RECV_PIN = 8;
IRrecv irrecv(RECV_PIN);
decode_results results;
long unsigned int return_decode()
{
if (irrecv.decode(&results)) {
irrecv.resume(); // 接收下一个值
return results.value;
}
else
{
return 0;
irrecv.resume(); // 接收下一个值
}
}
void setup()
{
pinMode(8,INPUT);//设置红外接收引脚为输入
irrecv.enableIRIn(); //初始化红外遥控
pinMode( 3 , OUTPUT );
pinMode( 5 , OUTPUT );
pinMode( 6 , OUTPUT );
pinMode( 9 , OUTPUT );
}
void loop()
{
_ABVAR_1_control = return_decode() ;
if (( ( _ABVAR_1_control ) == ( 16712445 ) ))
{
analogWrite( 3 , 255 );
analogWrite(5,0);
analogWrite( 6 , 0 );
analogWrite(9,255);
delay( 1000 );
analogWrite( 3 , 0 );
analogWrite(5,0);
analogWrite( 6 , 0 );
analogWrite(9,0);
}
if (( ( _ABVAR_1_control ) == ( 16720605 ) ))
{
analogWrite( 3 , 0 );
analogWrite(5,255);
analogWrite( 6 , 255 );
analogWrite(9,0);
delay( 1000 );
analogWrite( 3 , 0 );
analogWrite(5,0);
analogWrite( 6 , 0 );
analogWrite(9,0);
}
if (( ( _ABVAR_1_control ) == ( 16769055 ) ))
{
analogWrite( 3 , 0 );
analogWrite(5,0);
analogWrite( 6 , 0 );
analogWrite(9,255);
delay( 500 );
analogWrite( 3 , 0 );
analogWrite(5,0);
analogWrite( 6 , 0 );
analogWrite(9,0);
}
if (( ( _ABVAR_1_control ) == ( 16754775 ) ))
{
analogWrite( 3 , 255 );
analogWrite(5,0);
analogWrite( 6 , 0 );
analogWrite(9,0);
delay( 500 );
analogWrite( 3 , 0 );
analogWrite(5,0);
analogWrite( 6 , 0 );
analogWrite(9,0);
}
if (( ( _ABVAR_1_control ) == ( 16748655 ) ))
{
analogWrite( 3 , 255 );
analogWrite(5,0);
analogWrite( 6 , 255 );
analogWrite(9,0);
delay( 1000 );
analogWrite( 3 , 0 );
analogWrite(5,0);
analogWrite( 6 , 0 );
analogWrite(9,0);
}
}