最近想做红外小车,遥控器是用电视遥控器,遥控是可以了,但是有一个问题出现了:按前进时,车子不是按一下就前进一下,而是按一下就一直前进了,只有按停止它才会停止,我的目的是想按着键是车子就走,发开键是车子就停,程序已基本完成,求高手帮忙指点下程序呀哦怎么修改!谢谢啦!!!!!
程序如下:
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void stop();
void forward();
void back();
void left();
void right();
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
pinMode( 4 , OUTPUT);
pinMode( 5 , OUTPUT);
pinMode( 7 , OUTPUT);
pinMode( 6 , OUTPUT);
pinMode( 9 , OUTPUT);
pinMode( 8 , OUTPUT);
}
void back()
{
digitalWrite( 4 , HIGH );
digitalWrite( 5 , HIGH );
digitalWrite( 7 , HIGH );
digitalWrite( 6 , LOW );
digitalWrite( 9 , HIGH );
digitalWrite( 8 , LOW );
}
void left()
{
digitalWrite( 4 , HIGH );
digitalWrite( 5 , HIGH );
digitalWrite( 7 , HIGH );
digitalWrite( 6 , LOW );
digitalWrite( 9 , LOW );
digitalWrite( 8 , HIGH );
}
void right()
{
digitalWrite( 4 , HIGH );
digitalWrite( 5 , HIGH );
digitalWrite( 7 , LOW );
digitalWrite( 6 ,HIGH );
digitalWrite( 9 , HIGH );
digitalWrite( 8 , LOW );
}
void forward()
{
digitalWrite( 5 , HIGH );
digitalWrite( 4 , HIGH );
digitalWrite( 8 , HIGH );
digitalWrite( 9 , LOW );
digitalWrite( 7 , LOW);
digitalWrite( 6 , HIGH );
}
void stop()
{
digitalWrite( 4 , LOW );
digitalWrite( 5 , LOW );
digitalWrite( 6 , LOW );
digitalWrite( 7 , LOW );
digitalWrite( 8 , LOW );
digitalWrite( 9 , LOW );
}
void loop()
{
if (irrecv.decode(&results)){
Serial.println( results.value,HEX);
if( results.value ==0x23B47CEC) {
forward();
irrecv.resume();
}
else {
stop();
if(results.value ==0x165CC0E2){
left();
irrecv.resume();
}
else{
stop();
if(results.value ==0xAC7DE7B4){
right();
irrecv.resume();
}
else {
stop();
if(results.value ==0x81086BA0){
back();
irrecv.resume();
}
else{
stop();
irrecv.resume();
}}}}}}
|