[solved] counting a pulse from a coin acceptor and displaying count on an lcd

so what i have set up is a coin acceptor hooked up to my arduino, and i also have a display hooked up. what i would like it to do is display the number of coins that have been inserted, with a way to clear that total. so i figured out how to simply increment it, but because it also has a delay for it, it does not increment again if another coin is inserted within that 5 seconds.

below is what i have so far

#include <LiquidCrystal.h>

int credit = 0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
  //start serial connection
  Serial.begin(9600);
  //configure pin2 as an input and enable the internal pull-up resistor
  pinMode(8, INPUT_PULLUP);
  pinMode(9, OUTPUT); 
  lcd.begin(16, 2);
  lcd.print("credits:");
}

void loop(){
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(8);
  //print out the value of the pushbutton
  Serial.println(sensorVal);
  
  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on pin 13 when the 
  // button's pressed, and off when it's not:
  if (sensorVal == HIGH) {
    digitalWrite(9, LOW); 
  
    
   
    
  } 
  else {
    digitalWrite(9, HIGH);
      credit = credit +1;
    lcd.setCursor(9, 0);
     lcd.print(credit);
   delay(5000); \\delay for keeping lcd backlight on for five seconds after a coin is inserted.


    
  }
  
}

whats the question

and 5 seconds, wow I would pump 2 dollars into those things when I was a kid in 20 seconds

oh heheh yeah i didnt really address that. my question is, when i have the delay to keep the lcd light on for 5 seconds, it will not update the count of coins inserted if another one is inserted while the light is still on..

Correct, the program will be stuck in that delay (5000) line. You could reduce that to delay (50) and have another counter variable that you set to 100 when a coin is detected. Then after each delay, decrement the counter. When it hits zero, switch the led off.

Paul

Or remove calls to “delay()” completely, and take a look at the blink without delay example.

okay so i looked at the blink without delay, and it would be turning it on at set intervals, not conditionaly when a pin goes low. (in my case, that is. using internal pullup.) and Paul, i did try something like that, but because of the way it is turned on, it will not stay on. as soon as the input pin returns to high, the led will turn off.

i am now thinking of doing this with hardware rather than software, just because of the method used to initially turn it on.

the other thing i was wondering if it would work, was using do while statements instead of if then statements.

so like have the do portion be triggered by the input pin going low, which would increment the credit variable, and also write that variable to the screen, while making output pin 9 high for a number of seconds. would doing it that way not delay the entire loop?

Okay. so with the help of a friend who is a computer programmer, we came up with this:

#include <LiquidCrystal.h>

int credit = 0;
int counter = -1;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
  //start serial connection
  Serial.begin(9600);
  //configure pin2 as an input and enable the internal pull-up resistor
  pinMode(8, INPUT_PULLUP);
  pinMode(9, OUTPUT); 
  lcd.begin(16, 2);
  lcd.print("credits:");
}

void loop(){
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(8);
  //print out the value of the pushbutton
  Serial.println(sensorVal);
  
  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on pin 13 when the 
  // button's pressed, and off when it's not:
  if (sensorVal == HIGH) {
    
    if (counter < 100 and credit  >0) {
    digitalWrite(9, HIGH);
      delay(50);
  }
  else {
    digitalWrite(9, LOW);
  }
    counter = counter +1;
  } 
  else {
    digitalWrite(9, HIGH);
    credit = credit +1;
    lcd.setCursor(9, 0);
     lcd.print(credit);
    delay(50);
    counter = 0;
    
  }
  }

which this does exactly what i want it to do. looking at the code should be pretty self explanatory.
thanks for all who helped.