0

I'm working on a project that makes use of a CH926 coin acceptor. The problem we're having is that when I read the serial monitor, the coin value is continuously going up and adding credits when it's supposed to remain at 0 until a coin Is fed into the acceptor.

We've tried a different coin acceptor, different codes, arduinos, wiring, resistors, breadboards, the lot. nothing seems to be solving this problem and we can't determine why, it had been working fine until we tried to tidy up our wiring and untangle it.

we're using a basic code that had worked fine up until we had the issue of the counter value no longer remaining at 0:

const int coinpin = 2;
const int ledpin = 3;
const int targetcents = 100;

// Variables
volatile int cents = 0;
int credits = 0;

// Setup
void setup() {

  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
  pinMode(ledpin, OUTPUT);

}

// Main loop
void loop() {

  // If we've hit our target amount of coins, increment our credits and reset the cents counter
  if (cents >= targetcents) {
    credits = credits + 1;
    cents = cents - targetcents;
  }

  // If we haven't reached our target, keep waiting...
  else {
  }

  // Debugging zone
  Serial.print(cents);
  Serial.print(" cents toward current credit and ");
  Serial.print(credits);
  Serial.println(" credit(s) earned so far.");
  delay(1000);

  // Turn off LED
  digitalWrite(ledpin, LOW);

  // Now, write your own cool code here that triggers an event when the player has credits!
  if (credits > 0) {
    // Play music?
    // Spin up a motor?
    // Start a game?
    // It's up to you!
  }

}

// Interrupt
void coinInterrupt(){

  // Each time a pulse is sent from the coin acceptor, interrupt main loop to add 1 cent and flip on the LED
  cents = cents + 1;
  digitalWrite(ledpin, HIGH);

}

any advice on what may be causing this problem?

cheers

  • 3
    Sounds like a floating input. Check your pullup / pulldown resistor on coinpin. – Majenko Dec 2 '19 at 12:09
  • 1
    In addition to what @Majenko said, I have the following idea: Since the Sensor is powered by 12 V I guess you power it externally. Have the external power and the Arduino a common ground connection? I.e. did you connect the GND of the external power and a GND Pin of the Arduino. Perhaps you did it in your first setup but forgot it after rewiring. – Peter Paul Kiefer Dec 2 '19 at 13:38
  • if the coin acceptor worked until you tidied up the wiring, then why are you posting code? ... why did you say nothing about the wiring? – jsotola Dec 2 '19 at 15:29
  • 2
    "...it had been working fine until we tried to tidy up our wiring and untangle it." Wait, what? Your hardware/software system was working fine until you made a hardware change (trying to tidy up the wiring) and now it isn't working, so you decide it must be the software? Why does that make any sense? If it worked fine until you made a hardware change, shouldn't you focus on hardware problems? You almost certainly messed up some of the wiring. Focus on that. – Duncan C Dec 3 '19 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.