Arduino coin counter project

good evening everyone...I need help with my new project which is an arduino coin counter using arduino uno, coin hopper, i2c 16x2, 3 buttons for add, subtract and start/push button, relay module, my problem is when the coin counter is booted, the coin hopper starts automatically, I use a non inverted relay module, then the coin hopper doesn't want to stop even when it has reached the coin limit that was set and when I want to pause the coin hopper from counting then I resume again the arduino dies and it reboots again,,I hope you can help me what is wrong with my code,, this is the code I am using

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Pin Definitions
#define ADD_BUTTON 2
#define SUBTRACT_BUTTON 3
#define START_BUTTON 4
#define RELAY_PIN 5
#define COIN_SENSOR_PIN 7

// LCD Address, columns, and rows
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Variables
int coinCount = 0;
int coinLimit = 0;  // Start with 0 coin limit
bool isCounting = false;
bool isPaused = false;
bool buttonState = HIGH;  // Current state of the button
bool lastButtonState = HIGH;  // Previous state of the button
int totalCoins = 0;  // Total coins counted in the current session
bool limitReached = false;  // Tracks if the limit has been reached

void setup() {
  // Initialize Serial for debugging
  Serial.begin(9600);
  
  // Initialize LCD
  lcd.init();
  lcd.backlight();
  
  // Initialize pins
  pinMode(ADD_BUTTON, INPUT_PULLUP);
  pinMode(SUBTRACT_BUTTON, INPUT_PULLUP);
  pinMode(START_BUTTON, INPUT_PULLUP);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(COIN_SENSOR_PIN, INPUT_PULLUP);
  
  // Initial states
  digitalWrite(RELAY_PIN, LOW);

  // Display bootscreen
  showBootScreen();
  
  // Display initial state
  updateDisplay();
  
  // Debug: Display initial message
  Serial.println("System Initialized");
}

void loop() {
  // Check for Add Button Press
  if (digitalRead(ADD_BUTTON) == LOW && !limitReached) {
    coinLimit++;
    updateDisplay();
    Serial.println("Add Button Pressed - Coin Limit Increased");
    delay(200); // Debounce delay
  }

  // Check for Subtract Button Press
  if (digitalRead(SUBTRACT_BUTTON) == LOW && !limitReached) {
    if (coinLimit > 0) coinLimit--;  // Prevent negative limits
    updateDisplay();
    Serial.println("Subtract Button Pressed - Coin Limit Decreased");
    delay(200); // Debounce delay
  }

  // Check for Start/Pause Button Press
  buttonState = digitalRead(START_BUTTON);
  if (buttonState == LOW && lastButtonState == HIGH) {
    if (!isCounting && limitReached) {
      resetCounting();  // Reset count and start a new session after limit reached
      totalCoins = 0;   // Reset total coins for a new session
      startCounting();  // Start counting again
      Serial.println("Start Button Pressed - Counting Started After Limit Reached");
    } else if (!isCounting && !limitReached) {
      startCounting();  // Start counting if not already counting
      Serial.println("Start Button Pressed - Counting Started");
    } else if (isCounting && !isPaused) {
      pauseCounting();  // Pause counting if currently counting
      Serial.println("Start Button Pressed - Counting Paused");
    } else if (isCounting && isPaused) {
      resumeCounting(); // Resume counting if paused
      Serial.println("Start Button Pressed - Counting Resumed");
    }
    delay(200); // Debounce delay
  }
  lastButtonState = buttonState;  // Update last button state

  // Check coin sensor during counting
  if (isCounting && !isPaused && digitalRead(COIN_SENSOR_PIN) == LOW) {
    coinCount++;
    totalCoins++; // Increment total coins counted in the current session
    updateDisplay();
    Serial.print("Coin Inserted - Count: ");
    Serial.println(coinCount);
    delay(200); // Debounce delay to prevent multiple counts for one coin

    // Stop counting if limit is reached
    if (coinCount >= coinLimit) {
      stopCounting();
      Serial.println("Coin Limit Reached - Counting Stopped");
    }
  }
}

void startCounting() {
  // Start counting
  digitalWrite(RELAY_PIN, HIGH);  // Activate relay
  isCounting = true;
  isPaused = false;
  limitReached = false;  // Reset limit reached status
  updateDisplay();  // Update the display
}

void pauseCounting() {
  // Pause counting
  digitalWrite(RELAY_PIN, LOW);  // Deactivate relay
  isPaused = true;
  lcd.setCursor(0, 0);
  lcd.print("Paused         ");  // Show "Paused" on the display
}

void resumeCounting() {
  // Resume counting
  digitalWrite(RELAY_PIN, HIGH);  // Activate relay
  isPaused = false;
  updateDisplay();  // Continue showing normal display
}

void stopCounting() {
  // Stop counting when limit is reached
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Limit Reached!");
  lcd.setCursor(0, 1);
  lcd.print("Total Coins: ");
  lcd.print(totalCoins);  // Display the total coins for the session

  digitalWrite(RELAY_PIN, LOW);  // Deactivate relay
  isCounting = false;
  limitReached = true;  // Set the limit reached flag
}

void resetCounting() {
  coinCount = 0;  // Reset coin count after stopping
  updateDisplay();  // Update to normal display
}

void updateDisplay() {
  // Normal display
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Coin Limit: ");
  lcd.print(coinLimit);
  lcd.setCursor(0, 1);
  lcd.print("Count: ");
  lcd.print(coinCount);
}

void showBootScreen() {
  // Show bootscreen with the name of the coin counter
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("  Coin Counter ");
  lcd.setCursor(0, 1);
  lcd.print("    Welcome!   ");
  delay(5000);  // Display bootscreen for 3 seconds
  
  // Clear the screen after bootscreen
  lcd.clear();
}

Hi, @zwack20

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Can you please post a link to the coin counter module?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

i am using this diagram as a guide

Hi, @zwack20
Sorry but that is not a good diagram to troubleshoot your project.
For example connections to the relay?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Tom.. :smiley: :+1: :coffee: :australia:

i dont have that sir i can provide the picture

Hi, @zwack20
From your code;

Yet your image shows only 5, 6, 7, 8 being used, what about 2, 3, 4.

Get out your pen(cil) and paper and draw a schematic PLEASE.

Tom.. :smiley: :+1: :coffee: :australia:

sorry for that..i change it the picture i send is serve as may guide

look like this, pin 7 is for coin hopper

Hi, @zwack20
Is your relay module a HIGH or LOW active relay?

Can you please tell us your electronics, programming, arduino, hardware experience?

Do you have a DMM? (Digital MultiMeter)

Tom.. :smiley: :+1: :coffee: :australia:

1 Like

i use a non inverted relay module,,is there anyone willing to help me?

i have a experience in hardware and electronics, in terms in arduino programming i still learning

I used your code in this simulator, and since the simulator does not have a coin counter, I used the yellow button as a counter and it worked correctly.
Can you use the simulator and point out to me where the problem is in your project?

thank you,,my problem is when i plug the arduino the coin hopper automatically on, and the coin count of the hopper is not accurate and not even stop when he reached the coin limit that i set

i`m working with this project almost a week i cannot figure out the problem, i am using non inverted relay module, i try high and low the relay still the same result

In simulator do you see any problem?

in the simulator there is no problem but in actual there is

Hi, @zwack20

Please post a link to specs/data and or where you purchased it?

How have you got the output connections to the relay? NC, COM, NC.

Can you post some images of your project?
So we can see your component layout.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

this relay

is that wrong? i am still learning for this??

the one i send earlier is a 24v, but this one, this is the one i use