Problem with displaying a coin value on an lcd 16x2

Good evening to all of you! I have to implement a vending machine in a project and i want a helping hand on the programming part. In a past post (in the sensors section) i tried to make my own acceptor using some tcrt5000 sensor, but it failed at some point, so i decided to buy a coin acceptor (CH-926).I have already programmed the coin acceptor for 4 coins (10cent,20cent,50cent & 1euro), so i programmed it with this way: if a 10cent coin is inserted, it will give 1 pulse as an output signal, 2 pulses for a 20cent coin, 3 pulses for 50cent & 4 pulses for 1 euro. I have solved everything except from displaying the coin value on an lcd 16x2. I inserted the pin “coin” of the coin acceptor in pin 2 of arduino uno (pin 0 for interrupts).In the code i have posted below, the only thing i’ve achieved is to display the number of pulses in the lcd but i want to convert it as a coin value. I tried to do in the interrupt coins = millis(); so i would read the length each pulse, when a coin is inserted, in milliseconds but it didn’t work.Any ideas?

Thanks for your time!

Stephen

coin acceptor.txt (737 Bytes)

I almost forgot this…this is another try i did but it also doesnt work…

#include <LiquidCrystal.h>

// Initializing the lcd in the inserted pins
//
const int rs = 12, en = 11, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
volatile float coins = 0;

void setup() {

lcd.begin(16, 2); //Initialize of lcd and connect the pin “coin”
pinMode(2, INPUT_PULLUP); // to pin 2
attachInterrupt(0, coinInserted, FALLING); //Interrupt for the coins
lcd.setCursor(5,0);
lcd.print(“Credits”);
}

void coinInserted() //This is the interrupt i want to make
{
delay(2000);
coins=coins + 0.1;
}

void loop() { //…and displaying the coin on the lcd
lcd.setCursor(1,0);
lcd.print(coins);

}

With this code, the only thing i managed to display everything as float…it works when i insert 10 cent and 20 cents…thinking of changing the programming of the coin acceptor…

Well...I've just re-programmed the coin acceptor and it works perfect!!I changed the pulses which are given from the coin acceptor, according to the value of each coin (1 pulse for 10 cents,2 pulses for 20 cents, 5 pulses for 50cents and 10 pulses for 1 euro)...and with that little change of the value from int to float it works right as i want!!!When i will finish my project, i will post my whole project and the code!!Thanks to everyone who read my post!! (despite that no-one posted smth). :P

You may like o contact @Ruffsta. He was doing a bit of foot stamping, and didn’t really understand how the coin count pulses worked - before he spat the dummy and wandered off I’m sure he’d appreciate any input. There were a lot of suggestions in his thread which may be useful to you as well.

Congrats on your persistence paying off.

Steph44: Well...I've just re-programmed the coin acceptor and it works perfect!!I changed the pulses which are given from the coin acceptor, according to the value of each coin (1 pulse for 10 cents,2 pulses for 20 cents, 5 pulses for 50cents and 10 pulses for 1 euro)...and with that little change of the value from int to float it works right as i want!!!

Here is some advice for you: https://husobee.github.io/money/float/2016/09/23/never-use-floats-for-currency.html For you, I recommend using an int (or, for large amounts, a long) to keep track of money in cents, and using a float only for display. (Divide integer cents by 100.0 to convert to a float for display.)

Well, thanks once again a lot for your suggestions! After a lot of hours, i finally managed to make it work as i want. Before describing my new problem, let me do a little introduction of my project…I have to make a vending machine with 3 positions (for 3 products) and my problem is how to move a servo when only the right value has been inserted in the coin acceptor…For example, the 1st position need 50cents, the 2nd needs 80cents and the 3rd 1 euro & 30 cents.It sounds easy but when i tried to do it with (if…else if) things didnt go as i wanted. In the attachment below, it’s the code i’ve made on my own…excuse me for that question cause it sounds easy but i’m a little new one on arduino…When i insert 50cents, i have a green led that lights up, so that means it is ready to move the 1st servo…but if i want to insert 80 cents it doesnt let me move the 2nd servo. Of course, i will continue to search it on my own until i find a solution.
I tried to write a lot of commends to understand the whole situation as more as u can.

Thanks a lot for your time!

vending_project.ino (3.35 KB)

Steph44: When i insert 50cents, i have a green led that lights up, so that means it is ready to move the 1st servo...but if i want to insert 80 cents it doesnt let me move the 2nd servo.

In case you didn't read the link I gave you: It is a bad idea to keep track of money using floats. Use an int or long instead, and store the amount as cents.

Also, why all the delays in your code? In particular, why delay for two whole seconds in coinInserted()?

odometer: Also, why all the delays in your code? In particular, why delay for two whole seconds in coinInserted()?

+1

The hallmarks of an ISR are: 1. it does one or a few very (computationally) simple things and therefore, 2. it's fast. The preferred approach is to set a flag in the ISR and handle the detected condition somewhere in loop() code.

Try adding one or more Serial.print statements to monitor the accepted coin value as you go along.