Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
3.4 kB
2
Indexable
Never
#include <QTRSensors.h> 

QTRSensors qtr;

const uint8_t SensorCount = 7;
uint16_t sensorValues[SensorCount];
//motor control pin

int rightMotorAIN1=A1;
int rightMotorAIN2=A2;
int leftMotorBIN1=A3;
int leftMotorBIN2=A4;
int PWM1=10;
int PWM2=11;
float maxSensorVal=6000;
float minSensorVal=0;
float setMid= (maxSensorVal-minSensorVal)/2;    //for 7 array sensor, reading starting from 0-6000
float maxSpeed=255;
double PWMVal=(maxSpeed/setMid);
int dt=300;

void setup()
{
  // configure the sensors
  
  qtr.setTypeRC();
  qtr.setSensorPins((const uint8_t[]){2, 3, 4, 5, 6, 7, 8}, SensorCount);
  qtr.setEmitterPin(2);
  
  pinMode(rightMotorAIN1,OUTPUT);
  pinMode(rightMotorAIN2,OUTPUT);
  pinMode(PWM1,OUTPUT);
  
  pinMode(leftMotorBIN1,OUTPUT);
  pinMode(leftMotorBIN2,OUTPUT);
  pinMode(PWM2,OUTPUT);
  

  delay(500);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  for (uint16_t i = 0; i < 400; i++)
  {
    qtr.calibrate();
  }
  digitalWrite(LED_BUILTIN, 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(38400);
  Serial.println("Wait calibrating");
  for (uint8_t i = 0; i < SensorCount; i++)
  {
    Serial.print(qtr.calibrationOn.minimum[i]);
    Serial.print(' ');
  }
  Serial.println();

  // print the calibration maximum values measured when emitters were on
  for (uint8_t i = 0; i < SensorCount; i++)
  {
    Serial.print(qtr.calibrationOn.maximum[i]);
    Serial.print(' ');
  }
  Serial.println();
  Serial.println();
  delay(1000);
}


void loop()
{
  // read calibrated sensor values and obtain a measure of the line position
  // from 0 to 5000 (for a white line, use readLineWhite() instead)
  uint16_t position = qtr.readLineBlack(sensorValues);

  // print the sensor values as numbers from 0 to 1000, where 0 means maximum
  // reflectance and 1000 means minimum reflectance, followed by the line
  // position
  for (uint8_t i = 0; i < SensorCount; i++)
  {
    Serial.print(sensorValues[i]);
    Serial.print('\t');
  }
  Serial.println(position);
 /* for(int i=setMid;i<=maxSensorVal;i++)
  {
  double PWMR=maxSpeed-PWMVal*(setMid-position);  // for the right motor power value
  digitalWrite(rightMotorAIN1, HIGH);
  digitalWrite(rightMotorAIN2, LOW);
  Serial.print("PWMR value ");
  Serial.println(PWMR);
  analogWrite(PWM1,PWMR);
  
  }*/
  
  if(position>=setMid && position<=maxSensorVal){
    double PWMR=maxSpeed-PWMVal*(position-setMid);  // for the right motor power value
    Serial.print("PWMR value ");
  Serial.print(PWMR);
  digitalWrite(rightMotorAIN1, HIGH);
  digitalWrite(rightMotorAIN2, LOW);
  analogWrite(PWM1,PWMR);
  }
  
  if(position>=0 && position<=setMid){
    double PWML=maxSpeed-PWMVal*(setMid-position);
    Serial.print("PWML value");
    Serial.println(PWML);
    digitalWrite(leftMotorBIN1, HIGH);
    digitalWrite(leftMotorBIN2, LOW);
    analogWrite(PWM2,PWML); 
  }
  
//  delay(dt);
  
  
  /*for(int j=0;j<=setMid;j++)
  {
    double PWML=maxSpeed-PWMVal*(position-setMid);  //for the left motor power value
    digitalWrite(leftMotorBIN1, HIGH);
    digitalWrite(leftMotorBIN2, LOW);
    analogWrite(PWM2, PWML);
    
  }*/
}