Hello,
currently I am trying to making a projekt, where I want to show the value of coins (€), which have been inserted into an CH-926 coin acceptor (2€-10cents) on an I2C LCD. I connected the I2C pins like normal, and the Coin Acceptor pin DC 12v pin to the power supply, the coin pin to 12v and digital pin 2 and ground to ground. Also I am using an Arduino Mega. If I turn the coin Acceptor on, it can detect which coins have been inserted (10ct = 1 Pulse, 50ct = 5 Pulse, 2€ = 20 Pulse e.g.).
Now I want to show the value of coins on the display but I can seem to get it to work properly. With my current code the lcd just shows Impulses: 0 and Total: 4448.84. When a coin is inserted, nothing changes. This is my current code:
This is used in the ISR, so must be declared as volatile.
volatile int impulsCount = 0;
Also, it is a multi-byte variable, so non-interrupt access to it should be protected, though this is also perhaps not a problem, For good measure, get in the habit of making your own safe copy, viz:
noInterrupts(); // turn off interrupts
int myImpuksCount = impulsCount;
interrupts(); // and back on again
then use myImpuksCount everywhere and impulsCount nowhere else.
I think if you step through your code pretending to be really dumb like the microprocessor which will be doing the same thing, you will see where this goes off the rails.
Thanks for answering. But it seems like the problem is something different. Now the Impulses on the LCD seem to be just random Numbers, while the Total won't change after putting in a coin. Any other idea?
OK. Here in setup() you immediately go wrong - whatever you are pulling from EEPROM is nan not a number:
EEPROM.get(0, total_amount);
Comment out every line that does anything with the EEPROM and you'll get a half-step down the road. Think about adding that functionality later - at the very least, you'll have to get a valid number in there to start with.
I put
Serial.begin(115200);
into setup(), then used Serial.print() statments all over to check the values of variables and observe the flow of your program.
Which, without the EEPROM stuff, seems to do what it says.
I don't know how anyome can debug a sketch of even trivial extent without using print statements if more sophisticate tools are unavailable!
What it does now will need some attention. The coin pulses will not necessarily cooperate and come all at once during your delay(1000) "nap"...
Also, EEPROM has limited write cycle lifetime. You may want to rethink and avoid writing to it every time a coin drops or 1000 ms goes by, whatever.