NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <QTRSensors.h>

#define NUM_SENSORS 6 // number of sensors used
#define NUM_SAMPLES_PER_SENSOR 4 // average 4 analog samples per sensor reading
#define EMITTER_PIN 2 // emitter is controlled by digital pin 2

QTRSensorsAnalog qtra((unsigned char[]) {0, 1, 2, 3, 4, 5, 6, 7},
NUM_SENSORS, NUM_SAMPLES_PER_SENSOR, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];

#define PWM_LEFT 11
#define PWM_RIGHT 9
#define LEFT_UP 6
#define LEFT_DOWN 8
#define RIGHT_UP 3
#define RIGHT_DOWN 10
#define SHARP D4 //DEĞİŞTİR


int BASE_SPEED = 60;
int max_speed = 255;
#define KP 30
#define KD 10
#define KI 0

void setLeftDirection(bool);
void setRightDirection(bool);

float TIME_DIFF;
float total_distance_error = 0;
float last_distance_error = 0;

int beyaz = 0;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////// S E T U P /////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup() {
pinMode(PWM_LEFT, OUTPUT);
pinMode(PWM_RIGHT, OUTPUT);
pinMode(LEFT_UP, OUTPUT);
pinMode(LEFT_DOWN, OUTPUT);
pinMode(RIGHT_UP, OUTPUT);
pinMode(RIGHT_DOWN, OUTPUT);
//pinMode(SHARP, INPUT);

digitalWrite(13, HIGH); // turn on Arduino's LED to indicate we are in calibration mode
for (int i = 0; i < 200; i++) // make the calibration take about 10 seconds
{
qtra.calibrate(); // reads all sensors 10 times at 2.5 ms per six sensors (i.e. ~25 ms per call)
}
digitalWrite(13, LOW); // turn off Arduino's LED to indicate we are through with calibration

// print the calibration minimum values measured when emitters were on
Serial.begin(9600);
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtra.calibratedMinimumOn[i]);
Serial.print(' ');
}
Serial.println();

// print the calibration maximum values measured when emitters were on
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtra.calibratedMaximumOn[i]);
Serial.print(' ');
}
Serial.println();
Serial.println();
delay(1000);

TIME_DIFF = millis();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////// L O O P //////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void loop() {

float average_distance = 2500;
//checkifwhite(); //Zemin rengini kontrol ediyor.
beyaz = 0;

unsigned int distance = qtra.readLine(sensorValues,1,beyaz); //beyaz = 0 ise beyaz üzerne siyah zemin, 1 ise tersi.
float distance_error = (distance - average_distance) / average_distance;
Serial.println(distance);

//Serial.println(front_distance);
TIME_DIFF = millis() - TIME_DIFF;
if (TIME_DIFF == 0){
TIME_DIFF = 0.001;
}
TIME_DIFF /= 1000;
/*TIME_DIFF -= millis(); //ÖNCEKİ KOD
TIME_DIFF /= -1000;
TIME_DIFF += 0.1;*/
float error_derivative = (distance_error - last_distance_error) / TIME_DIFF;


setLeftDirection(true);
setRightDirection(true);

float PID = (KP * distance_error) + (KD * error_derivative) + (KI * total_distance_error);
int left_speed = BASE_SPEED + PID;
int right_speed = BASE_SPEED - PID;

if (left_speed > max_speed){
left_speed = max_speed;
}
else if (left_speed < 0){
left_speed = 0;
}
if (right_speed > max_speed){
right_speed = max_speed;
}
else if (right_speed < 0){
right_speed = 0;
}

Serial.print(left_speed);
Serial.print(", ");
Serial.print(right_speed);
Serial.print(" ----------- ");
Serial.print(distance_error);
Serial.print(", ");
Serial.println(error_derivative);

analogWrite(PWM_LEFT, left_speed);
analogWrite(PWM_RIGHT, right_speed);

last_distance_error = distance_error;
total_distance_error += distance_error;

TIME_DIFF = millis();
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////// F U N C T I O N S /////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int checkifwhite()
{
if (sensorValues[0] > 500 && sensorValues[1] > 500 && sensorValues[3] < 500 && sensorValues[5] > 500 && sensorValues[6] > 500){

Serial.print("Siyah üstünde beyaz çizgi");
beyaz = 1;
return;
} else if (sensorValues[0] < 500 && sensorValues[1] < 500 && sensorValues[3] > 500 && sensorValues[5] < 500 && sensorValues[6] < 500){
Serial.print("Beyaz üstünde siyah çizgi");
beyaz = 0;
return;
} else{
Serial.print("Degisim yok");
return;
}
}

void setLeftDirection(bool is_forward){
if (is_forward){
digitalWrite(LEFT_UP, HIGH);
digitalWrite(LEFT_DOWN, LOW);
} else{
digitalWrite(LEFT_UP, LOW);
digitalWrite(LEFT_DOWN, HIGH);
}
}

void setRightDirection(bool is_forward){
if (is_forward){
digitalWrite(RIGHT_UP, HIGH);
digitalWrite(RIGHT_DOWN, LOW);
} else{
digitalWrite(RIGHT_UP, LOW);
digitalWrite(RIGHT_DOWN, HIGH);
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.