拿廠商給的程式改的
超音波偵測感測器為 HC-SR04
//------------------------------------程式碼開始---------------------------------------------
#include
int RECV_PIN = A4;//宣告紅外線 PIN腳
IRrecv irrecv(RECV_PIN); //創建一個紅外線
decode_results results;//這個用來解析你的紅外線是哪一牌
int on = 0;//标志位
unsigned long last = millis();
//底下為超音波偵測器的設定,超音波裝置有主要二個PIN腳,echo 和trig
int echo=7;
int trig=12;
int distance=0; //用來記錄超音波裝置偵測到的距離
int dangerDistance=30; //自訂危險距離,我設30, 也就是偵測到小於三十公分,自走車強制停止前進,避免撞牆
//-------------------
long run_car = 0x00FF629D;//按键CH
long back_car = 0x00FFA857;//按键+
long left_car = 0x00FF22DD;//按键<<
long right_car = 0x00FFC23D;//按键>||
long stop_car = 0x00FF02FD;//按键>>|
long left_turn = 0x00ffE01F;//按键-
long right_turn = 0x00FF906F;//按键EQ
//==============================
int Left_motor_go=8; //左馬達前進(IN1)
int Left_motor_back=9; //左馬達後退(IN2)
int Right_motor_go=10; // 右馬達前進(IN3)
int Right_motor_back=11; // 右馬達後退(IN4)
int runCount=0;
void setup()
{
//初始化馬達的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);////端口模式,输出
pinMode(trig, OUTPUT);
pinMode(echo,INPUT);
Serial.begin(9600); //波特率9600
irrecv.enableIRIn(); // Start the receiver
}
void Distance_test(){
//底下為超音波測距離的標準寫法
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10); //最小為10,小於此值,偵測易失敗
digitalWrite(trig, LOW);
float Fdistance = pulseIn(echo, HIGH); // 讀取返回時間(單位:微秒)
Fdistance= Fdistance/58; //除以58等於公分, Y米=(X秒*344)/2
// X秒=( 2*Y米)/344 ==》X秒=0.0058*Y米 ==》公分=微秒/58
distance = Fdistance;
}
void Distance_display(){
Serial.print("Distance is: ");
Serial.println(distance);
}
void brake() //停車
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
runCount=0;
//delay(time * 100);//執行時間,可以调整
}
void run() // 前進
{
if(runCount ==0){
analogWrite(Right_motor_go,250);
runCount=1;
}else{
digitalWrite(Right_motor_go,HIGH);
}
// digitalWrite(Right_motor_go,HIGH); // 右馬達前進
digitalWrite(Right_motor_back,LOW);
//analogWrite(Right_motor_go,200);//PWM比例0~255调速,如果左右輪不平均(例如無法走直線),可以改用此函數,調整值即可
//analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW); // 左馬達前進
digitalWrite(Left_motor_back,HIGH);
//analogWrite(Left_motor_go,0);//PWM比例0~255调速,
//analogWrite(Left_motor_back,200);
//delay(time * 100); //執行時間,可以调整
}
void left() //左轉(左輪不動,右輪前進)
{
digitalWrite(Right_motor_go,HIGH); // 右馬達前進
digitalWrite(Right_motor_back,LOW);
//analogWrite(Right_motor_go,200);
//analogWrite(Right_motor_back,0);//PWM比例0~255调速
digitalWrite(Left_motor_go,LOW); //左輪後退
digitalWrite(Left_motor_back,LOW);
//analogWrite(Left_motor_go,0);
//analogWrite(Left_motor_back,0);//PWM比例0~255调速
//delay(time * 100); //執行時間,可以调整
}
void spin_left() //左轉(左輪後退,右輪前進)
{
digitalWrite(Right_motor_go,HIGH); // 右馬達前進
digitalWrite(Right_motor_back,LOW);
//analogWrite(Right_motor_go,200);
//analogWrite(Right_motor_back,0);//PWM比例0~255调速
digitalWrite(Left_motor_go,HIGH); //左輪後退
digitalWrite(Left_motor_back,LOW);
//analogWrite(Left_motor_go,200);
//analogWrite(Left_motor_back,0);//PWM比例0~255调速
//delay(time * 100); //執行時間,可以调整
}
void right() //右轉(右輪不动,左輪前進)
{
digitalWrite(Right_motor_go,LOW); //右电机後退
digitalWrite(Right_motor_back,LOW);
//analogWrite(Right_motor_go,0);
//analogWrite(Right_motor_back,0);//PWM比例0~255调速
digitalWrite(Left_motor_go,LOW);//左馬達前進
digitalWrite(Left_motor_back,HIGH);
//analogWrite(Left_motor_go,0);
// analogWrite(Left_motor_back,200);//PWM比例0~255调速
// delay(time * 100); //執行時間,可以调整
}
void spin_right() //右轉(右輪後退,左輪前進)
{
digitalWrite(Right_motor_go,LOW); //右电机後退
digitalWrite(Right_motor_back,HIGH);
//analogWrite(Right_motor_go,0);
//analogWrite(Right_motor_back,200);//PWM比例0~255调速
digitalWrite(Left_motor_go,LOW);//左馬達前進
digitalWrite(Left_motor_back,HIGH);
//analogWrite(Left_motor_go,0);
//analogWrite(Left_motor_back,200);//PWM比例0~255调速
//delay(time * 100); //執行時間,可以调整
}
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调速
digitalWrite(Left_motor_go,HIGH); //左輪後退
digitalWrite(Left_motor_back,LOW);
//analogWrite(Left_motor_go,150);
//analogWrite(Left_motor_back,0);//PWM比例0~255调速
//delay(time * 100); //執行時間,可以调整
}
void dump(decode_results *results)
{
int count = results->rawlen;
if (results->decode_type == UNKNOWN)
{
//Serial.println("Could not decode message");
brake();
}
//輸出你使用的遙控器的編碼,但此段程式會影響程式執行速度,除了測試之外建議註解掉
/*
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()
{
Distance_test();
delay(100);
if(distance < dangerDistance){
brake();
}
//Distance_display();
if (irrecv.decode(&results)) //解碼
{
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) //確定接收到訊號
{
on = !on;//標記位置反
digitalWrite(13, on ? HIGH : LOW);//收到訊號後閃一下LED
// dump(&results);//解碼紅外線訊號,看是哪一家(sony,NEC...)
}
if (results.value == run_car )//按键CH
run();//前進
if (results.value == back_car )//按键+
back();//後退
if (results.value == left_car ){//按键<<
left();//左轉
delay(300);
brake();
}
if (results.value == right_car ){//按键>||
right();//右轉
delay(300);
brake();
}
if (results.value == stop_car )//按键>>|
brake();//停車
if (results.value == left_turn )//按键-
spin_left();//左旋轉
if (results.value == right_turn )//按键EQ
spin_right();//右旋轉
last = millis();
irrecv.resume(); // Receive the next value
}
}