极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9102|回复: 0

急需大神帮忙

[复制链接]
发表于 2013-8-24 15:44:06 | 显示全部楼层 |阅读模式
我制作的循迹小车  为什么循迹传感器都过黑线了   检测不到似得   但是用电脑看   还可以检测到    那就应该是反应慢的问题   但是不知道问题在那    球大神帮忙     代码如下
  1. #include <FlexiTimer2.h>
  2. int countsperrev =28;
  3. int countsperrev2 =28;// 定义电机每次转动的步数,1.8步进角的一圈的总步数为58
  4. int count = 0;// 计算电机累积步数
  5. int count2 = 0;
  6. int motorSpeed =3000;//能够设定步进速度  延时时间
  7. //n=电机旋转圈数
  8. int k;
  9. int Pin0 = 8;
  10. int Pin1 = 9;
  11. int Pin2 = 10;
  12. int Pin3 = 11;
  13. int Pin4 = 4,Pin5 = 5,Pin6 = 6,Pin7 = 7;
  14. int xunji2;
  15. int xunji0;
  16. int xunji1;
  17. int xunji3;
  18. int xunji4;
  19. int lookup[8] = {
  20.   B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
  21. void flash()
  22. {
  23.   xunji2= analogRead(A2);
  24.   xunji0= analogRead(A0);
  25.   xunji1= analogRead(A1);
  26.   xunji3= analogRead(A3);
  27.   xunji4= analogRead(A4);
  28.   Serial.println(xunji3, DEC);
  29. }

  30. void setup()
  31. {
  32.   FlexiTimer2::set(500,flash); // call every 500 1ms "ticks"
  33.   // FlexiTimer2::set(500, flash); // MsTimer2 style is also supported
  34.   FlexiTimer2::start();
  35.   pinMode(Pin0, OUTPUT);
  36.   pinMode(Pin1, OUTPUT);
  37.   pinMode(Pin2, OUTPUT);
  38.   pinMode(Pin3, OUTPUT);
  39.   pinMode(Pin4, OUTPUT);
  40.   pinMode(Pin5, OUTPUT);
  41.   pinMode(Pin6, OUTPUT);
  42.   pinMode(Pin7, OUTPUT);
  43.   Serial.begin(9600);

  44. }

  45. void loop()
  46. {

  47.   if(xunji3>150)
  48.   {
  49.     zuozhuan();
  50.   }
  51.   if(xunji3<150)
  52.   {
  53.     qianjin();
  54.   }
  55. }

  56. void dianjiting()//关闭电机函数
  57. {
  58.   digitalWrite(Pin0, 0);
  59.   digitalWrite(Pin1, 0);
  60.   digitalWrite(Pin2, 0);
  61.   digitalWrite(Pin3, 0);
  62.   digitalWrite(Pin4, 0);
  63.   digitalWrite(Pin5, 0);
  64.   digitalWrite(Pin6, 0);
  65.   digitalWrite(Pin7, 0);
  66. }
  67. void setOutput(int out)//行走输入
  68. {
  69.   digitalWrite(Pin0,  bitRead(lookup[out],0));
  70.   digitalWrite(Pin1,  bitRead(lookup[out],1));
  71.   digitalWrite(Pin2,  bitRead(lookup[out],2));
  72.   digitalWrite(Pin3,   bitRead(lookup[out],3));
  73. }
  74. void setOutput2(int out)
  75. {
  76.   digitalWrite(Pin4, bitRead(lookup[out], 0));
  77.   digitalWrite(Pin5, bitRead(lookup[out], 1));
  78.   digitalWrite(Pin6, bitRead(lookup[out], 2));
  79.   digitalWrite(Pin7, bitRead(lookup[out], 3));
  80. }
  81. void clockwise()
  82. {
  83.   for(int i = 7; i >= 0; i--)
  84.   {
  85.     setOutput(i);
  86.     delayMicroseconds(motorSpeed);//延时时间
  87.   }
  88. }
  89. void anticlockwise()
  90. {
  91.   for(int i = 0; i < 8; i++)
  92.   {
  93.     setOutput(i);
  94.     delayMicroseconds(motorSpeed);
  95.   }
  96. }
  97. void anticlockwise2()
  98. {
  99.   for(int i = 0; i < 8; i++)
  100.   {
  101.     setOutput2(i);
  102.     delayMicroseconds(motorSpeed);
  103.   }

  104. }
  105. void clockwise2()
  106. {
  107.   for(int i = 7; i >= 0; i--)
  108.   {
  109.     setOutput2(i);
  110.     delayMicroseconds(motorSpeed);
  111.   }
  112. }
  113. //以下为定义A2电机顺时针旋转函数
  114. void youzhuan()
  115. {
  116.   int n=1;
  117.   for(k=0;k<n;k++)
  118.   {
  119.     for(count=0,count2=0;count < countsperrev,count2 < countsperrev2;count++,count2++)
  120.       youzhuan1();
  121.   }
  122. }
  123. void zuozhuan()
  124. {
  125.   int n=1;
  126.   for(k=0;k<n;k++)
  127.   {
  128.     for(count2=0,count=0;count2 < countsperrev2,count < countsperrev;count2++,count++)
  129.       zuozhuan1();
  130.   }
  131. }
  132. void qianjin()
  133. {
  134.   int n=1;
  135.   for(k=0;k<n;k++)
  136.   {
  137.     for(count2=0,count=0;count2 < countsperrev2,count < countsperrev;count2++,count++)
  138.       qianjin1();
  139.   }
  140. }

  141. void houtui()
  142. {
  143.   int n=1;
  144.   for(k=0;k<n;k++)
  145.   {
  146.     for(count=0,count2=0;count < countsperrev,count2 < countsperrev2;count++,count2++)
  147.       houtui1();
  148.   }
  149. }
  150. //以下为定义A2电机端口设定函数

  151. void houtui1()
  152. {
  153.   for(int i = 0; i < 8; i++)
  154.   {
  155.     setOutput(i);
  156.     setOutput2(i);
  157.     delayMicroseconds(motorSpeed);
  158.   }
  159. }
  160. void qianjin1()
  161. {
  162.   for(int i = 7; i >= 0; i--)
  163.   {
  164.     setOutput(i);
  165.     setOutput2(i);
  166.     delayMicroseconds(motorSpeed);//延时时间
  167.   }
  168. }
  169. void zuozhuan1()
  170. {
  171.   for(int i = 0,i1 = 7; i < 8,i1 >= 0; i++,i1--)
  172.   {
  173.     setOutput(i);
  174.     setOutput2(i1);
  175.     delayMicroseconds(motorSpeed);
  176.   }
  177. }
  178. void youzhuan1()
  179. {
  180.   for(int i = 7,i1 = 0; i >= 0,i1 < 8; i--, i1++)
  181.   {
  182.     setOutput(i);
  183.     setOutput2(i1);
  184.     delayMicroseconds(motorSpeed);//延时时间
  185.   }
  186. }
复制代码
回复

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 21:18 , Processed in 0.037210 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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