I have two coin acceptor and two 16x2 display that is connected to my arduino mega. The problem is when the first coin acceptor is inserted with the coin, it is working fine but when I inserted another coin to the second coin acceptor, it will not count until the first coin acceptor is finished. it will wait the first coin acceptor to finish its task before the second coin acceptor read the coin and start counting and vise versa.
Someone said that I can use the millis so the two coin acceptors will work simultaneously, but I don't know where do i have to put it.
here's the code:
const int coinPin = 2;
const int coinPin1 = 3;
volatile int pulse = 0;
volatile int pulse1 = 0;
boolean bInserted = false;
boolean bInserted1 = false;
int runFor = 10; // time in minutes
void setup() {
attachInterrupt(digitalPinToInterrupt(coinPin), coinInserted, RISING);
attachInterrupt(digitalPinToInterrupt(coinPin1), coinInserted1, RISING);
Serial.begin(9600);
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Please Insert");
lcd.setCursor(0, 1);
lcd.print("P5.00 Coin...");
lcd1.setCursor(0, 0);
lcd1.print("Please Insert");
lcd1.setCursor(0, 1);
lcd1.print("P5.00 Coin...");
check:
if ( bInserted )
{
bInserted = false;
lcd.clear();
lcd.print("TIME LEFT:");
//Start timer
timer();
goto check;
} else if ( bInserted1 ) {
bInserted1 = false;
lcd1.clear();
lcd1.print("TIME LEFT:");
//Start timer
timer1();
goto check;
}
}
void coinInserted()
{
pulse++;
bInserted = true;
}
void coinInserted1()
{
pulse1++;
bInserted1 = true;
}
void timer()
{
for (int timer = runFor; timer > 0; timer--) {
if (bInserted == true)
{
timer += 10; //extend
bInserted = false;
}
if (timer >= 10)
{
lcd.setCursor(3, 1);
} else {
lcd.setCursor(3, 1);
lcd.print("0");
lcd.setCursor(4, 1);
}
lcd.print(timer);
lcd.print(" Min");
delay(60000); //1 minute
}
lcd.setCursor(0, 0);
lcd.clear();
lcd.print(" Time Over !");
delay(500);
lcd.clear();
}
void timer1()
{
for (int timer = runFor; timer > 0; timer--) {
if (bInserted1 == true)
{
timer += 10; //extend
bInserted1 = false;
}
if (timer >= 10)
{
lcd1.setCursor(3, 1);
} else {
lcd1.setCursor(3, 1);
lcd1.print("0");
lcd1.setCursor(4, 1);
}
lcd1.print(timer);
lcd1.print(" Min");
delay(60000); //1 minute delay
}
lcd1.setCursor(0, 0);
lcd1.clear();
lcd1.print(" Time Over !");
delay(500);
lcd1.clear();
}