1、太阳能收集装置参考代码#include /一些库函数 #include #include Servo DF15MG1;Servo DF15MG2;int vala,valb,valc,vald,sum,mindata;int average25,pos2=0,i=1,no;void ready()DF15MG2.write(0); void setup() DF15MG1.attach(3);DF15MG2.attach(11); DF15MG1.write(70);DF15MG2.write(0);delay(1000);for(int i= 0; i averagei)mindata=av
2、eragei; /找到最小值及其最小值的编号no = i; DF15MG2.write(no*10); /舵机转到光源所在方向追光机器人参考代码 #include /一些函数库#include #include Servo DF15MG1;Servo DF15MG2;Metro receMetro=Metro(20);Metro proceMetro=Metro(30);Metro transMetro=Metro(40);int sumab,sumcd,pos1=90,pos2=90; /定义变量int vala,valb,valc,vald;void ready()DF15MG1.writ
3、e(90); /设定舵机的初始位置为 90DF15MG2.write(90);void setup()DF15MG1.attach(3); DF15MG2.attach(11);void loop() if(receMetro.check()=1) /控制数据采集、处理、输出时间receMetro.interval(10);receivedata(); if(proceMetro.check()=1)proceMetro.interval(15);processdata(); if(transMetro.check()=1)transMetro.interval(20);transmitdat
4、a(); void receivedata()vala=analogRead(0); /设定光传感器模拟数据读取端口valb=analogRead(1); valc=analogRead(2);vald=analogRead(3); void processdata() sumab=vala-valb; /横竖光传感器读入数值求差sumcd=valc-vald;if(sumab10)pos1=pos1-1;pos1=constrain(pos1,60,140);if(sumcd20)pos2=pos2-1;pos2=constrain(pos2,0,180); void transmitdata()DF15MG1.write(pos1); /舵机旋转角度输出DF15MG2.write(pos2);