Created
December 15, 2018 18:47
-
-
Save dbarrerap/5d606b0341682664e98fbc1536f4ecca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sentry Park S.A.V.R | |
* | |
* Diagrama de Conexiones | |
* | |
* Arduino | Componente | |
* 10, 11 | TX, RX HC-06, Comunicacion Bluetooth | |
* 9 | SIG Servo de Entrada | |
* 8 | SIG Servo de Salida | |
* 7 | OUT Sensor de Obstaculo de Entrada | |
* 6 | OUT Sensor de Obstaculo de Salida | |
* 22 - 29 | En pares, Trig, Echo HC-SR04, Sensores de Distancia Parqueos Normales | |
* 34, 35 | Trig, Echo HC-SR04, Sensores de Distancia Parqueos Discapacitados | |
*/ | |
// For I2C LCD: https://bitbucket.org/fmalpartida/new-liquidcrystal/overview | |
#include <Wire.h> | |
#include <LiquidCrystal_I2C.h> | |
#include <SoftwareSerial.h> | |
#include <NewPing.h> | |
#include <Servo.h> | |
SoftwareSerial entradaSerial(10, 11); // RX, TX | |
Servo entradaServo; | |
Servo salidaServo; | |
int obstSensorEntrada = 7; | |
int obstSensorSalida = 6; | |
int obstaculo = HIGH; | |
#define LCD_I2C_ADDR 0x27 | |
LiquidCrystal_I2C lcd(LCD_I2C_ADDR, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); | |
#define SONAR_DELAY 5000 // Tiempo de actualizacion (polling) de Ultrasonicos | |
#define SONAR_DIST 10 | |
#define MAX_DISTANCE 15 | |
#define SONAR_NUM 4 // Cantidad total de Sensores Ultrasonico | |
NewPing sonar[SONAR_NUM] = { // Inicializador de Ultrasonicos (Agregar de acuerdo a SONAR_NUM) | |
NewPing(22, 23, MAX_DISTANCE), // Trigger, Echo | |
NewPing(24, 25, MAX_DISTANCE), | |
NewPing(26, 27, MAX_DISTANCE), | |
NewPing(28, 29, MAX_DISTANCE) | |
}; | |
#define DISCAP_NUM 1 | |
NewPing discap[DISCAP_NUM] = { | |
NewPing(34, 35, MAX_DISTANCE) | |
}; | |
unsigned long prevMillis = 0; | |
bool estac[SONAR_NUM] = {0, 0, 0, 0}; // Estado de Estacionamientos (Modificar de acuerdo a SONAR_NUM) | |
bool disca[DISCAP_NUM] = {0}; // Estado de Estacionamientos (Modificar de acuerdo a DISCAP_NUM) | |
byte disponible; | |
byte dispoTotal = 0; // Cantidad de Estacionamientos disponibles | |
void setup() { | |
// Inicializacion de componentes. | |
lcd.begin(20, 4); | |
lcd.home(); | |
lcd.print("Sentry Park"); | |
lcd.setCursor(0, 1); | |
lcd.print("Cargando"); | |
Serial.begin(9600); | |
lcd.print("."); | |
entradaSerial.begin(9600); | |
lcd.print("."); | |
entradaServo.attach(9); | |
lcd.print("."); | |
entradaServo.write(0); | |
lcd.print("."); | |
salidaServo.attach(8); | |
lcd.print("."); | |
salidaServo.write(0); | |
lcd.print("."); | |
lcd.clear(); | |
lcd.print("Sentry Park"); | |
lcd.setCursor(0, 1); | |
lcd.print("Cargado"); | |
delay(1000); | |
lcd.clear(); | |
} | |
void loop() { | |
// Chequear el estado de los parqueos y actualizar el valor de disponibilidad, general y discapacidad. | |
if (millis() - prevMillis >= SONAR_DELAY) | |
{ | |
prevMillis = millis(); | |
lcd.clear(); | |
lcd.print("Sentry Park"); | |
dispoTotal = 0; | |
disponible = 0; | |
for (int i = 0; i < SONAR_NUM; i++) | |
{ | |
int dist = sonar[i].ping_cm(); | |
estac[i] = !(dist > SONAR_DIST); | |
disponible += estac[i]; | |
} | |
dispoTotal += disponible; | |
lcd.setCursor(0, 2); | |
lcd.print("Disponibles: "); | |
lcd.print(disponible); | |
disponible = 0; | |
for (int i = 0; i < DISCAP_NUM; i++) | |
{ | |
int dist = discap[i].ping_cm(); | |
disca[i] = !(dist > SONAR_DIST); | |
disponible += disca[i]; | |
} | |
dispoTotal += disponible; | |
lcd.setCursor(0, 3); | |
lcd.print("Discapacidad: "); | |
lcd.print(disponible); | |
} | |
// Chequea si se ha recibido la instruccion de abrir la entrada. | |
if (entradaSerial.available()) | |
{ | |
char val = entradaSerial.read(); | |
if (val == 'o') | |
{ | |
// Si ha disponibilidad, abrir la pluma, esperar a que pase el movil y cerrar. | |
// De lo contrario, mostrar mensaje de falta de disponibilidad. | |
if (dispoTotal > 0) | |
{ | |
int angulo; | |
for (angulo = 0; angulo < 90; angulo++) | |
{ | |
entradaServo.write(angulo); | |
delay(15); | |
} | |
lcd.clear(); | |
lcd.print("Bienvenido/a!"); | |
// Wait for 3 seconds | |
unsigned long m = millis(); | |
while (millis() - m <= 3000); | |
do { | |
obstaculo = digitalRead(obstSensorEntrada); | |
} while (obstaculo == LOW); | |
while (millis() - m <= 1000); | |
// Go back to 0 degrees | |
for (angulo = 90; angulo > 0; angulo--) | |
{ | |
entradaServo.write(angulo); | |
delay(15); | |
} | |
} else { | |
lcd.clear(); | |
lcd.print("Lo sentimos."); | |
lcd.setCursor(0,1); | |
lcd.print("No contamos con"); | |
lcd.setCursor(0,2); | |
lcd.print("puestos disponibles"); | |
lcd.setCursor(0,3); | |
lcd.print("al momento."); | |
} | |
} else if (val == 's') { | |
int angulo; | |
for (angulo = 0; angulo < 90; angulo++) | |
{ | |
salidaServo.write(angulo); | |
delay(15); | |
} | |
unsigned long m = millis(); | |
while (millis() - m <= 3000); | |
do { | |
obstaculo = digitalRead(obstSensorSalida); | |
} while (obstaculo == LOW); | |
while (millis() - m <= 1000); | |
// Go back to 0 degrees | |
for (angulo = 90; angulo > 0; angulo--) | |
{ | |
salidaServo.write(angulo); | |
delay(15); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment