Coin acceptor arduino millis problem

Hello! I need your advice. My code sometimes count only 1 coin, sometimes counts double 1 peso coin and always after 1 coin inserted this don t count anymore.

I need to be able to receive on serial monitor credits inserted and total amount each time a coin is inserted.

I have a little trouble to inderstand millis and pulses time. i m connecting coin pin to d2 of arduino nano,,

Also I have connectd my nano to a timer so each time a coin is received a timer starts, don t know if it makes noise or it don t affect because they are separate. `
¿Can you help me please? Thanks in advance!


#include  <Arduino.h>
#include <EEPROM.h>

//Constants for pins.
const int SignalPin = 2;

//Constants of coins
const int UnPeso = 1;
const int DosPesos  = 2;
const int CincoPesos = 3;
const int DiezPesos = 4;

//Variables.
volatile int pulso = 0;
volatile unsigned long MillisUltPulse = 0;

int Pulsesacum = 0;
int CreditsAcum = 0;
int MaxTimePulse = 100;
bool coinInserted = false;


void setup() {
pinMode(2, INPUT_PULLUP);

 Serial.begin(9600);


attachInterrupt(digitalPinToInterrupt(SignalPin),coinInterrupt, RISING);
}



void coinInterrupt() {


pulso++;
MillisUltPulse = millis();
}

void loop() {


 unsigned long MillisUltPulse = 0;
int coinValue = 0;
bool coinInserted= false;


unsigned long currentMillis = millis();

  if ((pulse - MillisUltPulse >= MaxTimePulse ) && (digitalRead(SignalPin)== HIGH)) {

  MillisUltPulse = pulse;
  coinInserted = true;
 PulsesAcum += pulse;
pulse = 0;


}

//1 pulse

  if(PulsesAcum == Onepeso) {
  Serial.println("Inserted coin $1.00");
  CreditsAcum += 1;
  Serial.println("Total Credit: $ (CreditsAcum)");
  Serial.print(CreditsAcum);
  //Serial.print(" .00 ");
  PulsesAcum = 0;


}


//2 pulses

  if(PulsesAcum == twopesos) {
  Serial.println("Inserted coin $2.00");
  CreditsAcum += 2;
  Serial.println("Total Credit: $ (CreditsAcum) ");
  Serial.print(CreditsAcum);
  //Serial.print(" .00 ");
  PulsesAcum = 0;
}

//5 pulses

if(PulsesAcum == fivepesos) {
  Serial.println("Inserted coin $5.00");
  CreditsAcum += 5;
  Serial.println("Total Credit: $(CreditsAcum) ");
  Serial.print(CreditsAcum);
  PulsesAcum = 0;
}

//10 pulses

if(PulsesAcum == Tenpesos) {
  Serial.println("Inserted coin $10.00");
  CreditsAcum += 10;
  Serial.println("Total Credit: $(CreditsAcum)");
  Serial.print(CreditsAcum);
  PulsesAcum = 0;
}
}

no need to use interrupts. monitor pulses, but need to debounce.

const byte SignalPin = 2;
byte signalState;
int  pulseCnt;

unsigned long msec0;

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();
    if (0 < pulseCnt && msec - msec0 > 5000)  {
        pulseCnt = 0;
        Serial.println ("reset");
    }

    byte sig = digitalRead (SignalPin);
    if (signalState != sig)  {
        signalState = sig;
        delay (20);             // debounce

        if (HIGH == sig) {
            pulseCnt ++;
            msec0 = msec;

            Serial.print (pulseCnt);
            Serial.println (" coins inserted");
        }
    }
}

void setup ()
{
    Serial.begin (9600);
    pinMode (SignalPin, INPUT_PULLUP);
    signalState = digitalRead (SignalPin);
}
1 Like

You are using a variable named "pulse" which is shown in a different color.
This different color indicates that this word "pulse" is a predefined word

similar to "print" println etc.

You do not define a variable named "pulse"
This means this predefined "thing" with name pulse is used.

additionally "pulse" gets never assigned any value
except here

    pulse = 0;

how should such a comparison like

  if ((pulse - MillisUltPulse >= MaxTimePulse ) && (digitalRead(SignalPin) == HIGH)) {

ever work correctly if "pulse" always has value 0 ?

I tried to find your declaration of "pulse" and had to go a huge number of occurences of the letter-sequence "pulse"
25 occurences to the letter-sequence "pulse" in your code
But you did not declare it. The compiler did not complain because it is a predefined word.
But is something really different than what you thought.

As a general advice give everything unique and self-explaining names.
one way is to add "my" to everything that you declare
myPulse
my....

pulse : what kind of pulse? input? or output? which one (there might be more than one thing that is "pulsing"

For analysing and debugging your code add serial printing in various places to see the real numbers / values that are there

1 Like

You re a Master Jedi, it worked!

Thank you so much!

One last thing Sir, if possible can you tell me:

How to write the code to get total of coins after reset? and what other topics should I study beside C language to get better.

Best Regards!

Thank you Sir! i think it s more about debounce but as you mentioned I will chenga that value and be careful with names. Thank you so much!

One important thing to learn is: writing very precise descriptions.

  • what kind of reset?
    manually reset ?
    power Off/On cycle of the microcontroller?
    reset after a certain time?
    reset after inserting coins for "one case" has finished?
    total number of allcoins that have ever been detected?

For answeing these questions you should give an overview about your project.
And a detailed description of the wanted behaviour written in normal words
Best method to so do is describing a typical use of your device with ALL details

You should provide datasheets of all used hardware-components

It is also important to learn the basics of C++-programming
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

1 Like

don't know what you want to do.

Presumably there's some other buttons pressed to purchase (?) something. either there's a sufficient # of coins and the purchase is accepted, or it is not accepted, but there may be sufficient coins for some other item

coin telephones had a button to pressed to release the coins. Not knowing how your machine works i Just used a timer to reset the coin count.

1 Like

Thanks youSir! it s personal project just trying to know what are the sales of my machine (the machine work and recevie money by itself.. i m adaptting this to coin acceptor and see sales online, thanks for your help!

I ll keep studying.

Best Regards!

Thank you Sir! Ill study it ! this is a personal project with my machine to see sales of coin acceptor on the web. I know there are somethings and services that give this information.. but i m trying to do it my myself.

Best Regards! Thanks for yout help!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.