標(biāo)題:
Arduino智能小車(chē)紅外遙控實(shí)驗(yàn)程序
[打印本頁(yè)]
作者:
kedy2008
時(shí)間:
2019-7-3 17:56
標(biāo)題:
Arduino智能小車(chē)紅外遙控實(shí)驗(yàn)程序
#include <IRremote.h>//包含紅外庫(kù)
int RECV_PIN = A4;//端口聲明
IRrecv irrecv(RECV_PIN);
decode_results results;//結(jié)構(gòu)聲明
int on = 0;//標(biāo)志位
unsigned long last = millis();
long run_car = 0x00FF18E7;//按鍵2
long back_car = 0x00FF4AB5;//按鍵8
long left_car = 0x00FF10EF;//按鍵4
long right_car = 0x00FF5AA5;//按鍵6
long stop_car = 0x00FF38C7;//按鍵5
long left_turn = 0x00ff30CF;//按鍵1
long right_turn = 0x00FF7A85;//按鍵3
//==============================
int Left_motor_back=8; //左電機(jī)后退(IN1)
int Left_motor_go=9; //左電機(jī)前進(jìn)(IN2)
int Right_motor_go=10; // 右電機(jī)前進(jìn)(IN3)
int Right_motor_back=11; // 右電機(jī)后退(IN4)
void setup()
{
//初始化電機(jī)驅(qū)動(dòng)IO為輸出方式
pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM)
pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM)
pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM)
pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM)
pinMode(13, OUTPUT);////端口模式,輸出
Serial.begin(9600); //波特率9600
irrecv.enableIRIn(); // Start the receiver
}
void run() // 前進(jìn)
{
digitalWrite(Right_motor_go,HIGH); // 右電機(jī)前進(jìn)
digitalWrite(Right_motor_back,LOW);
//analogWrite(Right_motor_go,200);//PWM比例0~255調(diào)速,左右輪差異略增減
//analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH); // 左電機(jī)前進(jìn)
digitalWrite(Left_motor_back,LOW);
//analogWrite(Left_motor_go,200);//PWM比例0~255調(diào)速,左右輪差異略增減
//analogWrite(Left_motor_back,0);
//delay(time * 100); //執(zhí)行時(shí)間,可以調(diào)整
}
void brake() //剎車(chē),停車(chē)
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
//delay(time * 100);//執(zhí)行時(shí)間,可以調(diào)整
}
void left() //左轉(zhuǎn)(左輪不動(dòng),右輪前進(jìn))
{
digitalWrite(Right_motor_go,HIGH); // 右電機(jī)前進(jìn)
digitalWrite(Right_motor_back,LOW);
//analogWrite(Right_motor_go,200);
//analogWrite(Right_motor_back,0);//PWM比例0~255調(diào)速
digitalWrite(Left_motor_go,LOW); //左輪不動(dòng)
digitalWrite(Left_motor_back,LOW);
//analogWrite(Left_motor_go,0);
//analogWrite(Left_motor_back,0);//PWM比例0~255調(diào)速
//delay(time * 100); //執(zhí)行時(shí)間,可以調(diào)整
}
void spin_left() //左轉(zhuǎn)(左輪后退,右輪前進(jìn))
{
digitalWrite(Right_motor_go,HIGH); // 右電機(jī)前進(jìn)
digitalWrite(Right_motor_back,LOW);
//analogWrite(Right_motor_go,200);
//analogWrite(Right_motor_back,0);//PWM比例0~255調(diào)速
digitalWrite(Left_motor_go,LOW); //左輪后退
digitalWrite(Left_motor_back,HIGH);
//analogWrite(Left_motor_go,0);
//analogWrite(Left_motor_back,200);//PWM比例0~255調(diào)速
//delay(time * 100); //執(zhí)行時(shí)間,可以調(diào)整
}
void right() //右轉(zhuǎn)(右輪不動(dòng),左輪前進(jìn))
{
digitalWrite(Right_motor_go,LOW); //右電機(jī)不動(dòng)
digitalWrite(Right_motor_back,LOW);
//analogWrite(Right_motor_go,0);
//analogWrite(Right_motor_back,0);//PWM比例0~255調(diào)速
digitalWrite(Left_motor_go,HIGH);//左電機(jī)前進(jìn)
digitalWrite(Left_motor_back,LOW);
//analogWrite(Left_motor_go,200);
//analogWrite(Left_motor_back,0);//PWM比例0~255調(diào)速
//delay(time * 100); //執(zhí)行時(shí)間,可以調(diào)整
}
void spin_right() //右轉(zhuǎn)(右輪后退,左輪前進(jìn))
{
digitalWrite(Right_motor_go,LOW); //右電機(jī)后退
digitalWrite(Right_motor_back,HIGH);
//analogWrite(Right_motor_go,0);
//analogWrite(Right_motor_back,200);//PWM比例0~255調(diào)速
digitalWrite(Left_motor_go,HIGH);//左電機(jī)前進(jìn)
digitalWrite(Left_motor_back,LOW);
//analogWrite(Left_motor_go,200);
//analogWrite(Left_motor_back,0);//PWM比例0~255調(diào)速
//delay(time * 100); //執(zhí)行時(shí)間,可以調(diào)整
}
void back() //后退
{
digitalWrite(Right_motor_go,LOW); //右輪后退
digitalWrite(Right_motor_back,HIGH);
//analogWrite(Right_motor_go,0);
//analogWrite(Right_motor_back,150);//PWM比例0~255調(diào)速
digitalWrite(Left_motor_go,LOW); //左輪后退
digitalWrite(Left_motor_back,HIGH);
//analogWrite(Left_motor_go,0);
//analogWrite(Left_motor_back,150);//PWM比例0~255調(diào)速
//delay(time * 100); //執(zhí)行時(shí)間,可以調(diào)整
}
void dump(decode_results *results)
{
int count = results->rawlen;
if (results->decode_type == UNKNOWN)
{
//Serial.println("Could not decode message");
brake();
}
//串口打印,調(diào)試時(shí)可以打開(kāi),實(shí)際運(yùn)行中會(huì)影響反應(yīng)速度,建議屏蔽
/*
else
{
if (results->decode_type == NEC)
{
Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY)
{
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5)
{
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6)
{
Serial.print("Decoded RC6: ");
}
Serial.print(results->value, HEX);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
}
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");
for (int i = 0; i < count; i++)
{
if ((i % 2) == 1)
{
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
}
else
{
Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
}
Serial.print(" ");
}
Serial.println("");
*/
}
void loop()
{
if (irrecv.decode(&results)) //調(diào)用庫(kù)函數(shù):解碼
{
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) //確定接收到信號(hào)
{
on = !on;//標(biāo)志位置反
digitalWrite(13, on ? HIGH : LOW);//板子上接收到信號(hào)閃爍一下led
dump(&results);//解碼紅外信號(hào)
}
if (results.value == run_car )//按鍵2
run();//前進(jìn)
if (results.value == back_car )//按鍵8
back();//后退
if (results.value == left_car )//按鍵4
left();//左轉(zhuǎn)
if (results.value == right_car )//按鍵6
right();//右轉(zhuǎn)
if (results.value == stop_car )//按鍵5
brake();//停車(chē)
if (results.value == left_turn )//按鍵1
spin_left();//左旋轉(zhuǎn)
if (results.value == right_turn )//按鍵3
spin_right();//右旋轉(zhuǎn)
last = millis();
irrecv.resume(); // Receive the next value
}
}
復(fù)制代碼
作者:
admin
時(shí)間:
2019-7-4 23:21
本帖需要重新編輯補(bǔ)全電路原理圖,源碼,詳細(xì)說(shuō)明與圖片即可獲得100+黑幣(帖子下方有編輯按鈕)
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1