Can someone please help me with my sketch

I'm new to Arduino and programming. I'm trying to make a coin counter with a coin acceptor and display the total amount on an LCD. I copied someone else's code and it works, the only problem is, They use Pounds instead of US Cents. I was able to get the right value for each coin but for example, 1Cent displays as 1.00 instead of .1
I was able to get it to display without the decimal so it would show just 1 but after it reaches $1 it displays it as 100 instead of 1.00 It's driving me crazy. any help is greatly appreciated.

#include <Arduino.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>

const int coinPin = 2;
volatile int impulsCount = 0;
int coinType = 0;
float totalAmount = 0.0;
float coinValue;


const int coinPulses[] = {1, 2, 5, 10, 10};
const float coinValues[] = {1.0, 2.0, 5.0, 10.0, 10.0};


int lookup(int pulses) {
  for (int i = 0; i < sizeof(coinPulses) / sizeof(coinPulses[0]); i++) {
    if (pulses == coinPulses[i]) {
      return coinValues[i];
    }
  }
  return -1;
}


LiquidCrystal_I2C lcd(0x27, 16, 2);


void setup() {
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(coinPin), coinInterrupt, FALLING);
  totalAmount = 0.0;
  EEPROM.write(0, 0);
  lcd.init();
  lcd.clear();
  lcd.backlight();
}


void coinInterrupt() {
  impulsCount++;
}


void loop() {
  if (impulsCount == coinPulses[coinType]) {
    coinValue = lookup(impulsCount);
    if (coinValue > 0) {
      totalAmount += coinValue;
    }
    delay(100);
    Serial.print("Total Amount: ");
    Serial.println(totalAmount, 2);
    impulsCount = 0;
    lcd.setCursor(0, 0);
    lcd.print("Total Balance: ");
    lcd.setCursor(0, 1);
    lcd.print(totalAmount, 2);
  }
}`Use code tags to format code for the forum`

It sounds like you didn't get the values right.

And the code and values you posted looks much like the code here:

Maybe check your coinValues[]?

I tried that. when I change the value, the LCD shows 0.00 when i put coins in.
for some reason His values are 1, 2, 5, 10, 10 but all the US coins match. the only problem is the position of the decimal point on the display. I've even tried playing around with the Set Cursor position with no luck.
And yes that's the exact code I used

Shouldn't the coin values be 0.25, 0.10, 0.05 and 0.01 for US coins? That's quarters, dimes, nickels, and pennies.

I don't know of any 2 dollar US coins so I'm sure 2.0 doesn't fit.

1, 2, 5, and 10 sound like bill amounts.

You are correct. They use dollar coins or "pounds' I believe. But I'm trying to convert it to Cents. it wont let me change the coin value. if I change it the display shows 0.00 for every coin

If you keep adding whole numbers then you shouldn't expect to see anything after the decimal.

1 Like

I don't know how to change it

It is terrible code. It only sets coinType once:

and never sets it to anything else, so this if statement:

only works for coinPulses[0], and since this function only returns integers, it won't work with fractional dollars.

Maybe they cribbed it off ChatGPT and are pawning it off as functional.

2 Likes

Put your cursor after one of the values in that array and press delete until it is gone. Then type in the decimal value that is appropriate. Repeat for each of the coin values in the array.

Do you know of any way to change it to make it work? I will probably figure it out eventually but I have no clue what I'm doing. I'm slowly learning

Change this function to return float instead of int. Then use this function in loop to lookup the values.

1 Like

I don't have the hardware for testing, but structurally it's missing a way to detect which pulse code matches which coin.

It looks like this code might have been copied from something else that scanned through the different coin types, and maybe you could find that somewhere.

Either that or start over. Maybe expand your interrupt to record the time of the last trigger, and then you could just use impulsCount directly, like these completely untested snippets:

void coinInterrupt() {
  impulsCount++;
  lastFallMs = millis();
}

...
void loop(void){
  ...
  if(impulsCount> 0 && millis() - lastFallMillis > 100){
     value = conValues[impulsCount];
     ...


And:

And if you want more help, maybe edit your post to make the code be fully within the code tags, and edit the title to something useful and non-generic like:

Broken coin acceptor sketch