I have a coin operated coffee vending machine which get the pulses from the coin acceptor.
My project is to make the Arduino esp32 to communicate with the coffee vending machine by transfer the pulses from the coin acceptor to the coffee machine.
I followed this link to control and monitor the coin acceptor and doing some tricks but I need the code to transfer the pulses to the coffee vending machine.
I want to collect information like daily sales and count number of coins inserted and save the data to a memory or send it via httpclient to api.
and you're right about (A piece of wire might suffice) it's only one wire that need to be connected between the esp32 and the coffee machine but I don't know how to make the correct coding for this.
It sounds like what you are doing is reading in pulses from the coin acceptor, doing your data analysis and storage, then recreating the pulses as output to the coffee machine. Why not just allow the pulses from the coin acceptor to the coffee machine and read the pulses as they go by? No need to intercept the pulses, just observe them.
I know that I can do data analysis like what you say but I want to prompt a question to the person who put the coins and to force him to answer the question using my Arduino project and once he answered then I will allow pulses to proceed to the coffee machine.
I hope that you got my idea.
No differentiation between the coin types, even though it defines them. A coin that causes 10 pulses is registered as ten 1-pulse coins instead.
No accounting for the maximum total time required to receive all the pulses for any of the coins.
Poor handling of the volatile impulsCount variable. It needs a critical section in the main loop() function to avoid missing pulses (current code sets it to zero even though pulses might have occurred during processing of the current pulse).
I've tested it using a Function/Arbitrary Waveform Generator to simulate the coin acceptor.
I refered to the CH-926 Manual to set the pulse parameters.
Note that the CH-926 is the coin acceptor used in the link - we have no confirmation that malekses has the same model - there are lots of similar looking but differant ones available.
The pulse width is selectable via a switch on the coin acceptor - fast: 20ms , medium: 50ms , slow: 100ms.
The number of pulses for each coin type is programmable from 1 to 50 pulses.
I strived to emulate the MCA and write some code that should be more complete than the example. The function(s) to ask questions and afterwards control the coffee machine should be easy to implement ... if you can provide answers to my post #11.
integrated a wait time after the last pulse detected (WaitAfterLastPulseDetected) which is set to 50 here but might have to be adapted to the real situation
added a button to reset the total amount
The MCA Emulation uses digital pin D4 to create the pulses and connects to pin D2 of the ESP32.
[Edit]: The timing of the MACEmulation and the value of WaitAfterLastPulseDetected may have to be adjusted to those mentioned by @JohnLincoln in post #13!
[Edit 2]: I recognized I forgot the IRAM_ATTR attribute for the ISR and added it now on Wokwi in a new version (for some reason I had to move the ISR in front of setup() as it otherwise throws an error when the attribute is inserted...).
If a first (and only one!) pulse has been detected
totalAmount will be increased by 1
for 100 msec the sketch will ignore further pulses
after the delay impulsCount will be set to zero
This way one may count the number of coins but not the values that are coded in the number of pulses (unless the time between two pulses exceeds 100 msec ...).
That should explain why you got all 50 pulses after removing the delay ...
An ESP32 Critical Section using spinlocks is kind of a big gun with relatively high overhead. If you're only accessing the ISR data from a single core, the Arduino noInterrupts() function works just as well with lower overhead.
I think you're right, just wanted to be on the (very) safe side ...
BTW I forgot the IRAM_ATTR but fixed that in the newer Wokwi version and have put a comment in the sketch listed above. Wokwi did no longer recognize the function declaration after inserting the attribute when the declaration was behind setup() (where attachInterrupt() is called). Had to move it before setup() is declared.
Thanks for your post and please feel free to improve the sketch!
Voltage required: 3.3V, 5V or anything else?
Actually I don't know the voltage of the pulse wire that connected between the coin acceptor and coffee machine.
Do you need galvanic isolation?
I'm not sure, but while the coin acceptor connected directly to the coffee machine so I think no need.