Hi Guys
Ill try and keep this brief. I
m 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