Coin counter on ESP 8266

Place your projects here
Post Reply
User avatar
Basicboy
Posts: 179
Joined: Sat Jul 20, 2024 11:42 am
Location: Australia
Has thanked: 56 times
Been thanked: 13 times

Coin counter on ESP 8266

Post by Basicboy »

Well, here goes, my first noob contribution :mrgreen:

A coin counter on ESP 8266

Now this code will work on a diy coin sorter that looks something like this
image.png
I still didn't build a case yet as the biggest challenge seems to be not code but mechanical! (getting the right coin to fall through the right slot :lol: )

Schematics: LCD 1602 to be connected to pins 4,5 (i.e D1 and D2)

Other pins to be connected to ground, with the falling coin through the slit closing the circuit (I've pulled all pins high)
Customised for Australian coins, but you can change to your own
d3 = 5¢
d4 = 10¢
d5 = 20¢
d6 = 50¢
d7 = $1
tx = $2

rx = Reset button to zero all values and restart count a new

Code: [Local Link Removed for Guests]


'Initialise LCD
I2C.SETUP 4,5   
LCD.INIT 39, 16,2
LCD.CLS  

'Message
lcd.print 1,1, "Coin counter "
lcd.print 1,2, "@2024 Basicboy"
pause 3000

'Initialise Counter pins
D0=16:D1=5:D2=4:D3=0:D4=2:D5=14:D6=12:D7=13:D8=15:D9=3:Tx=1:rx=3

dim coin(6) =d3,d4, d5,d6,d7,tx,rx
dim lcoin(6) = 1,1,1,1,1,1,1
dim vcoin(5) = 0,0,0,0,0,0
for y = 0 to 6
PIN.MODE coin(y), INPUT, PULLUP
next y


'Initialise lcd update timer and Input timer
timer0 500, Update_lcd
timer1 10, scan_input
wait

update_lcd:
a$ =  "5¢ = " + str$(vcoin(0))+ ", 10¢ = " + str$(vcoin(1))+ ", 20¢ = " + str$(vcoin(2))+ ", 50¢ = " + str$(vcoin(3))+ ", $1 = " + str$(vcoin(4))+ ", $2 = " + str$(vcoin(5) )

a$ = "                "+a$+"                "
z =z +1
if z=(len(a$) -16) then z = 1 

LCD.PRINT 1, 1, mid$(a$,z,16)

total = (0.05* vcoin(0))+(0.1*vcoin(1))+(0.2*vcoin(2)) + (0.5*vcoin(3))+vcoin(4)+(2*vcoin(5))

lcd.print 1,2, "                "
lcd.prinT 1,2, "Total = " + str$(total)

return

scan_input:


'scans for coins 
for y = 0 to 5
if pin(coin(y)) = 0 and lcoin(y) = 1 then lcoin(y) = 0: vcoin(y) = vcoin(y)+1
lcoin(y) = pin(coin(y))
next y

'scans for reset button
if pin(coin(6)) = 0 and lcoin(6) = 1 then lcoin(6) = 0: gosub clear_all


return



clear_all:
for y = 0 to 5
vcoin(y) = 0
next

return

You do not have the required permissions to view the files attached to this post.
I love this community!
User avatar
PeterN
Posts: 642
Joined: Mon Feb 08, 2021 7:56 pm
Location: Krefeld, Germany
Has thanked: 297 times
Been thanked: 360 times
Contact:

Re: Coin counter on ESP 8266

Post by PeterN »

Nice idea!

How are the coins moved onto the inclined plane? Shaken?
What kind of switch do you want to use to detect the falling coins?

I'm not sure about the TX pin of the ESP8266. Pin 1 is used by Annex as the default serial TX output for system messages. Wasn't there possibly a conflict???? I am unsure whether this mode is reliably deactivated by PIN.MODE 1, INPUT, PULLUP?
But perhaps you have already been able to test this successfully?

My biggest problem would definitely be getting together that many coins for a good test drive :D :-)
User avatar
Basicboy
Posts: 179
Joined: Sat Jul 20, 2024 11:42 am
Location: Australia
Has thanked: 56 times
Been thanked: 13 times

Re: Coin counter on ESP 8266

Post by Basicboy »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Tue Aug 13, 2024 9:55 am Nice idea!

How are the coins moved onto the inclined plane? Shaken?
What kind of switch do you want to use to detect the falling coins?
I haven't built the actual body yet. Yes, I plan to have it shaken by a wobbly fan.
No switches. I'll just use the coin's conductivity to close the circuit.
I'm not sure about the TX pin of the ESP8266. Pin 1 is used by Annex as the default serial TX output for system messages. Wasn't there possibly a conflict???? I am unsure whether this mode is reliably deactivated by PIN.MODE 1, INPUT, PULLUP?
But perhaps you have already been able to test this successfully?
I have tested it successfully. It works fine. The ESP8266 help file says that TX and RX can be redefined as input pins, luckily for me. I was afraid I'll run out of pins and have to use an ESP32!
image.png
You do not have the required permissions to view the files attached to this post.
I love this community!
BeanieBots
Posts: 522
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 292 times
Been thanked: 163 times

Re: Coin counter on ESP 8266

Post by BeanieBots »

I concur with PeterN about the pin assigments.
Avoid any pins that are natively used already such as Tx/Rx.
Also, the internal pullups are very weak. It's not "good practice" to rely solely on them. Add 4k7 or 10k externally as well to avoid nuisance tripping.
Also on the subject of nuisance tripping, how will you counteract for contact bounce?
I see you are polling every 10ms. That is about the same as a 'typical' contact bounce. Not sure how well that will turn out.
It will certainly keep the 8266 extremely busy, probably to the point that there will be no time left for anything else such as updating the display or even using the editor page! Anything below several hundred is fraught with danger.
Maybe consider using interrupts. Then you will need to consider contact bounce either in software or better in hardware.
User avatar
PeterN
Posts: 642
Joined: Mon Feb 08, 2021 7:56 pm
Location: Krefeld, Germany
Has thanked: 297 times
Been thanked: 360 times
Contact:

Re: Coin counter on ESP 8266

Post by PeterN »

I was just about to write down my experiences with bouncing switches ... but Beaniebots has summarised them perfectly. :-)
Post Reply