1. 程式人生 > >Arduino智慧小車--只是隨便一搞

Arduino智慧小車--只是隨便一搞

在某寶寶買的智慧小車,挺精緻的,開心的連線上印表機的線,結果埠都沒有反應,

查了一下發現是少了驅動,博主用的mac os10.12.3

安裝好之後我們再啟動arduino 就可以看到新的埠了,選擇他,我們就可以進行上傳程式碼了。

只是對附贈原始碼進行了一下整理。。。

連結: https://pan.baidu.com/s/1dFwzx53 密碼: 2aju

#include <IRremote.h>//包含紅外庫  關鍵點
int RECV_PIN = A4;//埠宣告
IRrecv irrecv(RECV_PIN);
decode_results results;//結構宣告
int on = 0;//標誌位
unsigned long last = millis();

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
//避障開關
long avoidopen = 0x00FF42BD;//按鍵7
long trackingopen = 0x00FF52AD;//按鍵9
//==============================
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 key=A2;//定義按鍵 數字A2 介面
int beep=A3;//定義蜂鳴器 數字A3 介面
int LED=7;//定義LED 數字7 介面
const int SensorRight_2 = 5;     //中間紅外避障感測器(P3.4 OUT3)
int SR_2;    //右紅外感測器狀態

//循跡
const int SensorRight = 3;     //右循跡紅外感測器(P3.2 OUT1)
const int SensorLeft = 4;       //左循跡紅外感測器(P3.3 OUT2)
int SL;    //左循跡紅外感測器狀態
int SR;    //右循跡紅外感測器狀態

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);////埠模式,輸出
  Serial.begin(9600);	//波特率9600
  irrecv.enableIRIn(); // Start the receiver
  //避障
  pinMode(key,INPUT);//定義按鍵介面為輸入介面
  pinMode(beep,OUTPUT);//定義蜂鳴器為輸出介面
  pinMode(LED,OUTPUT);//定義LED為輸出介面

  pinMode(SensorRight, INPUT); //定義右循跡紅外感測器為輸入
  pinMode(SensorLeft, INPUT); //定義左循跡紅外感測器為輸入
}
void run()     // 前進
{
   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 brake(int time)  //剎車,停車
{
  digitalWrite(Right_motor_go,LOW);
  digitalWrite(Right_motor_back,LOW);
  digitalWrite(Left_motor_go,LOW);
  digitalWrite(Left_motor_back,LOW);
  delay(time * 100);//執行時間,可以調整  
}
void left(int time)         //左轉(左輪不動,右輪前進)
{
  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(int time)         //左轉(左輪後退,右輪前進)
{
  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 spin_right(int time)        //右轉(右輪後退,左輪前進)
{
  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(int time)          //後退
{
  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 brake()         //剎車,停車
{
  digitalWrite(Right_motor_go,LOW);
  digitalWrite(Right_motor_back,LOW);
  digitalWrite(Left_motor_go,LOW);
  digitalWrite(Left_motor_back,LOW);
  //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 keysacn()//按鍵掃描
{
  int val;
  val=digitalRead(key);//讀取數字7 口電平值賦給val
  while(!digitalRead(key))//當按鍵沒被按下時,一直迴圈
  {
    val=digitalRead(key);//此句可省略,可讓迴圈跑空
  }
  while(digitalRead(key))//當按鍵被按下時
  {
    delay(10);  //延時10ms
    val=digitalRead(key);//讀取數字7 口電平值賦給val
    if(val==HIGH)  //第二次判斷按鍵是否被按下
    {
      digitalWrite(beep,HIGH);    //蜂鳴器響
      while(!digitalRead(key))  //判斷按鍵是否被鬆開
        digitalWrite(beep,LOW);   //蜂鳴器停止
    }
    else
      digitalWrite(beep,LOW);//蜂鳴器停止
  }
}
//避障程式
void avoid()
{
  //修改為遙控器控制
  //keysacn();     //呼叫按鍵掃描函式
  while(1)
  {
    //有訊號為LOW  沒有訊號為HIGH  有障礙物輸出0  沒有障礙物輸出1
    SR_2 = digitalRead(SensorRight_2);
    if (SR_2==HIGH)//前面沒有障礙物
    {
      run();   //呼叫前進函式
      digitalWrite(beep,LOW);   //蜂鳴器不響
      digitalWrite(LED,LOW);    //LED不亮
    } 
    else if ( SR_2 == LOW)// 前面探測到有障礙物,有訊號返回 
       { 
         digitalWrite(beep,HIGH);   //蜂鳴器響
         digitalWrite(LED,HIGH);    //LED亮
         brake(3);//停止300MS
         back(4);//後退400MS
         left(5);//呼叫左轉函式  延時500ms       
     }
     
  }

}
void tracking(){
  while(1)
  {
    //有訊號為LOW  沒有訊號為HIGH   檢測到黑線  輸出高  檢測到白色區域輸出低
    SR = digitalRead(SensorRight);//有訊號表明在白色區域,車子底板上L1亮;沒訊號表明壓在黑線上,車子底板上L1滅
    SL = digitalRead(SensorLeft);//有訊號表明在白色區域,車子底板上L2亮;沒訊號表明壓在黑線上,車子底板上L2滅
    if (SL == LOW&&SR==LOW)
      run();   //呼叫前進函式
    else if (SL == HIGH & SR == LOW)// 左循跡紅外感測器,檢測到訊號,車子向右偏離軌道,向左轉 
      left();
    else if (SR == HIGH & SL == LOW) // 右循跡紅外感測器,檢測到訊號,車子向左偏離軌道,向右轉  
      right();
    else // 都是黑色, 停止
    brake();
    }
}
void loop()
{
  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);//解碼紅外訊號
    }
    if (results.value == run_car )//按鍵CH
      run();//前進
    if (results.value == back_car )//按鍵+
      back();//後退
    if (results.value == left_car )//按鍵<<
      left();//左轉
    if (results.value == right_car )//按鍵>||
      right();//右轉
    if (results.value == stop_car )//按鍵>>|
      brake();//停車
    if (results.value == left_turn )//按鍵-
      spin_left();//左旋轉
    if (results.value == right_turn )//按鍵EQ
      spin_right();//右旋轉
    if (results.value == avoidopen)//按鍵7
      avoid();// 開啟智慧避障 
    if (results.value == trackingopen)//按鍵9
      tracking();
      
    last = millis();      
    irrecv.resume(); // Receive the next value
  }
}