求大神帮助修改代码

2019-11-26 13:26发布

<font face="微软雅黑"><font style="font-size:18px">想要用Arduino做个玩具</font></font><font color="#333333"><font face="微软雅黑"><font style="font-size:18px">但是程序问题</font></font></font><br><pre style="max-width: 100%;"><code class="cpp hljs" codemark="1"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> //载入Servo库</span> <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> InA A5 <span class="hljs-comment">//定义读取a光敏电阻的模拟口</span></span> <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> InB A4 <span class="hljs-comment">//定义读取b光敏电阻的模拟口</span></span> <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> InC A3</span> <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> InD A2</span> <span class="hljs-keyword">int</span> valA,valB,valC,valD; <span class="hljs-comment">//用于存储两个光敏电阻的数值 </span> <span class="hljs-keyword">int</span> angle =<span class="hljs-number">90</span>; <span class="hljs-comment">//设置初始角度</span> <span class="hljs-keyword">int</span> range = <span class="hljs-number">180</span>; <span class="hljs-comment">//设置范围</span> Servo myServo1,myServo2; <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">setup</span><span class="hljs-params">()</span> </span>{ myServo1.attach(<span class="hljs-number">9</span>); <span class="hljs-comment">//定义舵机接口</span> myServo1.write(angle); <span class="hljs-comment">//初始化舵机角度为0度</span> myServo2.attach(<span class="hljs-number">10</span>); myServo2.write(angle); Serial.begin(<span class="hljs-number">9600</span>); } <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">loop</span><span class="hljs-params">()</span> </span>{ valA = analogRead(InA); <span class="hljs-comment">//读取A光敏电阻数据</span> valB = analogRead(InB); <span class="hljs-comment">//读取B光敏电阻数据</span> valC = analogRead(InC); valD = analogRead(InD); <span class="hljs-comment">//A大于B,A比B光 A-B为正数 舵机向A方向转</span> IF(valA-valB &gt; range) { angle = angle + <span class="hljs-number">1</span>; <span class="hljs-comment">//角度+1</span> angle = constrain(angle,<span class="hljs-number">0</span>,<span class="hljs-number">180</span>); <span class="hljs-comment">//约束输出</span> myServo1.write(angle); <span class="hljs-comment">//执行角度</span> } <span class="hljs-comment">//A小于B,B比A光 A-B为负数 舵机向B方向转</span> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(valA - valB &lt; -range) { angle = angle - <span class="hljs-number">1</span>; angle = constrain(angle,<span class="hljs-number">0</span>,<span class="hljs-number">180</span>); myServo1.write(angle); } <span class="hljs-keyword">if</span>(valC-valD &gt; range) { angle = angle + <span class="hljs-number">1</span>; <span class="hljs-comment">//角度+1</span> angle = constrain(angle,<span class="hljs-number">0</span>,<span class="hljs-number">180</span>); <span class="hljs-comment">//约束输出</span> myServo2.write(angle); <span class="hljs-comment">//执行角度</span> } <span class="hljs-comment">//A小于B,B比A光 A-B为负数 舵机向B方向转</span> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(valC - valD &lt; -range) { angle = angle - <span class="hljs-number">1</span>; angle = constrain(angle,<span class="hljs-number">0</span>,<span class="hljs-number">180</span>); myServo2.write(angle); } <span class="hljs-comment">//串口输出数据,便于观察工作原理</span> Serial.print(<span class="hljs-string">"A="</span>); Serial.print(valA); Serial.print(<span class="hljs-string">"||B="</span>); Serial.println(valB); Serial.print(<span class="hljs-string">"Angle="</span>); Serial.println(angle); delay(<span class="hljs-number">10</span>); <span class="hljs-comment">//延时</span> } </code></pre><p><br></p>
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。