Hi, I am making a vending machine for a school project and I am using a coin acceptor I bought from e-Bay for the money aspect of the machine. I want it to accept a specific amount of money and then the person can choose their snack with a keypad. The problem is that when the amount of money reaches the "target amount" it would need to go over the amount so it would activate the next event. I feel like it is the floats but I just have no idea. Thank you in advance.
Here the relevant part of my code(If anyone needs the full code I can post that too):
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// Constants
const int coinpin = 2;
const int ledpin = 3;
const float targetcents = 1.00;
// Variables
volatile float cents = 0.00;
int credits = 0;
// Setup
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
lcd.backlight();
pinMode(coinpin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
pinMode(ledpin, OUTPUT);
}
// Main loop
void loop() {
// If we've hit our target amount of coins, increment our credits and reset the cents counter
if (cents >= targetcents) {
credits = credits + 1;
cents = cents - targetcents;
}
// If we haven't reached our target, keep waiting...
else {
}
// Debugging zone
Serial.println(cents);
lcd.setCursor(0,0);
lcd.print("Insert Coin");
lcd.setCursor(0, 1);
lcd.print(cents);
while (credits > 0) {
lcd.setCursor(0,0);
lcd.print("Select Snack:");
delay(2500);
break;
}
}
// Interrupt
void coinInterrupt(){
// Each time a pulse is sent from the coin acceptor, interrupt main loop to add 1 cent and flip on the LED
cents = cents + 0.01;
}
P.S. I am pretty sure it is the code as it would not be the wiring and that is why I didn't post any pictures and schematics of the wiring. But I can post it if we need it to fix the problem.