极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10614|回复: 2

求救大牛们!!!把红外改蓝牙如何改???

[复制链接]
发表于 2014-1-25 11:01:38 | 显示全部楼层 |阅读模式
我想把 这个程序里的红外控制部分 全改成 用蓝牙控制 不知道如何改程序代码?哪位路过的大牛 帮帮忙 看看 谢谢!!!

这个程序也是借用某位大牛的 谢谢了  现在 想把红外控制部分改成蓝牙控制,就是模式切换 控制不知道如何做?
  1. #include <IRremote.h>
  2. #include <Servo.h>
  3. //***********************定義馬達腳位*************************
  4. int MotorRight1=5;
  5. int MotorRight2=6;
  6. int MotorLeft1=10;
  7. int MotorLeft2=11;
  8. int counter=0;
  9. const int irReceiverPin = 2; //紅外線接收器 OUTPUT 訊號接在 pin 2
  10. //***********************設定所偵測到的 IRcode*************************
  11. long IRfront= 0x00FF629D; //前進碼
  12. long IRback=0x00FFA857; //後退
  13. long IRturnright=0x00FF22DD; //右轉
  14. long IRturnleft= 0x00FFC23D; //左轉
  15. long IRstop=0x00FF02FD; //停止
  16. long IRcny70=0x00FF6897; //CNY70 自走模式
  17. long IRAutorun=0x00FF9867; //超音波自走模式
  18. long IRturnsmallleft= 0x00FFB04F;
  19. //*************************定義 CNY70 腳位************************************
  20. const int SensorLeft = 7; //左感測器輸入腳
  21. const int SensorMiddle= 4 ; //中感測器輸入腳
  22. const int SensorRight = 3; //右感測器輸入腳
  23. int SL; //左感測器狀態
  24. int SM; //中感測器狀態
  25. int SR; //右感測器狀態
  26. IRrecv irrecv(irReceiverPin); // 定義 IRrecv 物件來接收紅外線訊號
  27. decode_results results; // 解碼結果將放在 decode_results 結構的 result 變數裏
  28. //*************************定義超音波腳位******************************
  29. int inputPin =13 ; // 定義超音波信號接收腳位 rx
  30. int outputPin =12; // 定義超音波信號發射腳位'tx
  31. int Fspeedd = 0; // 前方距離
  32. int Rspeedd = 0; // 右方距離
  33. int Lspeedd = 0; // 左方距離
  34. int directionn = 0; // 前=8 後=2 左=4 右=6
  35. Servo myservo; // 設 myservo
  36. int delay_time = 250; // 伺服馬達轉向後的穩定時間
  37. int Fgo = 8; // 前進
  38. int Rgo = 6; // 右轉
  39. int Lgo = 4; // 左轉
  40. int Bgo = 2; // 倒車
  41. //********************************************************************(SETUP)
  42. void setup()
  43. {
  44.   Serial.begin(9600);
  45.   pinMode(MotorRight1, OUTPUT); // 腳位 8 (PWM)
  46.   pinMode(MotorRight2, OUTPUT); // 腳位 9 (PWM)
  47.   pinMode(MotorLeft1, OUTPUT); // 腳位 10 (PWM)
  48.   pinMode(MotorLeft2, OUTPUT); // 腳位 11 (PWM)
  49.   irrecv.enableIRIn(); // 啟動紅外線解碼
  50.   pinMode(SensorLeft, INPUT); //定義左感測器
  51.   pinMode(SensorMiddle, INPUT);//定義中感測器
  52.   pinMode(SensorRight, INPUT); //定義右感測器
  53.   digitalWrite(2,HIGH);
  54.   pinMode(inputPin, INPUT); // 定義超音波輸入腳位
  55.   pinMode(outputPin, OUTPUT); // 定義超音波輸出腳位
  56.   myservo.attach(9); // 定義伺服馬達輸出第 5 腳位(PWM)
  57. }
  58. //******************************************************************(Void)
  59. void advance(int a) // 前進
  60. {
  61.   digitalWrite(MotorRight1,LOW);
  62.   digitalWrite(MotorRight2,HIGH);
  63.   digitalWrite(MotorLeft1,LOW);
  64.   digitalWrite(MotorLeft2,HIGH);
  65.   delay(a * 100);
  66. }
  67. void right(int b) //右轉(單輪)
  68. {
  69.   digitalWrite(MotorLeft1,LOW);
  70.   digitalWrite(MotorLeft2,HIGH);
  71.   digitalWrite(MotorRight1,LOW);
  72.   digitalWrite(MotorRight2,LOW);
  73.   delay(b * 100);
  74. }
  75. void left(int c) //左轉(單輪)
  76. {
  77.   digitalWrite(MotorRight1,LOW);
  78.   digitalWrite(MotorRight2,HIGH);
  79.   digitalWrite(MotorLeft1,LOW);
  80.   digitalWrite(MotorLeft2,LOW);
  81.   delay(c * 100);
  82. }
  83. void turnR(int d) //右轉(雙輪)
  84. {
  85.   digitalWrite(MotorRight1,HIGH);
  86.   digitalWrite(MotorRight2,LOW);
  87.   digitalWrite(MotorLeft1,LOW);
  88.   digitalWrite(MotorLeft2,HIGH);
  89.   delay(d * 100);
  90. }
  91. void turnL(int e) //左轉(雙輪)
  92. {
  93.   digitalWrite(MotorRight1,LOW);
  94.   digitalWrite(MotorRight2,HIGH);
  95.   digitalWrite(MotorLeft1,HIGH);
  96.   digitalWrite(MotorLeft2,LOW);
  97.   delay(e * 100);
  98. }
  99. void stopp(int f) //停止
  100. {
  101.   digitalWrite(MotorRight1,LOW);
  102.   digitalWrite(MotorRight2,LOW);
  103.   digitalWrite(MotorLeft1,LOW);
  104.   digitalWrite(MotorLeft2,LOW);
  105.   delay(f * 100);
  106. }
  107. void back(int g) //後退
  108. {
  109.   digitalWrite(MotorRight1,HIGH);
  110.   digitalWrite(MotorRight2,LOW);
  111.   digitalWrite(MotorLeft1,HIGH);
  112.   digitalWrite(MotorLeft2,LOW);
  113.   delay(g * 100);
  114. }
  115. void detection() //測量 3 個角度(前.左.右)
  116. {
  117.   int delay_time = 250; // 伺服馬達轉向後的穩定時間
  118.   ask_pin_F(); // 讀取前方距離
  119.   if(Fspeedd < 10) // 假如前方距離小於 10 公分
  120.   {
  121.     stopp(1); // 清除輸出資料
  122.     back(2); // 後退 0.2 秒
  123.   }
  124.   if(Fspeedd < 25) // 假如前方距離小於 25 公分
  125.   {
  126.     stopp(1); // 清除輸出資料
  127.     ask_pin_L(); // 讀取左方距離
  128.     delay(delay_time); // 等待伺服馬達穩定
  129.     ask_pin_R(); // 讀取右方距離
  130.     delay(delay_time); // 等待伺服馬達穩定
  131.     if(Lspeedd > Rspeedd) //假如 左邊距離大於右邊距離
  132.     {
  133.       directionn = Lgo; //向左走
  134.     }
  135.     if(Lspeedd <= Rspeedd) //假如 左邊距離小於或等於右邊距離
  136.     {
  137.       directionn = Rgo; //向右走
  138.     }
  139.     if (Lspeedd < 15 && Rspeedd < 15) //假如 左邊距離和右邊距離皆小於 10 公分
  140.     {
  141.       directionn = Bgo; //向後走
  142.     }
  143.   }
  144.   else //加如前方大於 25 公分
  145.   {
  146.     directionn = Fgo; //向前走
  147.   }
  148. }
  149. //*****************************************************************************
  150. void ask_pin_F() // 量出前方距離
  151. {
  152.   myservo.write(90);
  153.   digitalWrite(outputPin, LOW); // 讓超聲波發射低電壓 2μs
  154.   delayMicroseconds(2);
  155.   digitalWrite(outputPin, HIGH); // 讓超聲波發射高電壓 10μs,這裡至少是 10μs
  156.   delayMicroseconds(10);
  157.   digitalWrite(outputPin, LOW); // 維持超聲波發射低電壓
  158.   float Fdistance = pulseIn(inputPin, HIGH); // 讀差相差時間
  159.   Fdistance= Fdistance/5.8/10; // 將時間轉為距離距离(單位:公分)
  160.   Serial.print("F distance:"); //輸出距離(單位:公分)
  161.   Serial.println(Fdistance); //顯示距離
  162.   Fspeedd = Fdistance; // 將距離 讀入 Fspeedd(前速)
  163. }
  164. //*****************************************************************************

  165. void ask_pin_L() // 量出左邊距離
  166. {
  167.   myservo.write(177);
  168.   delay(delay_time);
  169.   digitalWrite(outputPin, LOW); // 讓超聲波發射低電壓 2μs
  170.   delayMicroseconds(2);
  171.   digitalWrite(outputPin, HIGH); // 讓超聲波發射高電壓 10μs,這裡至少是 10μs
  172.   delayMicroseconds(10);
  173.   digitalWrite(outputPin, LOW); // 維持超聲波發射低電壓
  174.   float Ldistance = pulseIn(inputPin, HIGH); // 讀差相差時間
  175.   Ldistance= Ldistance/5.8/10; // 將時間轉為距離距离(單位:公分)
  176.   Serial.print("L distance:"); //輸出距離(單位:公分)
  177.   Serial.println(Ldistance); //顯示距離
  178.   Lspeedd = Ldistance; // 將距離 讀入 Lspeedd(左速)
  179. }
  180. //*****************************************************************************

  181. void ask_pin_R() // 量出右邊距離
  182. {
  183.   myservo.write(5);
  184.   delay(delay_time);
  185.   digitalWrite(outputPin, LOW); // 讓超聲波發射低電壓 2μs
  186.   delayMicroseconds(2);
  187.   digitalWrite(outputPin, HIGH); // 讓超聲波發射高電壓 10μs,這裡至少是 10μs
  188.   delayMicroseconds(10);
  189.   digitalWrite(outputPin, LOW); // 維持超聲波發射低電壓
  190.   float Rdistance = pulseIn(inputPin, HIGH); // 讀差相差時間
  191.   Rdistance= Rdistance/5.8/10; // 將時間轉為距離距离(單位:公分)
  192.   Serial.print("R distance:"); //輸出距離(單位:公分)
  193.   Serial.println(Rdistance); //顯示距離
  194.   Rspeedd = Rdistance; // 將距離 讀入 Rspeedd(右速)
  195. }
  196. //******************************************************************************(LOOP)
  197. void loop()
  198. {
  199.   SL = digitalRead(SensorLeft);
  200.   SM = digitalRead(SensorMiddle);
  201.   SR = digitalRead(SensorRight);
  202.   //***************************************************************************正常遙控模式
  203.   if (irrecv.decode(&results))
  204.   { // 解碼成功,收到一組紅外線訊號
  205.     /***********************************************************************/
  206.     if (results.value == IRfront)//前進
  207.     {
  208.       advance(10);//前進
  209.     }
  210.     /***********************************************************************/
  211.     if (results.value == IRback)//後退
  212.     {
  213.       back(10);//後退
  214.     }
  215.     /***********************************************************************/
  216.     if (results.value == IRturnright)//右轉
  217.     {
  218.       right(6); // 右轉
  219.     }
  220.     /***********************************************************************/
  221.     if (results.value == IRturnleft)//左轉
  222.     {
  223.       left(6); // 左轉);
  224.     }
  225.     /***********************************************************************/
  226.     if (results.value == IRstop)//停止
  227.     {
  228.       digitalWrite(MotorRight1,LOW);
  229.       digitalWrite(MotorRight2,LOW);
  230.       digitalWrite(MotorLeft1,LOW);
  231.       digitalWrite(MotorLeft2,LOW);
  232.     }
  233.     //***************************************cny70模式自走模式 黑OW 白:
  234.     if (results.value == IRcny70)
  235.     {
  236.       while(IRcny70)
  237.       {
  238.         SL = digitalRead(SensorLeft);
  239.         SM = digitalRead(SensorMiddle);
  240.         SR = digitalRead(SensorRight);
  241.         if (SM == HIGH)//中感測器在黑色區域
  242.         {
  243.           if (SL == LOW & SR == HIGH) // 左黑右白, 向左轉彎
  244.           {
  245.             digitalWrite(MotorRight1,LOW);
  246.             digitalWrite(MotorRight2,HIGH);
  247.             analogWrite(MotorLeft1,0);
  248.             analogWrite(MotorLeft2,80);
  249.           }
  250.           else if (SR == LOW & SL == HIGH) //左白右黑, 向右轉彎
  251.           {
  252.             analogWrite(MotorRight1,0);//右轉
  253.             analogWrite(MotorRight2,80);
  254.             digitalWrite(MotorLeft1,LOW);
  255.             digitalWrite(MotorLeft2,HIGH);
  256.           }
  257.           else // 兩側均為白色, 直進
  258.           {
  259.             digitalWrite(MotorRight1,LOW);
  260.             digitalWrite(MotorRight2,HIGH);
  261.             digitalWrite(MotorLeft1,LOW);
  262.             digitalWrite(MotorLeft2,HIGH);
  263.             analogWrite(MotorLeft1,200);
  264.             analogWrite(MotorLeft2,200);
  265.             analogWrite(MotorRight1,200);
  266.             analogWrite(MotorRight2,200);
  267.           }
  268.         }
  269.         else // 中感測器在白色區域
  270.         {
  271.           if (SL == LOW & SR == HIGH)// 左黑右白, 快速左轉
  272.           {
  273.             digitalWrite(MotorRight1,LOW);
  274.             digitalWrite(MotorRight2,HIGH);
  275.             digitalWrite(MotorLeft1,LOW);
  276.             digitalWrite(MotorLeft2,LOW);
  277.           }
  278.           else if (SR == LOW & SL == HIGH) // 左白右黑, 快速右轉
  279.           {
  280.             digitalWrite(MotorRight1,LOW);
  281.             digitalWrite(MotorRight2,LOW);
  282.             digitalWrite(MotorLeft1,LOW);
  283.             digitalWrite(MotorLeft2,HIGH);
  284.           }
  285.           else // 都是白色, 停止
  286.           {
  287.             digitalWrite(MotorRight1,HIGH);
  288.             digitalWrite(MotorRight2,LOW);
  289.             digitalWrite(MotorLeft1,HIGH);
  290.             digitalWrite(MotorLeft2,LOW);
  291.             ;
  292.           }
  293.         }
  294.         if (irrecv.decode(&results))
  295.         {
  296.           irrecv.resume();
  297.           Serial.println(results.value,HEX);
  298.           if(results.value ==IRstop)
  299.           {
  300.             digitalWrite(MotorRight1,HIGH);
  301.             digitalWrite(MotorRight2,HIGH);
  302.             digitalWrite(MotorLeft1,HIGH);
  303.             digitalWrite(MotorLeft2,HIGH);
  304.             break;
  305.           }
  306.         }
  307.       }
  308.       results.value=0;
  309.     }
  310.     //***********************************************************************超音波自走模式
  311.     if (results.value ==IRAutorun )
  312.     {
  313.       while(IRAutorun)
  314.       {
  315.         myservo.write(90); //讓伺服馬達回歸 預備位置 準備下一次的測量
  316.         detection(); //測量角度 並且判斷要往哪一方向移動
  317.         if(directionn == 8) //假如 directionn(方向) = 8(前進)
  318.         {
  319.           if (irrecv.decode(&results))
  320.           {
  321.             irrecv.resume();
  322.             Serial.println(results.value,HEX);
  323.             if(results.value ==IRstop)
  324.             {
  325.               digitalWrite(MotorRight1,LOW);
  326.               digitalWrite(MotorRight2,LOW);
  327.               digitalWrite(MotorLeft1,LOW);
  328.               digitalWrite(MotorLeft2,LOW);
  329.               break;
  330.             }
  331.           }
  332.           results.value=0;
  333.           advance(1); // 正常前進
  334.           Serial.print(" Advance "); //顯示方向(前進)
  335.           Serial.print(" ");
  336.         }
  337.         if(directionn == 2) //假如 directionn(方向) = 2(倒車)
  338.         {
  339.           if (irrecv.decode(&results))
  340.           {
  341.             irrecv.resume();
  342.             Serial.println(results.value,HEX);
  343.             if(results.value ==IRstop)
  344.             {
  345.               digitalWrite(MotorRight1,LOW);
  346.               digitalWrite(MotorRight2,LOW);
  347.               digitalWrite(MotorLeft1,LOW);
  348.               digitalWrite(MotorLeft2,LOW);
  349.               break;
  350.             }
  351.           }
  352.           results.value=0;
  353.           back(8); // 倒退(車)
  354.           turnL(3); //些微向左方移動(防止卡在死巷裡)
  355.           Serial.print(" Reverse "); //顯示方向(倒退)
  356.         }
  357.         if(directionn == 6) //假如 directionn(方向) = 6(右轉)
  358.         {
  359.           if (irrecv.decode(&results))
  360.           {
  361.             irrecv.resume();
  362.             Serial.println(results.value,HEX);
  363.             if(results.value ==IRstop)
  364.             {
  365.               digitalWrite(MotorRight1,LOW);
  366.               digitalWrite(MotorRight2,LOW);
  367.               digitalWrite(MotorLeft1,LOW);
  368.               digitalWrite(MotorLeft2,LOW);
  369.               break;
  370.             }
  371.           }
  372.           results.value=0;
  373.           back(1);
  374.           turnR(6); // 右轉
  375.           Serial.print(" Right "); //顯示方向(左轉)
  376.         }
  377.         if(directionn == 4) //假如 directionn(方向) = 4(左轉)
  378.         {
  379.           if (irrecv.decode(&results))
  380.           {
  381.             irrecv.resume();
  382.             Serial.println(results.value,HEX);
  383.             if(results.value ==IRstop)
  384.             {
  385.               digitalWrite(MotorRight1,LOW);
  386.               digitalWrite(MotorRight2,LOW);
  387.               digitalWrite(MotorLeft1,LOW);
  388.               digitalWrite(MotorLeft2,LOW);
  389.               break;
  390.             }
  391.           }
  392.           results.value=0;
  393.           back(1);
  394.           turnL(6); // 左轉
  395.           Serial.print(" Left "); //顯示方向(右轉)
  396.         }
  397.         if (irrecv.decode(&results))
  398.         {
  399.           irrecv.resume();
  400.           Serial.println(results.value,HEX);
  401.           if(results.value ==IRstop)
  402.           {
  403.             digitalWrite(MotorRight1,LOW);
  404.             digitalWrite(MotorRight2,LOW);
  405.             digitalWrite(MotorLeft1,LOW);
  406.             digitalWrite(MotorLeft2,LOW);
  407.             break;
  408.           }
  409.         }
  410.       }
  411.       results.value=0;
  412.     }
  413.     /***********************************************************************/
  414.     else
  415.     {
  416.       digitalWrite(MotorRight1,LOW);
  417.       digitalWrite(MotorRight2,LOW);
  418.       digitalWrite(MotorLeft1,LOW);
  419.       digitalWrite(MotorLeft2,LOW);
  420.     }
  421.     irrecv.resume(); // 繼續收下一組紅外線訊號
  422.   }
  423. }
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-1-25 11:02:02 | 显示全部楼层
正常遙控模式   \cny70模式自走模式 \超音波自走模式 这三种模式如何切换  我改了下现在只能实现蓝牙控制小车前进后退左右转弯 ,,但是 如何切换模式 让小车自动巡线和超声壁障  就不知道如何改代码了  哪位大牛 能帮帮忙
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-25 11:02:29 | 显示全部楼层
蓝牙模块 用的是HC-06 接到 v5的扩展板的蓝牙接口  用的事arduino uno r3的控制板
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-5 13:40 , Processed in 0.035232 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表