Hola, tengo un problema...tengo un proyecto para un seguidor de linea, ahora mismo he conseguido dos codigos, uno donde controlo 2 motores a 6 pines, con el drive L298N y el otro donde hago una prueba del sensor QTR-8RC .
En la prueba del sensor, me arroja valores desde 0 hasta 7000, el valor aproximado para que la linea este al centro del sensor es de 3500. mientras mas se aproxima la linea al extremo izquierdo del sensor los valores tienden a cero, y mientras mas a la derecha se aproximen tienden a 7000. quisiera ayuda para poder combinar estos dos codigos para que junto con el pwm pueda dar seguimiento a la linea en curvas, zig-zag, giros de 90 grados, 8´s . etc. Dejare mis dos codigos para ver si pueden ayudarme a montarlos. MUCHAS GRACIAS
ESTE ES EL DE MOTORES:
// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;
void setup()
{
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
demoOne();
delay(1000);
demoTwo();
delay(1000);
}
void demoOne()
{
// this function will run the motors in both directions at a fixed speed
// ENCIENDE MOTOR A
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 200); // Establece la velocidad a 200. (Puede estar entre 0~255)
// ENCIENDE MOTOR B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, 200); // Establece la velocidad a 200. (Puede estar entre 0~255)
delay(2000);
// Cambia sentido de giro de ambos motores
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
// Apaga los dos motores
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void demoTwo()
{
// this function will run the motors across the range of possible speeds
// note that maximum speed is determined by the motor itself and the operating voltage
// the PWM values sent by analogWrite() are fractions of the maximum speed possible
// by your hardware
// turn on motors
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// ACELERA DESDE CERO HASTA LA VELOCIDAD MÁXIMA (0~300)
for (int i = 0; i < 301; i++)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
// DECELERA DESDE VELOCIDAD MÁXIMA HASTA CERO (0~300)
for (int i = 300; i >= 0; --i)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
ESTE ES EL DEL SENSOR QTR-8RC
#include <QTRSensors.h>
#define NUM_SENSORS 8 // numero de sensores usados
#define TIMEOUT 2500 // esperar 2.5 ms para tener una respuesta del sensado
#define EMITTER_PIN 6 // este pin controla el led on del los sensores (enciende y apaga)
//aqui se pone el numero de los pines conectados a los sensores
QTRSensorsRC qtrrc((unsigned char[]) {19, 18, 17, 16, 15, 14, 11, 12},
NUM_SENSORS, TIMEOUT, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];
void setup()
{
delay(500);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH); //este led encendido indica que comienza la calibracion
for (int i = 0; i < 200; i++) // la calibracion se lleva a cabo por 5 segundos
{
qtrrc.calibrate(); // funcion para calibrar los sensores
}
digitalWrite(13, LOW); // se paga el led para que indique que termino la calibracion
// imprime la calibracion minima de los sensores
Serial.begin(9600);
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtrrc.calibratedMinimumOn[i]);
Serial.print(' ');
}
Serial.println();
// imprime la calibracion maxima de los sensores
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtrrc.calibratedMaximumOn[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
// To get raw sensor values, call:
// qtrrc.read(sensorValues); instead of unsigned int position = qtrrc.readLine(sensorValues);
unsigned int position = qtrrc.readLine(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 (unsigned char i = 0; i < NUM_SENSORS; i++)
{
Serial.print(sensorValues[i]);
Serial.print('\t');
}
// Serial.println(); // uncomment this line if you are using raw values
Serial.println(position); // comment this line out if you are using raw values
delay(50);
}