找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2861|回復(fù): 1
收起左側(cè)

Arduino智能小車紅外遙控實(shí)驗(yàn)程序

[復(fù)制鏈接]
ID:558099 發(fā)表于 2019-7-3 17:56 | 顯示全部樓層 |閱讀模式
  1. #include <IRremote.h>//包含紅外庫
  2. int RECV_PIN = A4;//端口聲明
  3. IRrecv irrecv(RECV_PIN);
  4. decode_results results;//結(jié)構(gòu)聲明
  5. int on = 0;//標(biāo)志位
  6. unsigned long last = millis();

  7. long run_car = 0x00FF18E7;//按鍵2
  8. long back_car = 0x00FF4AB5;//按鍵8
  9. long left_car = 0x00FF10EF;//按鍵4
  10. long right_car = 0x00FF5AA5;//按鍵6
  11. long stop_car = 0x00FF38C7;//按鍵5
  12. long left_turn = 0x00ff30CF;//按鍵1
  13. long right_turn = 0x00FF7A85;//按鍵3
  14. //==============================
  15. int Left_motor_back=8;     //左電機(jī)后退(IN1)
  16. int Left_motor_go=9;     //左電機(jī)前進(jìn)(IN2)

  17. int Right_motor_go=10;    // 右電機(jī)前進(jìn)(IN3)
  18. int Right_motor_back=11;    // 右電機(jī)后退(IN4)

  19. void setup()
  20. {
  21.   //初始化電機(jī)驅(qū)動IO為輸出方式
  22.   pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM)
  23.   pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM)
  24.   pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM)
  25.   pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM)
  26.   pinMode(13, OUTPUT);////端口模式,輸出
  27.   Serial.begin(9600);        //波特率9600
  28.   irrecv.enableIRIn(); // Start the receiver
  29. }
  30. void run()     // 前進(jìn)
  31. {
  32.   digitalWrite(Right_motor_go,HIGH);  // 右電機(jī)前進(jìn)
  33.   digitalWrite(Right_motor_back,LOW);     
  34.   //analogWrite(Right_motor_go,200);//PWM比例0~255調(diào)速,左右輪差異略增減
  35.   //analogWrite(Right_motor_back,0);
  36.   digitalWrite(Left_motor_go,HIGH);  // 左電機(jī)前進(jìn)
  37.   digitalWrite(Left_motor_back,LOW);
  38.   //analogWrite(Left_motor_go,200);//PWM比例0~255調(diào)速,左右輪差異略增減
  39.   //analogWrite(Left_motor_back,0);
  40.   //delay(time * 100);   //執(zhí)行時(shí)間,可以調(diào)整  
  41. }

  42. void brake()         //剎車,停車
  43. {
  44.   digitalWrite(Right_motor_go,LOW);
  45.   digitalWrite(Right_motor_back,LOW);
  46.   digitalWrite(Left_motor_go,LOW);
  47.   digitalWrite(Left_motor_back,LOW);
  48.   //delay(time * 100);//執(zhí)行時(shí)間,可以調(diào)整  
  49. }

  50. void left()         //左轉(zhuǎn)(左輪不動,右輪前進(jìn))
  51. {
  52.   digitalWrite(Right_motor_go,HIGH);        // 右電機(jī)前進(jìn)
  53.   digitalWrite(Right_motor_back,LOW);
  54.   //analogWrite(Right_motor_go,200);
  55.   //analogWrite(Right_motor_back,0);//PWM比例0~255調(diào)速
  56.   digitalWrite(Left_motor_go,LOW);   //左輪不動
  57.   digitalWrite(Left_motor_back,LOW);
  58.   //analogWrite(Left_motor_go,0);
  59.   //analogWrite(Left_motor_back,0);//PWM比例0~255調(diào)速
  60.   //delay(time * 100);        //執(zhí)行時(shí)間,可以調(diào)整  
  61. }

  62. void spin_left()         //左轉(zhuǎn)(左輪后退,右輪前進(jìn))
  63. {
  64.   digitalWrite(Right_motor_go,HIGH);        // 右電機(jī)前進(jìn)
  65.   digitalWrite(Right_motor_back,LOW);
  66.   //analogWrite(Right_motor_go,200);
  67.   //analogWrite(Right_motor_back,0);//PWM比例0~255調(diào)速
  68.   digitalWrite(Left_motor_go,LOW);   //左輪后退
  69.   digitalWrite(Left_motor_back,HIGH);
  70.   //analogWrite(Left_motor_go,0);
  71.   //analogWrite(Left_motor_back,200);//PWM比例0~255調(diào)速
  72.   //delay(time * 100);        //執(zhí)行時(shí)間,可以調(diào)整  
  73. }

  74. void right()        //右轉(zhuǎn)(右輪不動,左輪前進(jìn))
  75. {
  76.   digitalWrite(Right_motor_go,LOW);   //右電機(jī)不動
  77.   digitalWrite(Right_motor_back,LOW);
  78.   //analogWrite(Right_motor_go,0);
  79.   //analogWrite(Right_motor_back,0);//PWM比例0~255調(diào)速
  80.   digitalWrite(Left_motor_go,HIGH);//左電機(jī)前進(jìn)
  81.   digitalWrite(Left_motor_back,LOW);
  82.   //analogWrite(Left_motor_go,200);
  83.   //analogWrite(Left_motor_back,0);//PWM比例0~255調(diào)速
  84.   //delay(time * 100);        //執(zhí)行時(shí)間,可以調(diào)整  
  85. }

  86. void spin_right()        //右轉(zhuǎn)(右輪后退,左輪前進(jìn))
  87. {
  88.   digitalWrite(Right_motor_go,LOW);   //右電機(jī)后退
  89.   digitalWrite(Right_motor_back,HIGH);
  90.   //analogWrite(Right_motor_go,0);
  91.   //analogWrite(Right_motor_back,200);//PWM比例0~255調(diào)速
  92.   digitalWrite(Left_motor_go,HIGH);//左電機(jī)前進(jìn)
  93.   digitalWrite(Left_motor_back,LOW);
  94.   //analogWrite(Left_motor_go,200);
  95.   //analogWrite(Left_motor_back,0);//PWM比例0~255調(diào)速
  96.   //delay(time * 100);        //執(zhí)行時(shí)間,可以調(diào)整  
  97. }

  98. void back()          //后退
  99. {
  100.   digitalWrite(Right_motor_go,LOW);  //右輪后退
  101.   digitalWrite(Right_motor_back,HIGH);
  102.   //analogWrite(Right_motor_go,0);
  103.   //analogWrite(Right_motor_back,150);//PWM比例0~255調(diào)速
  104.   digitalWrite(Left_motor_go,LOW);  //左輪后退
  105.   digitalWrite(Left_motor_back,HIGH);
  106.   //analogWrite(Left_motor_go,0);
  107.   //analogWrite(Left_motor_back,150);//PWM比例0~255調(diào)速
  108.   //delay(time * 100);     //執(zhí)行時(shí)間,可以調(diào)整  
  109. }

  110. void dump(decode_results *results)
  111. {
  112.   int count = results->rawlen;
  113.   if (results->decode_type == UNKNOWN)
  114.   {
  115.     //Serial.println("Could not decode message");
  116.     brake();
  117.   }
  118. //串口打印,調(diào)試時(shí)可以打開,實(shí)際運(yùn)行中會影響反應(yīng)速度,建議屏蔽
  119. /*
  120.   else
  121.   {

  122.     if (results->decode_type == NEC)
  123.     {
  124.       Serial.print("Decoded NEC: ");
  125.     }
  126.     else if (results->decode_type == SONY)
  127.     {
  128.       Serial.print("Decoded SONY: ");
  129.     }
  130.     else if (results->decode_type == RC5)
  131.     {
  132.       Serial.print("Decoded RC5: ");
  133.     }
  134.     else if (results->decode_type == RC6)
  135.     {
  136.       Serial.print("Decoded RC6: ");
  137.     }
  138.     Serial.print(results->value, HEX);
  139.     Serial.print(" (");
  140.     Serial.print(results->bits, DEC);
  141.     Serial.println(" bits)");

  142.   }
  143.   Serial.print("Raw (");
  144.   Serial.print(count, DEC);
  145.   Serial.print("): ");

  146.   for (int i = 0; i < count; i++)
  147.   {
  148.     if ((i % 2) == 1)
  149.     {
  150.       Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
  151.     }
  152.     else  
  153.     {
  154.       Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
  155.     }
  156.     Serial.print(" ");
  157.   }
  158.   Serial.println("");
  159. */
  160. }

  161. void loop()
  162. {
  163.   if (irrecv.decode(&results)) //調(diào)用庫函數(shù):解碼
  164.   {
  165.     // If it's been at least 1/4 second since the last
  166.     // IR received, toggle the relay
  167.     if (millis() - last > 250) //確定接收到信號
  168.     {
  169.       on = !on;//標(biāo)志位置反
  170.       digitalWrite(13, on ? HIGH : LOW);//板子上接收到信號閃爍一下led
  171.       dump(&results);//解碼紅外信號
  172.     }
  173.     if (results.value == run_car )//按鍵2
  174.       run();//前進(jìn)
  175.     if (results.value == back_car )//按鍵8
  176.       back();//后退
  177.     if (results.value == left_car )//按鍵4
  178.       left();//左轉(zhuǎn)
  179.     if (results.value == right_car )//按鍵6
  180.       right();//右轉(zhuǎn)
  181.     if (results.value == stop_car )//按鍵5
  182.       brake();//停車
  183.     if (results.value == left_turn )//按鍵1
  184.       spin_left();//左旋轉(zhuǎn)
  185.     if (results.value == right_turn )//按鍵3
  186.       spin_right();//右旋轉(zhuǎn)
  187.     last = millis();      
  188.     irrecv.resume(); // Receive the next value
  189.   }
  190. }
復(fù)制代碼
回復(fù)

使用道具 舉報(bào)

ID:1 發(fā)表于 2019-7-4 23:21 | 顯示全部樓層
本帖需要重新編輯補(bǔ)全電路原理圖,源碼,詳細(xì)說明與圖片即可獲得100+黑幣(帖子下方有編輯按鈕)
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表