#include <QTRSensors.h>
#define sagtabanhiz 200
#define soltabanhiz 200
#define sagmotoryon 13
#define sagmotorpwmpin 11
#define solmotoryon 12
#define solmotorpwmpin 3
int sensor=10;
QTRSensorsRC qtrrc((unsigned char[]){2,4,5,6,7,8,9,10},8,2500,QTR_NO_EMITTER_PIN);
unsigned int sensorValues[8];
void setup()
{
delay(2000);
pinMode(sensor,INPUT);
pinMode(sagmotoryon,OUTPUT);
pinMode(sagmotorpwmpin,OUTPUT);
pinMode(solmotoryon,OUTPUT);
pinMode(solmotorpwmpin,OUTPUT);
int i;
digitalWrite(13,HIGH);
for (int i=0;i<200;i++)
{
if( 0 <=i&&i< 5 ) hafifsagadon();
if( 5 <=i&&i< 15 ) hafifsoladon();
if( 15 <=i&&i< 25 ) hafifsagadon();
if( 25 <=i&&i< 35 ) hafifsoladon();
if( 35 <=i&&i< 45 ) hafifsagadon();
if( 45 <=i&&i< 55 ) hafifsoladon();
if( 55 <=i&&i< 65 ) hafifsagadon();
if( 65 <=i&&i< 75 ) hafifsoladon();
if( 75 <=i&&i< 85 ) hafifsagadon();
if( 85 <=i&&i< 90 ) hafifsoladon();
if(i>=90 ) {
frenle();delay(5);
}
qtrrc.calibrate();
delay(4);
}
digitalWrite(13,LOW);
delay(2000);
Serialbegin(9600);
int sonhata = 0;
float Kp = 0.06;
float Kd = 0.8;
int sagmotorpwm = 0;
int solmotorpwm = 0;
int zemin=0;
void loop()
{
unsigned int sensorValues[8];
unsigned int position = qtrrc.readLine(sensorValues,1,zemin);
int hata = position-3500;
if ( sensorValues[0]<100 && sensorValues[7]<100 ){zemin=0; }
if ( sensorValues[0]>500 && sensorValues[7]>500 ){zemin=1; }
int duzeltmehizi = Kp * hata + Kd*(hata - sonhata);
sonhata = hata;
sagmotorpwm = sagtabanhiz + duzeltmehizi ;
solmotorpwm = soltabanhiz - duzeltmehizi ;
sagmotorpwm = constrain(sagmotorpwm,-100,110);
solmotorpwm = constrain(solmotorpwm,-100,110);
motorkontrol(sagmotorpwm,solmotorpwm);
}
void motorkontrol( int sagmotorpwm, int solmotorpwm){
if(sagmotorpwm<=0){
sagmotorpwm=abs(sagmotorpwm);
digitalWrite(sagmotoryon,LOW);
analogWrite(sagmotorpwmpin, sagmotorpwm);
}
else{
digitalWrite(sagmotoryon,LOW);
analogWrite(sagmotorpwmpin,sagmotorpwm);
}
if(solmootrpwm<=0){
solmotorpwm=abs(solmotorpwm);
digitalWrite(solmotoryon,LOW);
analogWrite(solmotorpwmpin,solmotorpwm);
}
else{
digitalWrite(solmotoryon,HIGH);
analogWrite(solmotorpwmpin,solmotorpwm);
}
}
void frenle(){
motorkontrol(0,0);
}
void hafifsagadon(){
motorkontrol(-100,100);
}
void hafifsoladon(){
motorkontrol(100,-100);
}
}