Having Code Problems please help!

Hi all new member here this is my first post so I’m sorry if I have done anything incorrectly, First post but been playing about with Arduinos for a few years now and I’m getting better at coding and normally figure things out by myself with a bit of googling but I’m stuck on this and I hope somebody will be able to help.

I am building a Bartop Arcade Cabinet that will accept coins I have a coin acceptor (ch-926) and have it working with my Arduino Duemilanove using code I found online however I am trying to modify this code to allow me to send credits to player 1 or 2 depending on what button is press I have a few other bits going on in the code too like flashing an LED when credits are available this all works and the code runs on the Arduino however once it has credits it will automaticlly give credits to player 1 then player 2 until it has no credits remaining

my code is Below

Thanks and look forward to your replies

Jamie :slight_smile:

int buttonState1;                         // the current reading from the input pin P1
int buttonState2;                         // the current reading from the input pin P2
const int coinInt = 1;                    // Coin Acceptor Pulse Pin
int creditled = 13;                       // Led to flash when credits are avaliable
int Play1Coin = 4;                        // Player 1 credit to Arcade IPAC Board
int Play2Coin = 5;                        // Player 2 credit to Arcade IPAC Board
int P1credit = 6;                               // Player 1 credit button
int P2credit = 7;                               // Player 2 credit button
int credit = 0.50;                        // Cost of Credit
volatile float coinsValue = 0.00;         //Set the coinsValue to a Volatile float Volatile as this variable changes any time the Interrupt is triggered
int oldMillis = 0;                        // int oldmills for the coin acceptor to stop static adding credit
int coinsChange = 0;                      //A Coin has been inserted flag
int currentMillis = 0;                    // More Staic credit prevention
int currentMillisled = 0;                 // Current mills for Credit LED Flash
unsigned long previousMillis = 0;         // Pre mills for Credit LED Flash
const long interval = 500;                // delay ms for Credit LED Flash
int ledState = LOW;                       // State of LED


void setup()
{
  Serial.begin(9600);
  //Start Serial Communication
  attachInterrupt(coinInt, coinInserted, RISING);
  pinMode(creditled, OUTPUT);
  pinMode(Play1Coin, OUTPUT);
  pinMode(Play2Coin, OUTPUT);
  pinMode(P1credit, INPUT);
  pinMode(P2credit, INPUT);
  //If coinInt goes HIGH (a Pulse), call the coinInserted function
  //An attachInterrupt will always trigger, even if your using delays
}

void coinInserted()
//The function that is called every time it recieves a pulse
{

  //As we set the Pulse to represent 50p we add this to the coinsValue
  coinsChange = 1;
  unsigned long currentMillis = millis();
  int difference = currentMillis - oldMillis;
  Serial.print("Millis: ");
  Serial.println(difference);
  oldMillis = currentMillis;
  if (difference < 135 && difference > 125) {
    coinsValue = coinsValue + 0.50;
    coinsChange = 1;
  }
  //Flag that there has been a coin inserted
}

void loop()
{
  unsigned long currentMillis = millis();
  if (coinsChange == 1) {

    Serial.print("Credit: £");
    Serial.println(coinsValue);
    coinsChange = 0;
  }

  // Flash LED if we have enought money for a Credit
  if (coinsValue >= 0.50) {
    digitalWrite(creditled, ledState);
  }
  if (currentMillisled - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
  }


  buttonState1 = digitalRead(P1credit); //Read Buttons to check if a player wants a Credit
  buttonState2 = digitalRead(P2credit);

  if ((buttonState1 = HIGH) && (coinsValue >= credit))
  {
    digitalWrite (Play1Coin, HIGH);
    delay(300);
    coinsValue = coinsValue - 0.50;
    Serial.print("Player 1 one Credit");
    digitalWrite (Play1Coin, LOW);
  }
  else
  {
    digitalWrite (Play1Coin, LOW);
  }
  if ((buttonState1 = HIGH) && (coinsValue >= credit))
  {
    digitalWrite (Play2Coin, HIGH);
    delay(300);
    coinsValue = coinsValue - 0.50;
    Serial.print("Player 2 one Credit");
    digitalWrite (Play2Coin, LOW);
  }
  else
  {
    digitalWrite (Play2Coin, LOW);
  }
}
  1. No Serial inside the interrupt. Comment out those lines.

  2. Big Oopsie here. = vs ==

if ((buttonState1 = HIGH) && (coinsValue >= credit))

Also when that lines shows up again I suppose is actually supposed to look at buttonState2 but didn't get changed when you pasted it.

Delta_G:

  1. No Serial inside the interrupt. Comment out those lines.

  2. Big Oopsie here. = vs ==

if ((buttonState1 = HIGH) && (coinsValue >= credit))

Also when that lines shows up again I suppose is actually supposed to look at buttonState2 but didn't get changed when you pasted it.

You are awesome! Fastest reply i have ever had on a forum and solved my issue first time not sure how i missed that error feel a bit stupid now ha ha

You're also correct the second buttonState is buttonstate2 was trying to make my code readable before posting and forgot to change it

Many thanks if i get stuck again i will be back

int credit = 0.50;                        // Cost of Credit

?

credit is set at zero, so any coin value will greater than.

This code would be much better done in just plain Pence (int) rather than fractions of Pounds (float)

indev2:

int credit = 0.50;                        // Cost of Credit

?

credit is set at zero, so any coin value will greater than.

This code would be much better done in just plain Pence (int) rather than fractions of Pounds (float)

Yeah I missed that. You might want to check out what an int variable can and cannot hold.

And please don’t be tempted to use float to solve it. Sometimes float values aren’t exact and 0.50 might actually be stored as 0.499999999 and get truncated to 0.49 in your calculations. Instead do as the last poster recommended. Store 50 for one coin and two coins is 100 etc. Ditch the decimal entirely.