CCCPaste

**Coin Counter** rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: **Coin Counter**
	- Source Code NOT compiled for: ESP8266 NodeMCU V1.0
	- Source Code created on: 2024-11-06 01:01:37

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* this is a coin counter project the counting coins */
/****** END SYSTEM REQUIREMENTS *****/

/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include 
#include  // LCD address, 16 column, 2 row display

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateLCD();
void displayCounting();

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Increment_PushButton_PIN_D1 = 1;
const int incrementButton = 25;  // GPIO pin for Increment button
const int startStopButton = 26;   // GPIO pin for Start/Stop button
const int relayPin = 27;          // GPIO pin for Relay to control coin hopper
const int coinHopperPin = 14;     // GPIO pin for Coin hopper pulse sensor
int coinCountLimit = 0;           // Max coins to be counted
int currentCount = 0;             // Current count of coins
bool counting = false;            // State variable to check if counting is active
bool coinDetected = false;        // Variable to track coin pulse

LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD

void setup(void)
{
    // put your setup code here, to run once:
    pinMode(Increment_PushButton_PIN_D1, INPUT_PULLUP);
    
    // Initialize LCD
    lcd.init();
    lcd.backlight();
    
    // Initialize button and coin hopper sensor pins
    pinMode(incrementButton, INPUT_PULLUP);
    pinMode(startStopButton, INPUT_PULLUP);
    pinMode(relayPin, OUTPUT);
    pinMode(coinHopperPin, INPUT_PULLUP);
    
    // Initialize relay to be off
    digitalWrite(relayPin, LOW);
    
    // Initial display
    updateLCD(); // Set initial state on the LCD
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    
    // Handle increment button
    if (digitalRead(incrementButton) == LOW) {
        delay(200);  // Debounce delay
        coinCountLimit++;
        updateLCD();  // Only update the coin count limit on the LCD
    }
    
    // Handle start/stop button
    if (digitalRead(startStopButton) == LOW) {
        delay(200);  // Debounce delay
        if (!counting) {
            // Start counting
            counting = true;
            currentCount = 0;          // Reset the current count to 0 when starting
            digitalWrite(relayPin, HIGH);  // Turn on the relay to start hopper
            displayCounting();  // Update the LCD to show counting state
        } else {
            // Stop counting
            counting = false;
            digitalWrite(relayPin, LOW);   // Turn off the relay
            updateLCD();                   // Update back to the original screen
        }
    }
    
    // Detect coin hopper pulse
    if (counting && digitalRead(coinHopperPin) == LOW && !coinDetected) {
        // A coin is detected
        coinDetected = true;
        currentCount++;
        displayCounting();  // Update the counting process on the LCD
        delay(200);  // Debounce for coin detection
        // Stop if the limit is reached
        if (currentCount >= coinCountLimit) {
            counting = false;
            digitalWrite(relayPin, LOW);   // Stop the coin hopper
            updateLCD();                   // Update the LCD to reflect the stop
        }
    }
    
    // Reset coin detection after pulse is cleared
    if (digitalRead(coinHopperPin) == HIGH) {
        coinDetected = false;
    }
}

// Function to update the LCD display for coin count limit screen
void updateLCD() {
    lcd.setCursor(0, 0);
    lcd.print("Count: ");
    lcd.print(coinCountLimit);
    lcd.print("   ");  // Clear any leftover characters
    lcd.setCursor(0, 1);
    lcd.print("Press To Start");
}

// Function to display the counting process on the LCD
void displayCounting() {
    lcd.setCursor(0, 0);
    lcd.print("Count: ");
    lcd.print(currentCount);
    lcd.print("   ");  // Clear any leftover characters
    lcd.setCursor(0, 1);
    lcd.print(coinCountLimit);
    lcd.print(" o Press Stop");
}

/* END CODE */