good day everyone..thank you for accepting me in this forum...i just need help for my project i am creating a arduino coin counter, a project dispensing coins i am using arduino uno, i2c 16x2, 3 buttons for add, subtract and start button coin hopper for dispensing coins and non inverted relay module...i created a code but the problem of my project is when i power up the counter the hopper is started dispensing even i am not pressing any button..i try to change the logic of my relay which i change it to high since the relay is non inverted but the problem is still there the hopper automatic dispensing without pressing any button..please i need your help for this project...this is the code i am using
#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)
}
this is the diagram i am using