Led+counter+button

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 :)");
  }
}

Could you rephrase your issue and post the entire code?

my code is in spanish but I think I refrased it right, I still added the code :slight_smile: I hope you understand :frowning:

That code doesn't compile.

consider the following code
however, i think the logic is flawed (should issueing a new card be contingent on the previous car having passed)

#define pinCard A1
#define pinCar  A2
#define pinLed  13

#define CarThresh  25

enum { Off = HIGH, On = LOW };

byte card;
byte cardLst = Off;
byte car;

int  cardCount = 0;
int  carCount  = 0;

char s [80];

void loop (void) {
    card = digitalRead (pinCard);
    car  = analogRead  (pinCar);

    if (cardLst != card)  {
        cardLst = card;
        if (On == card)  {
            cardCount++;

            sprintf (s, "    %3d cardCount", cardCount);
            Serial.println (s);
        }
    }

    if (carCount < cardCount & CarThresh > car)  {
        digitalWrite (pinLed, On);
        carCount++;

        sprintf (s, " %3d    carCount", carCount);
        Serial.println (s);
    }

    else if (CarThresh < car)
        digitalWrite (pinLed, Off);
}

// -----------------------------------------------------------------------------
void setup (void) {
    Serial.begin (9600);

    digitalWrite (pinLed, Off);
    pinMode (pinLed, OUTPUT);
}

This is same problema?

RV mineirin

it compiles now! sorry I added the version I was editing

Could you rephrase you issue? This looks like your 2nd attempt to fix the issue. Suppose, if people had understood the issue the first time around then there would not be a 2nd time.

Just a guess but if several states must be met for the button led to glow than would the removal of all states be the reason why to make the led be a no glow?

To me, once the button is pressed and the light is blocked, then led comes on. If light remains blocked after the button press then the led should remain on, that's the condition you want to write for.

yes and thats how it works, what I want to do is up the counter after I stop pressing the button and before Iight stops being blocked (after i remove my finger), the counter goes up after giving it light not when I stop pressing the button at the same time.

You can detect a button going from HIGH to LOW? You can detect a button going from LOW to HIGH?

A state can be light and no button press, lets call no button press a HIGH and no light is a HIGH.

When no light and button press is LOW/LOW == LED ON.

When no light is a LOW and button goes from HIGH to LOW increment counter and LED stays on.

That's what you want?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.