Coin Acceptor to Pro Micro

So I'm trying some basic before bigger project(coin acceptor to turn on 12v air hockey table).
Want to turn on LED after a pulse from Coin Acceptor.
When one coin inserted I see #2 on Coin Acceptor LED, assuming that's 2 pulse.

Anyway, I leached this code off internet but it is not working for me. The minute Pro Micro is powered, LED is 'ON' without trigger from Coin Acceptor, why? I hooked up LED from pin3 and through resistor to onboard ground.

const int coinpin = 2;
//const int ledpin = LED_BUILTIN;
const int ledpin = 3;
const int target = 4;

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

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

void loop() {
  // If we've hit our target amount of coins, increment our credits and reset the cents counter
  if (cents >= target) {
    credits = credits + 1;
    cents = cents - target;
  }
  // 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);
}
// 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);
}

If anyone has better code please share.

Most likely your coininterrupt is floating and acting like an antenna for your mains frequency. Study the schematic of your coin device and see where the interrupt line goes.

Paul

ps. Is the Arduino ground connected to the coin accepter ground?

i would think the LED could only be on for a fraction of a second since it is constantly being turned off in loop.

i would comment out the attachInterrupt() to see if the LED is still lit. are you sure the LED is wires to ground?

if it isn't, then is seems the interrupt is constantly being invoked. how is the coinPin wired. should is be configured as INPUT_PULLUP?

what happens if a coin is inserted near the end of the 1 sec delay? the led won't be on very long. it would make more sense to recognize when a coin is inserted and start a timer or call delay.

i think using an interrupt is unnecessary in this case. either you're waiting for coins or you're doing something else.

As a test, i'm running Pro Micro off powerbank, so No they are not sharing common ground.

The coinpin is hook up straight to pin 2.

If I disconnect the pin 2, led goes off.

By commented out that attachinterrupt, no LED what so ever.

Also, how would I use the built in led? LED_BUILTIN doesn't work. Do I have to specify TXLED or RXLED?

Any suggestion? Help rework my code please?

did you configure coinPin as INPUT_PULLUP?

If the coin machine and the Arduino DO NOT SHARE A COMMON ground, then you get just exactly what you are seeing.

Paul

Though it's getting power from powerbank, can I still connect ground from Arduino to Coin ground?

I was trying to connect 3 electronics with one 12v power source so it could share common ground and it seems that this little board draw more voltage then the other. I unplug the one leaving just the coin and Arduino and I think I killed ARDY.

Alright, on with the 3rd pro micro. Time to solder some more pins.

From Arduino IDE examples, which one should I go off of based on my project vision?

I think it's time for you to draw a schematic of how your all this connected together.

Paul

Well here's the sketch I have to start out with. Eventually I want to replace LED with a motor.

Waiting for the schematic.

Paul

The picture show the best of my ability for schematic.

ART50317:
The picture show the best of my ability for schematic.

?

There's an attachment. a picture.

Why do you have the Arduino reset pin pulled high to 12 volts?

Paul

It's not I met to draw on the vcc.

This is the closest code I could find that might help with my project. Someone please help me code an "IF" statement.

If balance equal 100 activate/turn on LED pin for 60 second and reset.

const int coin = 1;
boolean insert = false;
volatile int pulse = 0;
unsigned long balance;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), coinInterrupt, RISING);
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (insert) {
    insert = false;
    //Serial.println("coin detected!");
    balance+=25;
    Serial.println("Balance : "+(String)balance);
    delay(1000);
  }
}

//interrupt
void coinInterrupt() {
  pulse++ ;
  insert = true;
}

BUMP!
Anyone? I was able to have the code read pulse from the coin acceptor.
each Quarter is 25 and $1 coins 25x4.

Please help me add "if" statement.
If it reach 100 enabled LED for amount in minutes then reset back to zero.
I had something like this:

if (balance = 0);{
  pinMode(9, OUTPUT);
}

but it didn't wait for the count, once pulse triggered, LED just turn on.