Each activity area had a specific length of paper. We had to detect the length of paper in order to determine the appropriate activity.
Paper Measurements are as follows:
After measuring the paper, we display the length detected on the screen.
int measurePaper() {
///write code here to measure length of paper
Stop(0);
delay(1000);
//first, line each wheel up with the edge of the paper
leftMotorSpeed=0;
rightMotorSpeed=-100;
Forward(0);
while(digitalRead(PaperSensor)==LOW && !lost);
if (lost) return;
Stop(0);
delay(1000);
leftMotorSpeed=-100;
rightMotorSpeed=0;
Forward(0);
int psmvg[4];
int total1=0;
for (byte i =0;i<4;i++) total1+=psmvg[i]=analogRead(PaperSensor2);
int index1=0;
while(total1/4>=30 && !lost) {
total1-=psmvg[index1];
total1+=psmvg[index1]=analogRead(PaperSensor2);
index1=(index1+1)%4;
}
if (lost) return 0;
leftTick=0;
while(leftTick<2);
Stop(0);
delay(1000);
//drive forward
leftMotorSpeed=leftMotorDefaultSpeed*0.7;
rightMotorSpeed=rightMotorDefaultSpeed*0.8;
Forward(0);
int lastread;
while ((lastread=digitalRead(PaperSensor))==HIGH);
long atleast=millis(); // set time
while(millis()-atleast<5) {
int currentread=digitalRead(PaperSensor);
if (lastread!=currentread) { atleast=millis(); }
lastread=currentread;
}
//setting up sensors after we hit the inital edge of the paper
leftTick=0;
bool lastps1 = digitalRead(PaperSensor);
int lastps2;
int total=0;
int index=0;
for (int i=0; i<4;i++) total+=paperSensorMovingAverage[i]=analogRead(PaperSensor2);
long lastChange=millis();
//drive until edge of paper
while (1 && !lost) {
bool ps1 = digitalRead(PaperSensor);
total-=paperSensorMovingAverage[index];
paperSensorMovingAverage[index]=analogRead(PaperSensor2);
index=(index+1)%4;
int ps2=total/4;
if (ps1!=lastps1) lastChange=millis();
if (ps2<30 && millis()-lastChange>50 && ps1) break; //we have crossed over the paper
lastps1=ps1;
}
if (lost) return 0;
Stop(0);
//calculate length and return/display on screen
paperLength=(int)(leftTick*(0.945));
currentMenu=paperLengthMenu;
drawCurrentMenu();
delay(1000);
return paperLength;
}
Each area is usually detected properly. Sometimes if the robot hit the paper at an sharp angle, it was able to measure the paper correctly, but it might lose the line on the other side of the paper, if it did not drive straight across the paper. Libero did provide a straight line on top of the paper to follow, but the position of our Paper Sensor did not allow us to track that line, and moving it would have involved too much work, sa its position was important for following the sharp curves in other areas.