This chapter used the infrared IR LED/receiver pairs for simple distance detection, to make a BOE Shield-Bot shadow vehicle. Now-familiar skills combined with some new concepts got the job done:
for(long f = 38000; f <= 42000; f += 1000) { distance += irDetect(irLedPin, irReceivePin, f); }
const int setpoint = 2; // Target distances const int kpl = -45; // Proportional control constants const int kpr = -55;
void loop() // Main loop auto-repeats { int irLeft = irDistance(9, 10); // Measure left distance int irRight = irDistance(2, 3); // Measure right distance // Left and right proportional control calculations int driveLeft = (setpoint - irLeft) * kpl; int driveRight = (setpoint - irRight) * kpr; int drive = (driveLeft + driveRight)/2; // Average drive levels maneuver(drive, drive, 20); // Apply same drive to both delay(10); // 0.1 second delay }