Newbie: Arcade Credit System Project, having trouble with initial program display

Hi, I'm making a Coin Operated Arcade Credit system (prototype/simulation).
The idea is that a user inserts a coin, a credit gets added, then a credit is deducted when the "start" button is pressed.

My problem is that I'm having trouble with the start of my program where I'm attempting to loop a flashing message reading "Please Insert Coin" to an I2C LCD Display while waiting for credits/coins to be inserted into the "game", then break out of that loop to display the amount of credits inserted.

When the program is started, the "Please Insert Coin" does not show, nor the loop works.

Thanks

// Arcade Credit counter
// Version 2

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//Variables
int led = 13;
long int x = 0;
int toggleState; // May be buggy
int creditState =0;
int creditPin = 2; // Here may be buggy too


const int coin = 2;
boolean insert = false;
volatile int pulse = 0;
unsigned long balance;



//Declare the LCD Display
LiquidCrystal_I2C lcd(0x27,16,2);



void setup() {
  
  

  
  //Initialise the LCD and Backlight
  lcd.init();
  lcd.backlight(); 

  //Serial Monitor logs the Coins inserted
  Serial.begin(9600);
  Serial.println("Credit: 0");
  
  delay(1000);


  pinMode(creditPin, INPUT_PULLUP); //Bug here maybe
  attachInterrupt(digitalPinToInterrupt(2), coinInterrupt, RISING);
  
  }

  


void loop() {
  

  //Bug here not displaying the "Insert Coin" message
  creditState = digitalRead(creditPin); //Maybe bugs
    
  if (creditState = 0) {
    lcd.setCursor(0,0);
    lcd.print(" Please  Insert");
    lcd.setCursor(6,1);
    lcd.print("Coin");
    delay(1000);
    lcd.clear();
    delay(1000);    
  }
  
    if (insert) {
      lcd.clear();
     insert = false;
     //Serial.println("coin detected!");
     balance+=1;
     Serial.println("Credit: "+(String)balance);
     lcd.print("Credit: "+(String)balance);
     delay(1000);
   }



  
}


  //Runs the code here when coins are inserted into the mech
    //if (insert) {
     //lcd.clear();
     //insert = false;
     //Serial.println("coin detected!");
     //balance+=1;
     //Serial.println("Credit: "+(String)balance);
     //lcd.print("Credit: "+(String)balance);
    // delay(1000);
   //}


//interrupt
void coinInterrupt() {
  pulse++ ;
  insert = true;
}

I'm not going to some random download! Post code here, in < / > tags.
You have not told us what the 'trouble' you are having is! Does it not display the message? does it not flash? Does it not detect the coin? does in not exit the loop? Does it not display the credits?

I'm so sorry, i'm still all new to this.

I've now put in the edits

Not sure if this affects it, but in your code it says "boolean" instead of "bool".

EDIT: I edited the code to correct syntax. (from what I could see):

// Arcade Credit counter
// Version 2

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//Variables
int led = 13;
long int x = 0;
int toggleState; // May be buggy
int creditState =0;
int creditPin = 2; // Here may be buggy too


const int coin = 2;
bool insert = false;
volatile int pulse = 0;
unsigned long balance;



//Declare the LCD Display
LiquidCrystal_I2C lcd(0x27,16,2);



void setup() {
  //Initialise the LCD and Backlight
  lcd.init();
  lcd.backlight(); 

  //Serial Monitor logs the Coins inserted
  Serial.begin(9600);
  Serial.println("Credit: 0");
  
  delay(1000);


  pinMode(creditPin, INPUT_PULLUP); //Bug here maybe
  attachInterrupt(digitalPinToInterrupt(2), coinInterrupt, RISING);
  
  }

  


void loop() {
  

  //Bug here not displaying the "Insert Coin" message
  creditState = digitalRead(creditPin); //Maybe bugs
    
  if (creditState == 0) {
    lcd.setCursor(0,0);
    lcd.print(" Please  Insert");
    lcd.setCursor(6,1);
    lcd.print("Coin");
    delay(1000);
    lcd.clear();
    delay(1000);    
  }
  
  if (insert) {
    lcd.clear();
    insert = false;
    //Serial.println("coin detected!");
    balance+=1;
    Serial.println("Credit: "+String(balance));
    lcd.print("Credit: "+String(balance));
    delay(1000);
  }



  
}


  //Runs the code here when coins are inserted into the mech
    //if (insert) {
     //lcd.clear();
     //insert = false;
     //Serial.println("coin detected!");
     //balance+=1;
     //Serial.println("Credit: "+String(balance));
     //lcd.print("Credit: "+String(balance));
    // delay(1000);
   //}


