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

標題: Arduino智能小車紅外遙控實驗程序 [打印本頁]

作者: kedy2008    時間: 2019-7-3 17:56
標題: Arduino智能小車紅外遙控實驗程序
  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. }
復制代碼

作者: admin    時間: 2019-7-4 23:21
本帖需要重新編輯補全電路原理圖,源碼,詳細說明與圖片即可獲得100+黑幣(帖子下方有編輯按鈕)




歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 一区二区三区国产在线观看 | 日日碰狠狠躁久久躁96avv | 日韩在线播放一区 | 国产在线a | 国产一区二区a | 欧美一级在线观看 | 精品中文字幕在线观看 | 亚洲风情在线观看 | 欧美成人精品一区二区男人看 | 欧美片网站免费 | 综合久久久久 | 99精品欧美一区二区三区综合在线 | 国产欧美精品区一区二区三区 | 久久一视频 | 91文字幕巨乱亚洲香蕉 | 91麻豆精品一区二区三区 | 亚洲欧美日韩国产综合 | 成人亚洲网站 | 国产精品免费观看视频 | 999久久久久久久久6666 | 能免费看的av | 污视频免费在线观看 | 欧美高清一区 | 亚洲啊v在线 | 国产精品久久久久久久久图文区 | 精品国产欧美一区二区三区成人 | 亚洲免费视频在线观看 | 国产超碰人人爽人人做人人爱 | 一区二区三区四区电影视频在线观看 | 成人在线观看免费视频 | 日本视频中文字幕 | 日韩欧美在线视频观看 | 久久尤物免费一区二区三区 | 九九热在线精品视频 | 一级免费毛片 | 欧美亚洲综合久久 | www.久久久久久久久 | 国产精品一区二区三区久久 | 欧美一区二区三区在线观看视频 | 日韩不卡一区二区三区 | 992人人草|