Change Machine - half way made!

Hi Guys
Ill try and keep this brief. Im making a change machine - you put money in it and change it for 10p coins. £1 to 10x10p £50p to 5X10p… you get the idea.

I have used an arduino to control a coin hopper so that when a button is pressed and the pin goes high, it turns a relay on, waits for 10 pulses from the coin hopper to spit out 10 coins:

Code for the hopper:

//This code dispences 10 x 10p coins when £1 is inserted.
//A coin hopper is attached to a relay on "relayPin". When a coin is released a pulse is generated on the opCountPin.
//The code waits for buttonPin to be pressed (£1 coin in), turns the relay on and waits for a count of 10 pulses before 
//turning the relay off.

volatile byte burp=0;    // a counter to see how many times the pin has changed
byte cmd=0;     // a place to put our serial data
int opCountPin = 2;  // pin2 as optical count input 
int relayPin = 7;  // pin7 output relay
int buttonPin = 3; //pin3 button pin
int buttonState = 0;  //reads coin button state

void setup() {
  Serial.begin(9600);
  Serial.print("PinChangeInt test2 on pin ");
  Serial.print(opCountPin);
  Serial.println();
  pinMode(opCountPin, INPUT);     //optical count is an input
  pinMode(relayPin, OUTPUT);    // relay is an output
  pinMode(buttonPin, INPUT);    //coin button trigger input
  digitalWrite(opCountPin, HIGH); //use the internal pullup resistor on the optical input
  attachInterrupt(0, burpcount, FALLING); // attach a PinChange Interrupt to our pin on the rising edge
// and execute the function burpcount when that pin changes - ALWAYS CHECKING CHANGES
  }

void loop()
{ 
/*{  
  cmd=Serial.read();  
  if (cmd=='p')
  {
    Serial.print("burpcount:\t");
    Serial.println(burp, DEC);
  }
  cmd=0;
}*/
delay(50);
digitalWrite(relayPin, HIGH);   //turn off relay - active LOW.
buttonState = digitalRead(buttonPin);   // read the state of the pushbutton value:
  
  if (buttonState == HIGH)  // button state high and it has been pressed 
   {        
    digitalWrite(relayPin, LOW);   //turn on relay - active LOW.
    delay(50);  
    burp =0;
      
      while (burp < 10)
      {
        //do nothing and wait with the relay on
      }
        
        
   }
}


void burpcount()
{
  burp++;
}

So basically, the interrupt is always checking for pulses or “burps” from the optical coin hopper output. When the button gets pressed, it turns on the relay and waits for 10 pulses and turns off the relay/hopper.

OK - so what I`m trying to do next!!!

I`ve now purchased one of these:

Basically, this simply detects what coin you have put in it and then on a single output, pulses a set number of pulses for different coins (max 30 pulses)

So for example, you could set it up so that 10p pulses once, 20p pulses twice, 50p five times and £1 ten pulses.

I now want to adapt the code so that instead of having a button dispense 10 x10p coins - The code waits for a coin to be inserted, once it feels a pulse on a second interrupt, it waits a short while to see what coin has been put it, (eg, it senses that 5 pulses have come through for a 50p) and then ejects 5x10p coins. So I already have code and can adapt the code to release a set number of coins from the hopper - I`m not sure how to monitor pulses coming in for different coins inserted.

I`m new to arduino so it has taken me a long way to get to this point so any help appreciated. Thanks to those who have helped in an earlier thread- I appreciate it.

Chris

Do you know what those 2 wires we can see are for?

I'm presuming you should be able to connect a wire to a digital in of the Arduino and just keep count of the number of pulses received within N seconds of the first pulse, perhaps?

Hi, there are pretty much just 3 wires, two for power and one pulse line to the arduino which will have the internal pull-up enabled as it pulses by dropping LOW.

To simplify the code I need as best as possible....

I just need to hang around and wait for a pulse.... When I sense a pulse, wait for two seconds to check how many others come if any! Store the count to a variable and then wait for the next pulse...whenever that may be when another coin is inserted.

Any help appreciated. I'm really not clear where to go with this,

I would hook the pulse wire to Pin 2 and then use Interrupt 0:

volatile unsigned char PulseCount = 0;
volatile unsigned long LastPulseTime = 0;

void setup() {
    pinMode(2, INPUT_PULLUP);
    attachInterrupt(0, PulseISR, FALLING);  // Call PulseISR() whenever the line goes low.
}

void PulseISR() {
    PulseCount++;
    LastPulseTime = millis();
}

void loop() {
    unsigned char pulseCount = 0;
    unsigned long now = millis();

    noInterrupts();  // Prevent the ISR from mucking with the variables

    if (now-LastPulseTime > 2000)
        pulseCount = PulseCount;
        PulseCount = 0;
        LastPulseTime = now;  // Wait some before processing the 0 count
    }

    interrupts();  // Let the ISR continue

    for (int i=0; i<pulseCount; i++) {
        // dispense a coin
    }
}

This should work for up to 255 pulses.

Hi John, that is a fantastic bit of code - although it will take me a short while to fully get my head around it, I can see how it works.

If I use demonisations of one pulse from the coin mechanism to equal one pulse of a 10p dispense that would work! I.e every pulse in throws a 10p out:
10p in, 1 pulse, 10p out
20p in, 2 pulses, 2x10p out
50p in, 5 pulses, 5x50p out
£1 in, 10 pulses, 10x10p out

Do I even have to count pulses? ... Is it actually simply a case of dispensing a coin for each pulse in. Bearing in mind I'm already detecting the coin hopper pulses on an interrupt to count how many coins are being dispensed - I'm guessing if I don't count, I'll end up missing pulses coming in from the coin mechanism while waiting for pulses coming back from the coin hopper.

craynerd:
If I use demonisations of one pulse from the coin mechanism to equal one pulse of a 10p dispense that would work! I.e every pulse in throws a 10p out:

That would work if your output hopper can keep up with the input pulses.

The interrupt version allows you to take as long as you need to to dispense coins. If they insert coins, pause two seconds, and put in more coins the new pulses will accumulate while the first batch is being dispensed. Two seconds after the last coin is inserted (or when the dispensing process is done, whichever comes later) the second batch of pulses will be processed.

You should probably add logic that will reject input coins if you don't have enough output coins to make change. That will make everything more complex.