//interrupt
void coinInterrupt() {
  pulse++;
  insert = true;
}

So you now make me look like a liar! Good luck, I'm out! Idiot.

Several problem

First, this is an assignment, not a comparison.

  if (creditState = 0) {

Second, creditPin is set to INPUT_PULLUP, the normal state will be HIGH, the triggered state will be LOW (which would be common with a coin acceptor with an open-collector or relay output). Checking for 0 (LOW) would then print the message after the coin was inserted, if you happen to be checking the input at the exact instant that was occurring.

Third, you have two one-second delays, that can make the input very non-responsive.

Somewhat unusual to be checking an input in the code, while using the same input for an interrupt. Since you already have the insert flag to indicate a coin insertion, just check that to see if you should display the message.

Thanks for the code update, It seems to now presents the "Please Insert Coin" message after a coin has been inserted and changes to display the credits currently in.

So it worked?

It now displays the message, however it's not the outcome i wanted.

I want the message to display while the program is waiting for credits to be inserted (or if all credits have been "used")

It now displays the message after a coin has been inserted, then with the credit count to follow. It seems like the problem was definitely with if (creditState = 0) not being a comparison.

However I am unable to figure out how to get the "please insert coin" message to loop when no coins have been inserted or all credits have been used

This is not a suitable use for interrupts. Just use digitalRead() and also see the "state change" example sketch.

So are you suggesting an if else:

if (creditState == 0) {
//please insert coin...
}
else {
//credit count
}

nope that's not working at all, the program is adding infinite credits

Yay you can play the game infinitely! Change it to an else if maybe?
EDIT: Hang on, if credit state is low, that means the coin has been inserted, right? So should it be set to high? Because the pinmode is INPUT_PULLUP.

i'll try

Me? No, just pointing out that using interrupts is not appropriate here. Interrupts are for situations where the input signals are very fast and very short-lived, and could be missed otherwise. Here, using digitalRead() in loop() will be fine, as long as the code is well written. (Well written code does not use long delay() calls.)

void loop() {
  

  //Bug here not displaying the "Insert Coin" message
  creditState = digitalRead(creditPin); //Maybe bugs
    
  if (creditState == 0) {
    lcd.setCursor(0,0);
    lcd.print(" Please  Insert");
    lcd.setCursor(6,1);
    lcd.print("Coin");
    delay(1000);
    lcd.clear();
    delay(1000);    
  }
  
    else if (creditState > 0) {
      lcd.clear();
     insert = false;
     //Serial.println("coin detected!");
     balance+=1;
     Serial.println("Credit: "+(String)balance);
     lcd.print("Credit: "+(String)balance);
   }

Did this and the program went nuts again and kept adding infinite credits

If credit state is low, that means the coin has been inserted, right? So should it be set to high? Because the pinmode is INPUT_PULLUP.

Ok now we have some progress, the program now displays the flashing "Please Insert coin" message indefinitely, even if a coin is inserted.

void loop() {
  

  //Bug here not displaying the "Insert Coin" message
  creditState = digitalRead(creditPin); //Maybe bugs
    
  if (creditState == HIGH) {
    lcd.setCursor(0,0);
    lcd.print(" Please  Insert");
    lcd.setCursor(6,1);
    lcd.print("Coin");
    delay(1000);
    lcd.clear();
    delay(1000);    
  }
  
    if (insert) {
      lcd.clear();
     insert = false;
     //Serial.println("coin detected!");
     balance+=1;
     Serial.println("Credit: "+(String)balance);
     lcd.print("Credit: "+(String)balance);
   }



  
}

Coin acceptors commonly have either relay contact or open collector outputs, so active LOW on the input would be expected.

You need to detect when the input changes state, not when it is a steady HIGH or LOW. Also be careful for bouncing contacts on the input.

Do you want to return to the insert coin message after a coin has been inserted, or remain on the coin amount message?