SEGUIDOR DE LINEA
REALIZA TU SEGUIDOR DE LINEA CON ARDUINO
DIAGRAMA DE CONEXION
CODIGO
//sensor infrarojo
int reflex1=8; //IZQUIERDO
int reflex2=2 ; //DERECHO
//motores parte derecha
int G1motorA1= A0; //IN1
int G1motorA2= A1; //IN2
int G1motorB1= A2; //IN3
int G1motorB2= A3; //IN4
//motores parte izquierda
int G2motorA1= 5; //IN2
int G2motorA2= 4; //IN1
int G2motorB1= 7; //IN4
int G2motorB2= 6; //IN3
int piezo = A4;
int led_izquierda= 0;
int led_centro= 1;
int led_derecha= A5;
int echo = 12;
int trigger =13;
int PWM1= 10;//llanta derecha
int PWM2= 11;//llanta derecha
int PWM3= 9;//llanta izquierda
int PWM4= 3;//llanta izquierda
void setup()
{
pinMode(reflex1, INPUT);
pinMode(reflex2, INPUT);
pinMode(G1motorA1, OUTPUT);
pinMode(G1motorA2, OUTPUT);
pinMode(G1motorB1, OUTPUT);
pinMode(G1motorB2, OUTPUT);
pinMode(G2motorA1, OUTPUT);
pinMode(G2motorA2, OUTPUT);
pinMode(G2motorB1, OUTPUT);
pinMode(G2motorB2, OUTPUT);
pinMode(piezo,OUTPUT);
digitalWrite(piezo,HIGH);
pinMode(led_izquierda, OUTPUT);
pinMode(led_centro, OUTPUT);
pinMode(led_derecha, OUTPUT);
pinMode(echo, INPUT);
pinMode(trigger, OUTPUT);
//Serial.begin(9600);
}
void loop()
{
long t;
long d;
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
t= pulseIn(echo,HIGH);
d= t/59;//distancia total
if (d>= 10)
{
digitalWrite (led_centro, HIGH);
digitalWrite (led_derecha, LOW);
digitalWrite (led_izquierda, LOW);
//MOVIMIENTO A LA IZQUIERDA
if (digitalRead(reflex1)==HIGH && digitalRead(reflex2)==LOW)
{ //blanco LOW, negro HIGH
analogWrite(PWM1,200);
analogWrite(PWM2,200);
analogWrite(PWM3,200);
analogWrite(PWM4,200);
digitalWrite (G1motorA1, HIGH);
digitalWrite (G1motorA2, LOW);
digitalWrite (G1motorB1, HIGH);
digitalWrite (G1motorB2, LOW);
digitalWrite (G2motorA1, LOW);
digitalWrite (G2motorA2, HIGH);
digitalWrite (G2motorB1, LOW);
digitalWrite (G2motorB2, HIGH);
//Serial.println("derecha");
digitalWrite (led_derecha, HIGH);
digitalWrite (led_izquierda, LOW);
digitalWrite (led_centro, LOW);
}
//MOVIMIENTO A LA DERECHA
if (digitalRead(reflex1)==LOW && digitalRead(reflex2)==HIGH)
{
analogWrite(PWM1,200);
analogWrite(PWM2,200);
analogWrite(PWM3,200);
analogWrite (PWM4,200);
digitalWrite (G1motorA1, LOW);
digitalWrite (G1motorA2, HIGH);
digitalWrite (G1motorB1, LOW);
digitalWrite (G1motorB2, HIGH);
digitalWrite (G2motorA1, HIGH);
digitalWrite (G2motorA2, LOW);
digitalWrite (G2motorB1, HIGH);
digitalWrite (G2motorB2, LOW);
digitalWrite (led_izquierda, HIGH);
digitalWrite (led_centro, LOW);
digitalWrite (led_derecha, LOW);
//Serial.println("izquierda");
}
// CONTINUA AMBOS SENSORES EN BLANCO
if (digitalRead(reflex1)==LOW && digitalRead(reflex2)==LOW)
{
analogWrite(PWM1,150);
analogWrite(PWM2,150);
analogWrite(PWM3,150);
analogWrite(PWM4,150);
digitalWrite (G1motorA1, HIGH);
digitalWrite (G1motorA2, LOW);
digitalWrite (G1motorB1, HIGH);
digitalWrite (G1motorB2, LOW);
digitalWrite (G2motorA1, HIGH);
digitalWrite (G2motorA2, LOW);
digitalWrite (G2motorB1, HIGH);
digitalWrite (G2motorB2, LOW);
}
// RETROCEDE, AMBOS SENSORES EN NEGRO
if (digitalRead(reflex1)==HIGH && digitalRead(reflex2)==HIGH)
{
analogWrite(PWM1,150);
analogWrite(PWM2,150);
analogWrite(PWM3,150);
analogWrite(PWM4,150);
digitalWrite (G1motorA1, LOW);
digitalWrite (G1motorA2, HIGH);
digitalWrite (G1motorB1, LOW);
digitalWrite (G1motorB2, HIGH);
digitalWrite (G2motorA1, LOW);
digitalWrite (G2motorA2, HIGH);
digitalWrite (G2motorB1, LOW);
digitalWrite (G2motorB2, HIGH);
//Serial.println("centro");
}
}
else//se apagan todos los motores
{
digitalWrite (G1motorA1, LOW);
digitalWrite (G1motorA2, LOW);
digitalWrite (G1motorB1, LOW);
digitalWrite (G1motorB2, LOW);
digitalWrite (G2motorA1, LOW);
digitalWrite (G2motorA2, LOW);
digitalWrite (G2motorB1, LOW);
digitalWrite (G2motorB2, LOW);
digitalWrite(piezo,LOW);
delay(1000);
digitalWrite(piezo,HIGH);
}
}
Comentarios
Publicar un comentario