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?
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.
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
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.
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.)