#include <ArduinoRobot.h>
#include <Wire.h>
#include <SPI.h>
int maxspeed;
int pos;
int ontheline;
void setup() {
Robot.begin();
}
void loop() {
int maxspd = Robot.knobRead() ; // the value is in the range [0:1023]
// map the value into an useful range
maxspd = map(maxspd, 0, 1023, 50, 255); // minimum speed is 10
//this function needs about 10 ms to complete
Robot.updateIR();
pos = 5; // no line at all
ontheline = 1024;
for (int i = 0; i < 5; i++) {
// White background, black line
// the lowest value read is from the sensor over the line
if (Robot.IRarray[i] < ontheline){
ontheline = Robot.IRarray[i];
pos = i;
}
}
//one last check: have we lost the line
if (Robot.IRarray[pos] > 500){
pos = 5;
}
switch (pos) {
case 0: // Sharp turn right
Robot.motorsStop();
Robot.motorsWrite(maxspd, -maxspd);
break;
case 1: // Turn right
Robot.motorsStop();
Robot.motorsWrite(maxspd, 0 );
break;
case 2: // Go forward
Robot.motorsStop();
Robot.motorsWrite(maxspd, maxspd);
break;
case 3: // Turn left
Robot.motorsStop();
Robot.motorsWrite(0, maxspd);
break;
case 4: // Sharp turn right
Robot.motorsStop();
Robot.motorsWrite(-maxspd, maxspd);
break;
case 5: // No line. Go slowly left
Robot.turn(-180);
delay(3000);
Robot.motorsWrite(maxspd, maxspd);
break;
}
}
大神求教,上面是巡线的代码,Robot.IRarray[]包含的值是什么,
(Robot.IRarray[0] < Robot.IRarray[1])有什么意义么?
这是我用的小车
#include <Wire.h>
#include <SPI.h>
int maxspeed;
int pos;
int ontheline;
void setup() {
Robot.begin();
}
void loop() {
int maxspd = Robot.knobRead() ; // the value is in the range [0:1023]
// map the value into an useful range
maxspd = map(maxspd, 0, 1023, 50, 255); // minimum speed is 10
//this function needs about 10 ms to complete
Robot.updateIR();
pos = 5; // no line at all
ontheline = 1024;
for (int i = 0; i < 5; i++) {
// White background, black line
// the lowest value read is from the sensor over the line
if (Robot.IRarray[i] < ontheline){
ontheline = Robot.IRarray[i];
pos = i;
}
}
//one last check: have we lost the line
if (Robot.IRarray[pos] > 500){
pos = 5;
}
switch (pos) {
case 0: // Sharp turn right
Robot.motorsStop();
Robot.motorsWrite(maxspd, -maxspd);
break;
case 1: // Turn right
Robot.motorsStop();
Robot.motorsWrite(maxspd, 0 );
break;
case 2: // Go forward
Robot.motorsStop();
Robot.motorsWrite(maxspd, maxspd);
break;
case 3: // Turn left
Robot.motorsStop();
Robot.motorsWrite(0, maxspd);
break;
case 4: // Sharp turn right
Robot.motorsStop();
Robot.motorsWrite(-maxspd, maxspd);
break;
case 5: // No line. Go slowly left
Robot.turn(-180);
delay(3000);
Robot.motorsWrite(maxspd, maxspd);
break;
}
}
大神求教,上面是巡线的代码,Robot.IRarray[]包含的值是什么,
(Robot.IRarray[0] < Robot.IRarray[1])有什么意义么?
这是我用的小车