I am designing a coin sorter and counter for a project using an optical switch.
So far, I have my UNO count the number of coins that pass the IR beam and sending the count to the serial monitor:
const int coinPin = 2;
const int ledPin = 13;
int coinCounter = 0;
int coinState = 0;
int lastcoinState = 0;
void setup() {
pinMode(coinPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
coinState = digitalRead(coinPin);
if (coinState != lastcoinState) {
if (coinState == HIGH) {
coinCounter++;
Serial.println("on");
Serial.print("number of coins passed: ");
Serial.println(coinCounter);
}
else {
Serial.println("off");
}
}
lastcoinState = coinState;
if (coinCounter % 10 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
This is using just one type of coin. Now, I want to introduce a second type of coin.
The idea is that the 1p and 2p coin have different diameters and so the time it takes the 2p to roll past the slot (the length of time the voltage output is HIGH) will be longer than the 1p coin.
I would like to
measure the times it takes each type of coin to break the beam
use this measurement to then program the arduino to make two different coin counts. How many 1p coins pass, and how many 2p coins pass.
I am aware that I need to use a 16 bit counter and the interrupt function. I am new to code and programming in general.
I am aware that I need to use a 16 bit counter and the interrupt function
For what? Why?
You can tell when an event happened (the beam was broken or the beam was restored). You can record when these events occur, using millis() or micros(), depending on the interval between the events. You can subtract one event time from the other to get the interval between the events. No interrupts needed.
I may be getting you wrong here but I don't need to record the times between each coin insertion I need to record the time OF each coin insertion (i.e how long these events occur).
If I can do this by using millis() then would you please point me in the right direction on how to do this?
I may be getting you wrong here but I don't need to record the times between each coin insertion I need to record the time OF each coin insertion (i.e how long these events occur).
The time of the coin insertion is nothing to do with anything. Read your own post then re-read PaulS's comments
Interrupts may be necessary if you are doing other stuff in the code at the same
time, otherwise just use busy-waiting and call micros() to get the timestamps.
unsigned long time_coin ()
{
while (! digitalRead (coinPin)) // busy wait for coin
{}
unsigned long start_time = micros () ;
while (digitalRead (coinPin)) // busy wait for no coin
{}
return micros () - start_time ;
}
You may be able to detect the difference between a 1p (20mm) and a 2p (25mm) with this method, but I think you'll find it almost impossible to accurately detect the difference between a 1p (20mm) and a 5p (18mm). Also, have you taken into account the friction between the (possibly dirty) coin and the slot not always being the same, making the speed of the coin variable? I believe that commercial coin counters use a series of slots (smallest first) that the coin can fall through. Obviously, a 20mm or 25mm coin will not fall through an 19mm slot, but an 18mm coin will. The slots also vary in width so as to not allow thicker coins (Eg the £1 coin) to pass through the narrower slots. Using the slot method automatically sorts your coins into tubes of one value, which can then be used for dispensing change.
Rather than have the coins roll past the sensor with a risk that they roll at different speeds you could arrange for them to be carried by a conveyor belt at a known speed.
You will also need to ensure that the same cross-section of the same type of coin passes the detector every time. Easy to arrange if all are the same width in a guide that is the width of the coin. Not so easy if there are different sized coins that may not be aligned within the guides.
What about a series of beams such that a large coin covers all of them at the same time and a small coin only covers one or two of them.
Robin2:
What about a series of beams such that a large coin covers all of them at the same time and a small coin only covers one or two of them.
The beams would have to be very thin and accurate to differentiate between a 18mm 5p and a 20mm 1p. Lasers shining through 1mm wide slots would probably be the only way it would work. To avoid interference between the beams, I would stagger the beams along the coin's path so that the largest coin is detected first, smaller ones wouldn't break it, then lower and lower beams to detect smaller and smaller coins. The first beam broken will give the size of the coin. Subsequent broken beams can be ignored.
holmes4:
Now try sorting out the problem of the 50p which is not round and the give different readings.
Mark
No it won't. It may not be circular, but it has the same diameter all the way round (27mm*), as does the 20p coin (21.5mm*). It will always be the same height, whichever way up it is.
No it won't. It may not be circular, but it has the same diameter all the way round (27mm*), as does the 20p coin (21.5mm*). It will always be the same height, whichever way up it is.
No it won't. It may not be circular, but it has the same diameter all the way round (27mm*), as does the 20p coin (21.5mm*). It will always be the same height, whichever way up it is.
I never new that!
Mark
Each 'side' is an arc centred on the opposite point so that it will roll. The centre of rotation when rolling is not fixed, unlike a circular coin. Clever British design!