Hello, I am simulating a parking lot, basically I have a proyect that simulates a parking lot, I have a photoresistor and a button, the button simulates the card to get in and the photoresistor simulates the presence of a car.
A led will light up if I press the card button and cover the photresistor (car presence) meaning that the door opens, but it still should be on if I stop pressing the button since the led turns off until after the photo is not covered. The counter has to also go up but the thing is it has to go up as soon as I remove my finger from JUST the button. If I still have my finger covering the photresistor the led must stay on.
My problem is that the led works fine (when I stop covering the photoresistor led goes down, meaning there is no car) but my counter also goes up until AFTER I remove my finger from the photoresistor, it should go up after I stopped pressing the button which simulates a card.
I think the problem is here:
if((valPresEnt<25) && (estadoTEnt == HIGH) && contador<cupoMax){
conador++:
contador++;
while(valPresEnt<25){
digitalWrite(ledPlumaAbierta,HIGH);//LEd se prende.
valPresEnt=analogRead(presenciaEnt); //vuelve a leer fotoresistor, actualiza presencia
}
}
else
{
digitalWrite(ledPlumaAbierta,LOW);///si no se cumplen las condiciones se apaga
}
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int presenciaEnt=A0; // A0 tiene el fotoresistor de Entrada
int presenciaSal=A1; // A1 tiene fotoresistor de Salida
int valPresEnt= 0; //valores que tomaran los fotoresistores
int valPresSal= 0;
const int botonTarjEnt = 9; // numero del pin del boton
const int botonTarjSal = 13;
const int ledPlumaCerrada = 6; //numero del pin del LED pluma cerrada
const int ledPlumaAbierta = 7; //numero del pin del LED pluma abierta
const int ledCupo = 10; //numero del pin del LED cuando hay cupo
const int ledSinCupo = 8; //numero del pin del LED cuando no hay cupo
int cupoMax = 15;
int estadoTEnt = 0; // variable para leer estado de pushbuttons
int estadoTSal = 0;
int contador = 0; // contador de carros ingresados
int estadoUltimoSal = 0; // ultimo estado del boton salida
int estadoUltimoEnt = 0; // ultimo estado del boton entrada
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
// se inicializan LEDS como salidas
pinMode(ledPlumaCerrada,OUTPUT);
pinMode(ledPlumaAbierta,OUTPUT);
pinMode(ledCupo,OUTPUT);
pinMode(ledSinCupo,OUTPUT);
// se inicializan pushbuttons y fotoresistor como inputs
pinMode(botonTarjEnt, INPUT);
pinMode(botonTarjSal, INPUT);
pinMode(presenciaEnt, INPUT);
pinMode(presenciaSal, INPUT);
}
void loop() {
delay(200); //pequeña pausa entre cada pulsación y led
valPresEnt=analogRead(presenciaEnt); //Lee valor del foto de Salida
valPresSal=analogRead(presenciaSal); //Lee valor del foto de Entrada
estadoTEnt = digitalRead(botonTarjEnt); //lee el estado de los botones y lo guarda
estadoTSal = digitalRead(botonTarjSal);
if (estadoTEnt != estadoUltimoEnt) { //si el estado ultimo es igual al del presente no hace nada
// esto quiere decir que no se ha soltado el boton
//si no son iguales ya se dejo de presionar y hace lo siguiente:
//si hay presencia (fotoresistor sin luz, valor pequeño) y el boton de entrada se presiona y
// todavia no llega el cupo a 15 el led abierto se prende
if((valPresEnt<25) && (estadoTEnt == HIGH) && contador<cupoMax){
contador++;
while(valPresEnt<25){
digitalWrite(ledPlumaAbierta,HIGH);//LEd se prende.
valPresEnt=analogRead(presenciaEnt); //vuelve a leer fotoresistor, actualiza presencia
}
}
}
else
{
digitalWrite(ledPlumaAbierta,LOW);///si no se cumplen las condiciones se apaga
}
//se actualiza el ultimo estado del boton
estadoUltimoEnt = estadoTEnt;
Serial.print("Foto1: ");
Serial.print(valPresEnt);
Serial.print(" ");
Serial.print(valPresSal);
//si hay presencia y el boton de salida se presiona y
//el contador no es menor a 0 el led cerrado se prende
if (estadoTSal != estadoUltimoSal) {
if((valPresSal<25) && (estadoTSal == HIGH) && (contador > 0))
{
contador--; //contador se decrementa
while(valPresSal<25){
digitalWrite(ledPlumaCerrada,HIGH);//Led se prende
valPresSal=analogRead(presenciaSal);
}
}
}
else
{
digitalWrite(ledPlumaCerrada,LOW);//led se apaga
}
estadoUltimoSal = estadoTSal; //actualiza estado
/// codigo de cupo
if(contador == cupoMax){
digitalWrite(ledSinCupo,HIGH);//Led de cupo max enciende
} else{
digitalWrite(ledSinCupo,LOW);//Led cupo max se mantiene apagado
}
if(contador < cupoMax){
digitalWrite(ledCupo,HIGH);//Led de cupo max enciende
} else{
digitalWrite(ledCupo,LOW);//Led cupo max se mantiene apagado
}
lcd.setCursor(0,0);
lcd.print(" ");
lcd.print("Contador : "); //despliega la palabra contador en la primera linea
lcd.setCursor(10,0); //despliega el contador despues de la palabra
lcd.print(contador);
if(contador == cupoMax){ //va actualizando el cupo dependiendo del contador
lcd.setCursor(0,1); //despliega el cupo en la segunda linea
lcd.print("No hay cupo :(");
}
else{
lcd.setCursor(0,1);
lcd.print("Si hay cupo :)");
}
}