Coin counter project for school

what is with "L" LED? is it blinking when coins jumping out?

1 Like

yes

can i add this skitch of yours to my skitch sir?

what should your sketch do?

coin counter sir

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

#define ADD_BUTTON_PIN 5         // Pin for Add button
#define SUBTRACT_BUTTON_PIN 6    // Pin for Subtract button
#define START_BUTTON_PIN 7       // Pin for Start button
#define RELAY_PIN A0              // Pin for relay control
#define COIN_HOPPER_PIN 8        // Pin for coin hopper pulse

int coinCount = 0;               // Coin count
int stopCount = 0;               // Stop count (Starts from 0)
bool counting = false;           // State of the counter (counting or not)

// LCD setup (16x2 display with I2C address 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Debounce variables
unsigned long lastAddButtonTime = 0;
unsigned long lastSubtractButtonTime = 0;
unsigned long lastStartButtonTime = 0;
unsigned long debounceDelay = 200;  // 200 ms debounce delay

void setup() {
  // Initialize the LCD
  lcd.init();
  lcd.backlight();
  
  // Initialize button and relay pins
  pinMode(ADD_BUTTON_PIN, INPUT_PULLUP);
  pinMode(SUBTRACT_BUTTON_PIN, INPUT_PULLUP);
  pinMode(START_BUTTON_PIN, INPUT_PULLUP);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(COIN_HOPPER_PIN, INPUT_PULLUP);

  // Start with the relay off
  digitalWrite(RELAY_PIN, LOW);
  
  // Initialize LCD display
  displayLCD();
}

void loop() {
  // Button handling
  handleButtons();

  // Check if the counting has started
  if (counting) {
    // Check for coin pulses from the coin hopper
    if (digitalRead(COIN_HOPPER_PIN) == LOW) {
      delay(50);  // Debounce the coin hopper signal
      if (digitalRead(COIN_HOPPER_PIN) == LOW) {
        coinCount++;
        displayLCD();
      }
    }
    
    // Stop counting when the desired count is reached
    if (coinCount >= stopCount) {
      counting = false;
      digitalWrite(RELAY_PIN, LOW);  // Turn off the relay
    }
  }
}

// Function to handle button presses for adding, subtracting, and starting
void handleButtons() {
  unsigned long currentMillis = millis();
  
  // Add button
  if (digitalRead(ADD_BUTTON_PIN) == LOW && (currentMillis - lastAddButtonTime) > debounceDelay) {
    lastAddButtonTime = currentMillis;
    stopCount++;  // Increase stopCount by 1
    displayLCD();
  }

  // Subtract button
  if (digitalRead(SUBTRACT_BUTTON_PIN) == LOW && (currentMillis - lastSubtractButtonTime) > debounceDelay) {
    lastSubtractButtonTime = currentMillis;
    if (stopCount > 0) stopCount--;  // Decrease stopCount but never below 0
    displayLCD();
  }

  // Start button
  if (digitalRead(START_BUTTON_PIN) == LOW && (currentMillis - lastStartButtonTime) > debounceDelay) {
    lastStartButtonTime = currentMillis;
    startCounting();
  }
}

// Function to start the counting process
void startCounting() {
  counting = true;
  coinCount = 0;                   // Reset coin count
  digitalWrite(RELAY_PIN, HIGH);    // Turn on the relay
  displayLCD();
}

// Function to display the information on the LCD
void displayLCD() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Counter : ");
  lcd.print(coinCount);             // Display current coin count
  lcd.setCursor(0, 1);
  lcd.print("Stop Cnt: ");
  lcd.print(stopCount);             // Display the stop count (starts from 0)
}`

here`s my code sir

image
when i change this line to high the relay is still on

why it counting only to given amount?

1 Like
#include <LiquidCrystal_I2C.h>

#define ADD_BUTTON_PIN 5         // Pin for Add button
#define SUBTRACT_BUTTON_PIN 6    // Pin for Subtract button
#define START_BUTTON_PIN 7       // Pin for Start button
#define RELAY_PIN A0              // Pin for relay control
#define COIN_HOPPER_PIN 8        // Pin for coin hopper pulse

int coinCount = 0;               // Coin count
int stopCount = 0;               // Stop count (Starts from 0)
bool counting = false;           // State of the counter (counting or not)

// LCD setup (16x2 display with I2C address 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Debounce variables
unsigned long lastAddButtonTime = 0;
unsigned long lastSubtractButtonTime = 0;
unsigned long lastStartButtonTime = 0;
unsigned long debounceDelay = 200;  // 200 ms debounce delay

void setup() {
  // Initialize the LCD
  lcd.init();
  lcd.backlight();

  // Initialize button and relay pins
  pinMode(ADD_BUTTON_PIN, INPUT_PULLUP);
  pinMode(SUBTRACT_BUTTON_PIN, INPUT_PULLUP);
  pinMode(START_BUTTON_PIN, INPUT_PULLUP);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(COIN_HOPPER_PIN, INPUT_PULLUP);

  // Start with the relay off
  digitalWrite(RELAY_PIN, LOW);

  // Initialize LCD display
  displayLCD();
}

void loop() {
  if (counting) {
    // Check for coin pulses from the coin hopper
    if (digitalRead(COIN_HOPPER_PIN) == LOW) {
      coinCount++;
      displayLCD();
      delay(50);  // Debounce the coin hopper signal
      while (digitalRead(COIN_HOPPER_PIN) == LOW); // waiting until pulse ends
    }

    // Stop counting when the desired count is reached
    if (coinCount >= stopCount) {
      counting = false;
      digitalWrite(RELAY_PIN, LOW);  // Turn off the relay
    }
  } else handleButtons();
}

// Function to handle button presses for adding, subtracting, and starting
void handleButtons() {
  unsigned long currentMillis = millis();

  // Add button
  if (digitalRead(ADD_BUTTON_PIN) == LOW && (currentMillis - lastAddButtonTime) > debounceDelay) {
    lastAddButtonTime = currentMillis;
    stopCount++;  // Increase stopCount by 1
    displayLCD();
  }

  // Subtract button
  if (digitalRead(SUBTRACT_BUTTON_PIN) == LOW && (currentMillis - lastSubtractButtonTime) > debounceDelay) {
    lastSubtractButtonTime = currentMillis;
    if (stopCount > 0) {
      stopCount--;  // Decrease stopCount but never below 0
      displayLCD();
    }
  }

  // Start button
  if (digitalRead(START_BUTTON_PIN) == LOW && (currentMillis - lastStartButtonTime) > debounceDelay) {
    lastStartButtonTime = currentMillis;
    counting = true;
    coinCount = 0;                   // Reset coin count
    digitalWrite(RELAY_PIN, HIGH);    // Turn on the relay
    displayLCD();
  }
}

// Function to display the information on the LCD
void displayLCD() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Counter : ");
  lcd.print(coinCount);             // Display current coin count
  lcd.setCursor(0, 1);
  lcd.print("Stop Cnt: ");
  lcd.print(stopCount);             // Display the stop count (starts from 0)
}
1 Like

yes sir..counting by pieces

did you change anything in the sketch sir?

maybe

where? i only see the messing in the original sketch in the include wire.h

messing?

missing

if you replace your sketch in post #46 with mine and click
grafik
symbol, then it shows comparison window and any changes will be marked

1 Like

i try the code sir..the problem is in first attempt the counting is working well but when i press again the start button the lcd will go off and on again the hopper starts then stop

i need to reboot the arduino to back to normal and the L led in the arduino is not blinking when there is coin passing the coin hopper sensor

in code it is not present, so your problem is for sure electrical nature. how do you powering your project? make new diagram


here sir