Arduino processing coin acceptor pulse

Hello, I just a bought arduino nano 328. I'd like to program it to proccess incoming coins for about 2-3sec before firing accumulated pulses to my other device.

The attached image shows a coin accepter as separate module, with this in between the coin acceptor and another device.

He should be all set, the project is probably nothing too complicated (might require some level shifting, ofc, depending on what he's interfacing with) . I wonder if he had a question in mind when he posted this?

masterp0gi:
Hello, I just a bought arduino nano 328. I'd like to program it to proccess incoming coins for about 2-3sec before firing accumulated pulses to my other device.

Coin and bill acceptors usually output a pulse for every "X" unit of money (for example, 1 pulse for a 1 dollar bill, 20 pulses for s $20, etc..)

What you could do is have a loop that polls an input pin, then when you get the first pulse, grab a snapshot of the time (i.e. a timer like "millis()". And of course, also count the pulses.

The code would look something like this (pseudo-code, not actual code):

uint8_t pulses = 0;
uint32_t start = 0;

while (1) {
    check input pin  <-------|
    if no input, check again |
    if we have a pulse AND "pulses" == 0 then start = millis(); // get time of first pulse
    pulses = pulses + 1;
    if (millis () > (start + 3000)) {
        break; // 3 seconds elapsed, we assume pulses are done
    }
}

send the value of "pulses" to other device;

You see what that does? It loops around and around waiting for a pulse. When it sees a pulse AND the current count is zero, this means "we just started" so we grab a snapshot of the time, record the pulse and go back into the loop.

Most surely, all the pulses will be done long before the 3 seconds is up, and the loop keeps running until "current time" is greater than "start time plus 3 seconds", then it breaks out of the loop and goes to the next part (that sends the count to the other device).

Hope this helps, and remember this is only "pseudo-code", if you try to compile it as-is, it won't work. The intent is to give you an IDEA how to accomplish what you want.

Good luck.

Thank you sir! had an idea but still don't know how to proceed if inside while.

const int C = 4;
const int P = 13;

void setup (){
  Serial.begin(9600);
  pinMode(C, INPUT);
  pinMode(P, OUTPUT);
}

void loop (){

  int j = digitalRead(C);
  int x = 0;
  int y = 0;
  int Val = 0;

  while (j <= x){
    Serial.println("Please insert coin");
    delay(300);

    if (j > x){
      Serial.print("Coin Inserted"); 
      digitalWrite(P, HIGH);
      delay(100);
      digitalWrite(P, LOW);
    }
  }
  
}

As you can see I'm trying to print "Coin Inserted" if the value of j greater than x. Sorry for my very poor programming skills. Please advise. TIA

masterp0gi:
Sorry for my very poor programming skills.

I suspect more shortfalls in your reading skills.

You were given a very detailed recipe and present the above code as an implementation?

Retry.

Whandall:
I suspect more shortfalls in your reading skills.

OK, in fact Krupski did actually confuse the matter by suggesting a "While" loop instead of proper Arduino code. :roll_eyes:

Sorry I was trying to understand each function line by doing other approach. That's why I tried printing inside while and if.

uint8_t pulses = 0;
uint32_t start = 0;
int x = 13;

void setup(){
pinMode(x, OUTPUT);
}

void loop (){
  

while (1) {
    analogRead(A0);
    if (analogRead(A0) == 0){
      analogRead(A0);
    }
    if ((analogRead(A0) >=1) && (pulses == 0)){
      start = millis();
      pulses = pulses + 1;
    }
    if (millis () > (start + 3000)) {
        break;
    }
}
digitalWrite(x, pulses);
}

based on the given pseudo code. Here's what I did.

I'd really appreciate any comments. thanks :slight_smile:

@Paul__B: the presented code did not use millis at all.
I have read the while(1) {} as a non-Arduino metapher for loop() {}.

Paul__B:
OK, in fact Krupski did actually confuse the matter by suggesting a "While" loop instead of proper Arduino code. :roll_eyes:

A "while loop" is not confusing, nor is it "improper Arduino code".

I know you're picking on my penchant for using "int main (void)" instead of "setup/loop", but that's not the case here.

By the way, I didn't see any help or any code (pseudo or real) that would be of assistance to anyone in your post.

Surely I've just missed it? :roll_eyes:

Whandall:
@Paul__B: the presented code did not use millis at all.
I have read the while(1) {} as a non-Arduino metapher for loop() {}.

"while (1)" was not a "metaphor" for anything. It was a while loop.

The idea is to loop (wait) until the FIRST coin pulse is detected, then begin (at the same time) counting pulses AND counting down time so that the loop exits in a reasonable period of time with a final pulse count.

I arbitrarily chose 3 seconds because I've used bill and coin acceptors before and I know they output SLOWLY (like 5 to 10 pulses per second) and I thought 3000 milliseconds would be enough time to capture anything.

Of course, that can be adjusted if it proves to be too short or too long.

I REALLY get tired of reading on here the same few people always making snide comments, never posting any real help, insulting users who are not good English speakers and in general derailing a thread started by someone who is ASKING FOR HELP. I also get tired of trying to seriously help people and have to read between the snide, rude and no-help posts made by the same few users. Are there no moderators here???

(edit to add): By the way, the above "rant" was not aimed at you Whandall.