Coin Machine CH926 Arduino Coding

Hello Everyone. I am a first time, long time. This has been done and talked about many times, but I cannot figure out how to code a CH926 coin machine.

Project:

I am trying to program a the CH926 coin machine to accept 2 quarters and turn on a blower, lights, etc, for 3 minutes. It is code for a .50 cent air hockey table.

What I know:

I know how to do everything (relays, programming the coin machine etc.)

What I don't know/Having trouble with:

I cannot figure out how to program the Arduino correctly. I have viewed many sites and this is what I have come up with:

const int coinInt = 0;
volatile float coinsValue = 0.00;
int coinsChange = 0;
int ledPin = 13;

void setup()
{
attachInterrupt(coinInt, coinInserted, RISING);
pinMode(ledPin, OUTPUT);
}

void coinInserted()
{
coinsValue = coinsValue + 0.25;
coinsChange = 1;
}

void loop()
{
if(coinsChange == 2)
coinsChange = 0;
if(coinsValue >= 0.50)
digitalWrite(ledPin, HIGH);
delay(1 * 60 * 1000);
digitalWrite(ledPin, LOW);
}

I don't understand all of it (like RISING), but most of it seems logical (as far as Arduino coding goes).

Last question:

How can I simulate this in TinkerCAD (Circuit)? I don't think I am doing the pulse part right. I am just hooking up a switch with 5v to act as a "pulse" from the coin machine. (of course, since my coding is wrong, none of that works).

Thanks in advance and be gentle, I am still learning :slight_smile:

Well you clearly have pulled most of the code off the instructables website, but you have altered it in ways you might not understand.

Example how would coinsChange ever get to 2?

That coin unit is made to default to a output of one pulse per 5 cents worth of coin.

So when correctly installed, a nickel will send one pulse, a dime 2, a quarter 5, ect..ect.. I suppose if you only program the unit to accept a quarter then you could consider each pulse as .25.

Use the teachings of blink without delay to keep everything responsive. Prepare to deal with additional coins being inserted during the cycle.

I thought coinsChange behaves as a counter also. So if I want the game to activate only using quarters at .50 cents (2 coins), I thought I would need it to count to 2, then activate pin 13 (attached to a relay). I would like additional coins rejected during the 3min game cycle. Am I looking at/editing the code the wrong way?

Most of the code I am finding online has WAY more functions than I need. I am more of a builder than coder, so I am trying to edit existing code and learn more about it along the way. I want the code to do the following:

  1. Accept two quarters (only two quarters. All other coins will be rejected and no extra time will be added to the game/playing time).
  2. Turn on pin 13 (and/or others if needed) for 3 minutes.
  3. Turn off the game (pin 13 and/or others) after 3 minutes.
  4. Loop to the beginning and wait for two more quarters (in my case, two more/new pulses, because it is only accepting quarters) and repeat the cycle.

I don’t see a “lock out” control for the unit so you probably need to power it with a relay contact and control the relay to allow/disallow coin useage.

Dont even need all the coinValue stuff since it’s irrelevant as you just need to count to 2…

Interrupt needs to increment coinCount

Loop needs to see if coinCount is less than 2
turn on pin powering coin counter relay
Turn off blower and led pin(s)

Else
Turn off coin relay
Turn on pin(s) controlling blowers and leds
Set coinCount to 0
Delay 180000

Done

Ok, this code works, but I have no idea how. To me, it should not work, but it does exactly what I want. The only thing I could not figure out was how to add a coin count to it. There are remnants of count code in there, but I am still trying to figure that part out. Any thoughts?

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin1 = 13; // the number of the LED pin
const int ledPin2 = 12; // the number of the LED pin
const int ledPin3 = 10; // the number of the LED pin
const int ledPin4 = 11; // the number of the LED pin
int count = 0; // Count the button presses

// variables will change:

int buttonState = 0; // variable for reading the pushbutton status

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

void loop() {

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.

// if it is, the buttonState is HIGH:

if (buttonState == HIGH) {

// turn LED on:

digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);

}

else {

digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
delay(5000);
}

}