It's morning and no other takers so here is a rough start. As I see it you need to read 30 inputs fast and although the signals should be pretty clean from the opto sensors (depends on the sensor) they probably still need debouncing.
The debounce library may do this I haven't looked at it.
Done for 32 coins, will need slight adjustments for 30
Off the top of my head, thoroughly untested, not compiled, probably has bugs and maybe even logic errors. I think the basic idea is sound, but then I'm not the one putting the tender in for the job
// Wiring must be organised such that the sensors are connected
// in groups of 8 to to procesor ports
// Assumes
// 4-digit coin counters, ie 9999 coins max
// digital input goes HIGH when coin over sensor
#define DEBOUNCE_TIME 5
#define TRANSITION_TIME 50 // # of mS it takes a coin to pass the sensor
#define N_COINS 32
byte ports [4] = {PORTC, PORTD, PORTF, PORTG};
byte debounce_counters [N_COINS];
boolean coin_passing [N_COINS];
int coin_counters[N_COINS];
int display_timer;
void setup () {
// do some stuff here
SPI.begin(...); // etc
for (int i = 0; i < N_COINS; i++) {
coin_passing[i] = false; // shouldn't really need this
}
}
void loop () {
static display_counter = 0;
byte group = 0;
for (int port = 0, port < 4; port++) {
byte portval = ports[port]; // get 8 sensor bits
// now test each sensor bit
// increment the sensor's counter if the bit is 1
// and we didn't just do so,
// ie the coin is still passing from the last count
// clear the sensor's counter if 0
for (int sens = 0; sens < 8 sens++) {
if (portval & B00000001 == 1) {
if (coin_passing[i]) {
coin_passing[i]--; // coin is still passing from last count, dec counter
} else {
debounce_counters[port + sens]++; // one more closer to being a valid coin
}
} else {
debounce_counters[port + sens] = 0; // any 0 value and we start again
}
portval >>= 1; // move next bit into position 0
}
}
/////////////////////////////////////////
// now we have an updated array of 32 counters
// test to see if any have reached the debounce value
for (int i = 0; i < N_COINS; i++) {
if (debounce_counters[i] > DEBOUNCE_TIME) {
// we have a new coin
coin_counters[i]++;
debounce_counters[i] = 0;
coin_passing[i] = TRANSITION_TIME;
}
}
//////////////////////////////////////////
// every 100mS send the counters out to the displays,
// assumes a string of N_COINS AS1108 shift regs or similar and 4-digit counters
// for 32 displays we need to shift 32*4*7 bits
// at 8mMHz this will take 112uS plus some overhead
static temp_array[4];
display_timer++;
if (display_timer >= 100) {
display_timer = 0;
for (int i = 0; i < N_COINS; i++) {
func_to_convert_bin_to_BCD (temp_array, coin_counters[i]); // there may be a library func for this
SPI.transfer (temp_array[0]);
SPI.transfer (temp_array[1]);
SPI.transfer (temp_array[2]);
SPI.transfer (temp_array[3]);
}
}
delay (1); // 1mS delay added to the time it takes to do the above
}
Thanks Graynomad! I think I am going to order a few of those sensors to test to see if I can get it to work. Do you think I can hit you up sometime outside of forum if I need a little help? Okay well a lot of help?
As there are much better coders than me here I take some comfort from the fact that no one has pinged me on the code yet, or maybe they just haven't noticed the thread
Graynomad:
... I take some comfort from the fact that no one has pinged me on the code yet
...
Rob, I don't see anything non-functional. Perhaps there is a certain down-under accent to your declarations but even an old Yankee can understand them.
Grumpy_Mike:
You are asking quite a lot here especially given your current state of knowledge.
Are the light sensors only detecting the presence of the coins? If so then you need a digital one. You should not rely on ambient light you should shade the system and use only the lights under your control.
How are you detecting the difference between these coins?
I made a coin counter many years ago that used light sensors to detect the diameter of coins as they rolled down a slot.
Are you planning to build this yourself?
Hi. I am interested in what you said about using light sensors to detect the diameter of the coin. Could you maybe send me a personal message about how you built it.
Thanks!
I wrote an article to do thin in the July 94 issue of the Micro User Magazine.
PM me your email address and I will send you a copy.
The software is written in BBC basic but the hardware should translate. However it does use a TSL214 CCD sensor that you might have trouble getting hold of these days.