The following line sensor code contains the edited version for a 3 line sensor...
Comments are placed for your reference. The codes are purposedly jumbled for more challenge in concept mapping. Retype the code, convert it to a 4 line sensor.
.:start of code:.
Arduino
has to stay connected to computer.
Sensors must have their voltage provided.
Place the robot entirely onto its white running surface
presumably so that there is black line on its left,
but not in the view of sensors.
Press arduinos reset button.
After 2 seconds pin 13 onboard LED should light up.
White surface calibration measurings are done while the LED is lit -
about 2 sec.
LED will go out after that.
After a 2 seconds delay the LED will light up again.
Now you have 5 seconds to move the robot sideways over a black line.
Line has to be on the left when facing same way as robot is going to be
running.
You have to move the robot to its left and approximately in right angle
to the line.
During this move the maximum readings are taken that occur when a single
sensor is directly over the line.
Do not hurry, but all the sensors must be over line when LED goes out
after 5 sec.
After another 2 sec delay the LED will light up once again.
Now you have to move the robot back over the line, opposite to what you did in
last step.
It means during 5 sec move robot back to right over the line.
Important: this time robot has to move right - right side sensor has to
pass over line first!
During this are taken measures that say, how low do the two side by side
sensors go, when line is exactly inbetween the two.
Measurings end when LED goes out after 5 sec.
Start serial monitor.
Stop autoscroll and copy correction values from any successive three lines to
robots riding script.
*/
/*sensor input pins*/
const int sensorPins[] = {A0, A1, A2}; //right, center, left
/*onboard LED pin*/
const int indicatorPin = 13;
/*arrays for storing sensor and computed data
three sensors in each array:
0 - RIGHT, 1 - CENTER, 2 - LEFT */
float readings[3]; //sensor readings
float averageReadings[3] = {0, 0, 0}; //average readings
float whitePoints[3] = {0, 0, 0}; //reading on white surface
int blackPoints[3] = {0, 0, 0}; //maximum reading while passing over black line
float unitSteps[3]; //difference between white and black divided with 100
int corrections[3]; //white points as int
float peakDetect[3] = {0, 0, 0};
float betweenReadings[2] = {1024, 1024}; //readings when line is inbetween two
sensors, RIGHT-CENTER, LEFT-CENTER
boolean betweenState[3] = {false, false, false}; //helper for knowing which
minimum to store
/*loop helper variables*/
int i;
int j;
void setup() {
Serial.begin(9600); //start serial monitor to output data after
measurings
pinMode(indicatorPin, OUTPUT); //set nboard led pin to indicate
measuring state
/*start calibration after 2 sec delay*/
delay(2000);
/*MEASURING ON WHITE SURFACE FOR ABOUT 2 SECONDS ASSUMED*/
digitalWrite(indicatorPin, HIGH); //onboard LED lights up
for (i=0; i<=125; i++) { //125 readings from each sensor
for (j=0; j<=2; j++) { //loop through each sensor
readings[j] = analogRead(sensorPins[j]); //get
value
/*calculate average sensor values*/
whitePoints[j] = (whitePoints[j] * j +
readings[j]) / (j+1); //average from all measurings taken this far
delay(2);
}
delay(10);
}
digitalWrite(indicatorPin, LOW); //onboard LED goes out
delay(2000); //2 sec delay
/*GOING SIDEWAYS OVER BLACK LINE WITHIN NEXT 5 SECONDS ASSUMED*/
digitalWrite(indicatorPin, HIGH); //onboard LED lights up
for (i=0; i<320; i++) { //320 readings from each sensor
for (j=0; j<=2; j++) { //loop through each sensor
readings[j] = analogRead(sensorPins[j]); //get
value
if (readings[j] > blackPoints[j]) { //if
current value is grater than one olready saved
blackPoints[j] = readings[j];
//store if aplicable
}
delay(2);
}
delay(10);
}
digitalWrite(indicatorPin, LOW); //onboard LED goes out
/*calculate "unit step" - 1/100th of difference between white
and black for every sensor
and transform white points to int (corrections)*/
for (j=0; j<=2; j++) {
unitSteps[j] = (blackPoints[j] - whitePoints[j])/1000;
corrections[j] = (int) (whitePoints[j] + 0.5);
}
delay(2000); //2 sec delay
/*GOING SIDEWAYS (TO THE RIGHT!!!) OVER BLACK LINE WITHIN NEXT 5 SECONDS
ASSUMED*/
digitalWrite(indicatorPin, HIGH); //onboard LED lights up
for (j=0; j<=2; j++) { //to reuse this array, set all to 0
blackPoints[j] = 0;
}
for (i=0; i<320; i++) { //320 readings from each sensor
for (j=0; j<=2; j++) { //loop through each sensor
readings[j] = (analogRead(sensorPins[j]) -
corrections[j]) / unitSteps[j]; //get value for each in final units
}
for (j=0; j<=2; j++) { //loop through three sensor
readings
if (readings[j] > blackPoints[j]) { //if
current reading is greater than stored maxima
blackPoints[j] = readings[j]; //store if
appicable
}
else if (readings[j] + 30 < blackPoints[j]) {
//if reading is decreasing (stored one is greater than current)
betweenState[j] = true; //indicate
in this variable that sensor has moved over a maximum reading (line)
}
if (betweenState[0] == true &&
betweenState[1] == false) { //if first sensor reading has passed its maxima and
second has not
if (betweenReadings[0] >
readings[0] + readings[1]) { //if stored sum of two sensor readings is bigger
than current sum
betweenReadings[0] =
readings[0] + readings[1]; //store if applicable
}
}
else if (betweenState[1] == true &&
betweenState[2] == false) { //if second sensor reading has passed its maxima
and third has not
if (betweenReadings[1] >
readings[1] + readings[2]) { //if stored sum of two sensor readings is bigger
than current sum
betweenReadings[1] =
readings[1] + readings[2]; //store if applicable
}
}
delay(2);
}
delay(10);
}
digitalWrite(indicatorPin, LOW); //onboard LED goes out
} // /setup
void loop() {
/*print results in serial monitor*/
Serial.print("const int corrections[3] = {");
Serial.print(corrections[0]);
Serial.print(",");
Serial.print(corrections[1]);
Serial.print(",");
Serial.print(corrections[2]);
Serial.println("};");
Serial.print("const float unitSteps[3]= {");
Serial.print(unitSteps[0]);
Serial.print(",");
Serial.print(unitSteps[1]);
Serial.print(",");
Serial.print(unitSteps[2]);
Serial.println("};");
Serial.print("const float betweenReadings[2] = {");
Serial.print(betweenReadings[0]);
Serial.print(",");
Serial.print(betweenReadings[1]);
Serial.println("};");
Serial.println(" ");
} // /loop