I have a proyect that simulates a parking lot, I have a photoresistor and a button, the button simulates the ticket and the photoresistor the car. My goal is that as soon as I stop pressing the button the counter goes up or down but if there is still a presence (finger on photoresistor) the led is still on.
I keep having problems trying to up my counter while I still have my finger on photo, the counter goes up until after I remove my finger from the photo and not when I stop pressing the button.
Small portion of code where the problem is:
**estadoTent is state of button,
**estadoUltimoEnt is last state of this button
**valPresEnt is the state of the photo
if (estadoTEnt != estadoUltimoEnt) {
if((valPresEnt<25) && (estadoTEnt == HIGH) && contador<cupoMax){
contador++; //counter goes up
while(valPresEnt<25){
digitalWrite(ledPlumaAbierta,HIGH);// LEd turns on
valPresEnt=analogRead(presenciaEnt); //reads photo
}
}
}
ALL THE CODE:
#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);
// se inicializan pushbuttons como inputs
pinMode(botonTarjEnt, INPUT);
pinMode(botonTarjSal, 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))
{
while(valPresSal<25){
valPresSal=analogRead(presenciaSal);
digitalWrite(ledPlumaCerrada,HIGH);//Led se prende
}
contador=contador-1; //contador se decrementa
}
}
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 :)");
}
}