久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2836|回復: 1
打印 上一主題 下一主題
收起左側

Arduino智能小車紅外遙控實驗程序

[復制鏈接]
跳轉到指定樓層
樓主
ID:558099 發表于 2019-7-3 17:56 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  1. #include <IRremote.h>//包含紅外庫
  2. int RECV_PIN = A4;//端口聲明
  3. IRrecv irrecv(RECV_PIN);
  4. decode_results results;//結構聲明
  5. int on = 0;//標志位
  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;     //左電機后退(IN1)
  16. int Left_motor_go=9;     //左電機前進(IN2)

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

  19. void setup()
  20. {
  21.   //初始化電機驅動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()     // 前進
  31. {
  32.   digitalWrite(Right_motor_go,HIGH);  // 右電機前進
  33.   digitalWrite(Right_motor_back,LOW);     
  34.   //analogWrite(Right_motor_go,200);//PWM比例0~255調速,左右輪差異略增減
  35.   //analogWrite(Right_motor_back,0);
  36.   digitalWrite(Left_motor_go,HIGH);  // 左電機前進
  37.   digitalWrite(Left_motor_back,LOW);
  38.   //analogWrite(Left_motor_go,200);//PWM比例0~255調速,左右輪差異略增減
  39.   //analogWrite(Left_motor_back,0);
  40.   //delay(time * 100);   //執行時間,可以調整  
  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);//執行時間,可以調整  
  49. }

  50. void left()         //左轉(左輪不動,右輪前進)
  51. {
  52.   digitalWrite(Right_motor_go,HIGH);        // 右電機前進
  53.   digitalWrite(Right_motor_back,LOW);
  54.   //analogWrite(Right_motor_go,200);
  55.   //analogWrite(Right_motor_back,0);//PWM比例0~255調速
  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調速
  60.   //delay(time * 100);        //執行時間,可以調整  
  61. }

  62. void spin_left()         //左轉(左輪后退,右輪前進)
  63. {
  64.   digitalWrite(Right_motor_go,HIGH);        // 右電機前進
  65.   digitalWrite(Right_motor_back,LOW);
  66.   //analogWrite(Right_motor_go,200);
  67.   //analogWrite(Right_motor_back,0);//PWM比例0~255調速
  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調速
  72.   //delay(time * 100);        //執行時間,可以調整  
  73. }

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

  86. void spin_right()        //右轉(右輪后退,左輪前進)
  87. {
  88.   digitalWrite(Right_motor_go,LOW);   //右電機后退
  89.   digitalWrite(Right_motor_back,HIGH);
  90.   //analogWrite(Right_motor_go,0);
  91.   //analogWrite(Right_motor_back,200);//PWM比例0~255調速
  92.   digitalWrite(Left_motor_go,HIGH);//左電機前進
  93.   digitalWrite(Left_motor_back,LOW);
  94.   //analogWrite(Left_motor_go,200);
  95.   //analogWrite(Left_motor_back,0);//PWM比例0~255調速
  96.   //delay(time * 100);        //執行時間,可以調整  
  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調速
  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調速
  108.   //delay(time * 100);     //執行時間,可以調整  
  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. //串口打印,調試時可以打開,實際運行中會影響反應速度,建議屏蔽
  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)) //調用庫函數:解碼
  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;//標志位置反
  170.       digitalWrite(13, on ? HIGH : LOW);//板子上接收到信號閃爍一下led
  171.       dump(&results);//解碼紅外信號
  172.     }
  173.     if (results.value == run_car )//按鍵2
  174.       run();//前進
  175.     if (results.value == back_car )//按鍵8
  176.       back();//后退
  177.     if (results.value == left_car )//按鍵4
  178.       left();//左轉
  179.     if (results.value == right_car )//按鍵6
  180.       right();//右轉
  181.     if (results.value == stop_car )//按鍵5
  182.       brake();//停車
  183.     if (results.value == left_turn )//按鍵1
  184.       spin_left();//左旋轉
  185.     if (results.value == right_turn )//按鍵3
  186.       spin_right();//右旋轉
  187.     last = millis();      
  188.     irrecv.resume(); // Receive the next value
  189.   }
  190. }
復制代碼
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:1 發表于 2019-7-4 23:21 | 只看該作者
本帖需要重新編輯補全電路原理圖,源碼,詳細說明與圖片即可獲得100+黑幣(帖子下方有編輯按鈕)
回復

使用道具 舉報

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

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲一区中文字幕 | 亚洲综合在线一区二区 | 国产免费一区 | 国产成人精品久久二区二区91 | 国精日本亚洲欧州国产中文久久 | 久久一本| 日韩欧美在线免费观看视频 | 精品一区二区三区四区 | 91久久精品日日躁夜夜躁国产 | 国产在线观看不卡一区二区三区 | 精品国产91 | 亚洲伊人精品酒店 | 久久国产精品免费一区二区三区 | 成人免费视频 | 欧美精品二区 | 精品一区av | 国产精品欧美精品日韩精品 | 久久久久久一区 | 欧洲亚洲精品久久久久 | 手机看黄av免费网址 | 国精品一区二区 | 国产黄色精品在线观看 | 一级大片免费 | 91精品国产高清一区二区三区 | 久久中文字幕视频 | 亚洲精品一二三区 | 久久精品视频在线观看 | 国产一区二区不卡 | 午夜国产一区 | 精品国产视频 | 成人不卡 | 久久久久久久久久久丰满 | 国产黄色一级片 | 国产剧情一区 | 亚洲第1页 | 亚洲成人精品一区二区 | 国产欧美一区二区三区日本久久久 | 亚洲一区二区三区在线 | 九一视频在线观看 | 国产精品日韩一区 | 国产91在线精品 |