Self calibration of my reflective sensors on my autonomous line following car

Hi everyone,

I had some troubles recently with my latest project. I’m designing an autonomous line following pace car (to use on an athletic track for example) and to do this I use reflective sensors that read the value of the line (analogRead(pin#)) and the value of the track lane.
I’m trying to do some statistical analysis before I start doing my program by recording some samples of the values. Ideally, I want to set up the center the car on the line and say: read all the values, record them, then make a few steps, record a second array of values, few steps… etc.

The code below is the code I’m using to make those few steps between each readings but for some reasons it’s going to go full reverse sometimes, go full speed, go really fast forward for 2 seconds, go really slow forever, I just don’t understand why.
I imagine I don’t initiate well the ESC but after trying many things I couldn’t make it work.

The code isn’t too clear but I’ll answer any question you have about it if we can make it work.

#include <LiquidCrystal.h> //Includes library from using the LCD display
#include <Servo.h> //Includes library to control the servo on the RC car

Servo throttle; //initialize variables
Servo steering; //initialize variables
double velocity=0.5; //speed
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //map pins to lcd screen

void setup() //run once
{
pinMode(7,INPUT); //sB
Serial.begin(9600);
pinMode(13,OUTPUT);
delay(1000);

steering.attach(10);
steering.write(90); //point straight
throttle.attach(6);
throttle.write(70); initiate the ESC. 77 is the lowest value and 180 is the highest
delay(150);
digitalWrite(6,LOW);
delay(150);
}

void loop()
{
lcd.begin(16,2);
lcd.print(“calibration?”);

int sB=digitalRead(7);
if(sB==HIGH)
{
lcd.begin(16,2);
lcd.print(“calibration…”);
delay(1000);

//line values
int r7=0,r17=0,r27=0,r37=0;
int r8=0,r18=0,r28=0,r38=0;

//lane values on right
int r1=0,r11=0,r21=0,r31=0;
int r2=0,r12=0,r22=0,r32=0;
int r3=0,r13=0,r23=0,r33=0;

//lane values on left
int rc=0,r1c=0,r2c=0,r3c=0;
int rd=0,r1d=0,r2d=0,r3d=0;
int re=0,r1e=0,r2e=0,r3e=0;

//others
int r0=0,r4=0,r5=0,r6=0,r9=0,ra=0,rb=0,rf=0;

/*
1ST SAMPLE
/
r1=(analogRead(A1))-((0.7/100)
(analogRead(A1)));
r11=r1;
r2=(analogRead(A2))-((0.12/100)(analogRead(A2)));
r12=r2;
r3=analogRead(A3);
r13=r3;
rc=(analogRead(A12))+((13.7/100)
(analogRead(A12)));
r1c=rc;
rd=(analogRead(A13))+((11.1/100)(analogRead(A13)));
r1d=rd;
re=(analogRead(A14))+((13.1/100)
(analogRead(A14)));
r1e=re;
r7=(analogRead(A7))+((2.77/100)(analogRead(A7)));
r17=r7;
r8=(analogRead(A8))+((5.2/100)
(analogRead(A8)));
r18=r8;

delay(2000);

throttle.attach(6);
velocity=map(velocity,0,100,77,180); //scales speed percentage to range motor will accept
throttle.write(velocity);
delay(500);
digitalWrite(6,LOW);
steering.write(90);
throttle.attach(6);
throttle.write(0);
delay(2000);

/*
2ND SAMPLE
/
r1=(analogRead(A1))-((0.7/100)
(analogRead(A1)));
r21=r1;
r2=(analogRead(A2))-((0.12/100)(analogRead(A2)));
r22=r2;
r3=analogRead(A3);
r23=r3;
rc=(analogRead(A12))+((13.7/100)
(analogRead(A12)));
r2c=rc;
rd=(analogRead(A13))+((11.1/100)(analogRead(A13)));
r2d=rd;
re=(analogRead(A14))+((13.1/100)
(analogRead(A14)));
r2e=re;
r7=(analogRead(A7))+((2.77/100)(analogRead(A7)));
r27=r7;
r8=(analogRead(A8))+((5.2/100)
(analogRead(A8)));
r28=r8;

delay(2000);

velocity=map(velocity,0,100,77,180); //scales speed percentage to range motor will accept
throttle.attach(6);
throttle.write(velocity);
delay(500);
digitalWrite(6,LOW);
steering.write(90);
delay(2000);

/*
3RD SAMPLE
/
r1=(analogRead(A1))-((0.7/100)
(analogRead(A1)));
r31=r1;
r2=(analogRead(A2))-((0.12/100)(analogRead(A2)));
r32=r2;
r3=analogRead(A3);
r33=r3;
rc=(analogRead(A12))+((13.7/100)
(analogRead(A12)));
r3c=rc;
rd=(analogRead(A13))+((11.1/100)(analogRead(A13)));
r3d=rd;
re=(analogRead(A14))+((13.1/100)
(analogRead(A14)));
r3e=re;
r7=(analogRead(A7))+((2.77/100)(analogRead(A7)));
r37=r7;
r8=(analogRead(A8))+((5.2/100)
(analogRead(A8)));
r38=r8;

digitalWrite(6,LOW);
// steering.write(90);
// delay(2000);

Sadly your motor is doing exactly what you tell it to do.

You should probably not use 'velocity' sometimes as a decimal fraction of full speed, sometimes as a percentage in the speed range, and sometimes the PWM value to send to the ESC! Pick one meaning for 'velocity' and calculate the other values when you need them.

      throttle.attach(6);                                      
      velocity=map(velocity,0,100,77,180);                // ???? Change velocity form 0.5 to 77
      throttle.write(velocity);                                       // ???? Full speed backward
      delay(500);
      digitalWrite(6,LOW);
      steering.write(90);                                              // ???  Did you mean "DETACH"???
      throttle.attach(6); 
      throttle.write(0);                                                 ///  ???  BEYOND FULL SPEED BACKWARD?!?!?
      delay(2000);
            velocity=map(velocity,0,100,77,180);                   //??? Now change velocity from 77 to 156  (77+(0.77*(180-77)))
            throttle.attach(6);
            throttle.write(velocity);                                          // ???  Forward at 156
            delay(500);
            digitalWrite(6,LOW);
            steering.write(90);
            delay(2000);