There are two different type of targets in the Final Lab. One of them is the IR target that is seen in previous labs, the second type of target is the dark target. The dark target does not have an IR the robot uses the GP2D12 to detect the edges of the target, and then positions the servo in the middle of those two edges and fires the laser.
void darkTarget() {
delay(200); //wait for button to lift
if (!myservo.attached()) myservo.attach(servoPin);
myservo.write(0);
long now=millis();
while(millis()-now<2000);
int psavg[4] = {0,0,0,0};
int total=0;
int tmp;
for (byte i=0; i<4;i++) total+=psavg[i]=analogRead(A3);//(tmp=microsecondsToCentimeters(ping())==0)?400:tmp;
byte index=0;
int greatestDegree=0;
int readings[180];
//scan
for (byte degree=0;degree<180;degree++) {
myservo.write(degree);
now=millis();
while(millis()-now<20);
total=total-psavg[index];
total+=psavg[index]=analogRead(A3);//(tmp=microsecondsToCentimeters(ping())==0)?400:tmp;
index=(index+1)%4;
readings[degree]=total/4;
if (readings[degree]>readings[greatestDegree]) greatestDegree=degree;
if (useSave) saveData(useSave); //log data
}
//move servo to lowest degree
myservo.write((greatestDegree-4<0)?0:greatestDegree-4);
int leftedge=greatestDegree;
for (;readings[leftedge]>50;leftedge--);
int rightedge=greatestDegree;
for (;readings[rightedge]>50;rightedge++);
myservo.write((leftedge+(rightedge-leftedge)/2-4<0)?0:leftedge+(rightedge-leftedge)/2-4);
now=millis();
while(millis()-now<2000); //wait two seconds
//fire laser
digitalWrite(laserPin, HIGH);
now=millis();
while(millis()-now<5000);
digitalWrite(laserPin, LOW);
myservo.write(90);
now=millis();
while(millis()-now<2000);
servoOff();
followCurvyLine(1);
}
In most of our tests, the robot does not hit the darkest point on the target. It hits the other inner areas of the target as shown below: