martes, 24 de mayo de 2016

Aqui tenemos el programa ya terminado

#define LED 13
#define pinIR1 3
#define pinIR2 2
boolean val;
int IR = 1;
int IR2 = 1;
int trigPin = 8;
int echoPin = 9;
int trigPin2 = 6;
int echoPin2 = 7;

long duration; //tiempo de ida/vuelta
long duration2;
int cm = 0; //Para almacenar el valor obtenido en cm valor=0
int cm2 = 0;
#include <Servo.h>
Servo myservo; // crea un objeto tipo servo para controlar el servo


/* YourDuino.com Example Software Sketch
 16 character 2 line I2C Display
 Backpack Interface labelled "A0 A1 A2" at lower right.
 ..and
 Backpack Interface labelled "YwRobot Arduino LCM1602 IIC V1"
 MOST use address 0x27, a FEW use 0x3F
 terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <Wire.h>  // Comes with Arduino IDE
// Get the LCD I2C Library here:
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>

/*-----( Declare Constants )-----*/
/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 16 chars 2 line display
// A FEW use address 0x3F
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

/*-----( Declare Variables )-----*/
//NONE

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  myservo.attach(10); //conecta al pin 10
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);


  lcd.begin(16,2);   // initialize the lcd for 16 chars 2 lines, turn on backlight

// ------- Quick 3 blinks of backlight  -------------
  for(int i = 0; i< 3; i++)
  {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight(); // finish with backlight on 

//-------- Write characters on the display ------------------
// NOTE: Cursor Position: (CHAR, LINE) start at 0 
  lcd.setCursor(0,0); //Start at character 4 on line 0
  lcd.print("Bienvenido");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("Al Parking");
  delay(2000); 

// Wait and then tell user they can start the Serial Monitor and type in characters to
// Display. (Set Serial Monitor option to "No Line Ending")
    
  lcd.clear();
  lcd.setCursor(0,0); //Start at character 0 on line 0
  lcd.print("IES CPerezPastor");
  lcd.setCursor(0,1);
   
}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
 {
      IR = digitalRead(pinIR1);// digital interface will be assigned a value of 3 to read val
      IR2 = digitalRead(pinIR2);
     
     // Ultrasonido 1--------------------
        //ponemos al trigger a low para activarlo después
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      //Activar el módulo con un pulso de 10microsec.
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      //Esperamos la llegada de un pulso HIGH
      duration = pulseIn(echoPin, HIGH); // valor del pulso alto en microsegundos
      //tiempo de ida y vuelta, dividimos por 2
      duration=duration/2;
      //La velocidad del sonido es de 340 m/s
      //El pin echo entrega un pulso alto
      //de una duración de 29 microsegundos
      //por cada centímetro de distancia
      //a la que esté el objeto.
      cm = duration/ 29;
       // Nos aseguramos de que el siguiente trigger
      // y el echo actual no se mezclen (recomiendan 50 ms).
       delay(50);
    //fin Ultrasonido 1---------------------------
   
    // Ultrasonido 2--------------------
        //ponemos al trigger a low para activarlo después
      digitalWrite(trigPin2, LOW);
      delayMicroseconds(2);
      //Activar el módulo con un pulso de 10microsec.
      digitalWrite(trigPin2, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin2, LOW);
      //Esperamos la llegada de un pulso HIGH
      duration2 = pulseIn(echoPin2, HIGH); // valor del pulso alto en microsegundos
      //tiempo de ida y vuelta, dividimos por 2
      duration2=duration2/2;
      //La velocidad del sonido es de 340 m/s
      //El pin echo entrega un pulso alto
      //de una duración de 29 microsegundos
      //por cada centímetro de distancia
      //a la que esté el objeto.
      cm2 = duration2/ 29;
       // Nos aseguramos de que el siguiente trigger
      // y el echo actual no se mezclen (recomiendan 50 ms).
       delay(50);
     
      if (cm < 9 || cm2 < 9 )// Cualquiera de los dos detectan coche
      {
        myservo.write(90);
        delay(2000);
      }
     
      if (cm > 9 && cm2 > 9) // Ambos no detectan coches
      {
        myservo.write(0);
        //delay(3000);
      }

      if ((IR == 0 && IR2 == 1) || (IR == 1 && IR2 == 0)){
      lcd.clear();
      lcd.setCursor(0,0); //Start at character 0 on line 0
      lcd.print("Pl. libres = 1");
      }
      if ((IR == 1 && IR2 == 1)){
      lcd.clear();
      lcd.setCursor(0,0); //Start at character 0 on line 0
      lcd.print("Pl. libres = 2");
      }
      if ((IR == 0 && IR2 == 0)){
      lcd.clear();
      lcd.setCursor(0,0); //Start at character 0 on line 0
      lcd.print("Pl. libres = 0");
     

      }

      Serial.println(cm);
      Serial.println(cm2);
      Serial.println(IR);
      Serial.println(IR2);
      delay(500);
     
}/* --(end main loop )-- */


/* ( THE END ) */

No hay comentarios:

Publicar un comentario