#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 20x4 LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Pin assignments
const int coinSlotPin = 2; // Pin connected to the coin slot
const int startButtonPin = 3;
const int stopButtonPin = 4;
const int resetButtonPin = 5;
const int hopperControlPin = 6; // Pin to control the coin hopper (e.g., via a relay)
// Variables for coin counts
int peso1 = 0;
int peso5 = 0;
int peso10 = 0;
int totalCoins = 0;
bool machineRunning = false;
// Timer variables
unsigned long lastCoinTime = 0; // Stores the time when the last coin was detected
unsigned long autoStopInterval = 5000; // Auto-stop interval (5 seconds)
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize pins
pinMode(coinSlotPin, INPUT);
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(stopButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(hopperControlPin, OUTPUT);
// Show start screen
displayStartScreen();
delay(2000); // Show start screen for 2 seconds
// Show paused screen
displayPausedScreen();
}
void loop() {
// Check if the start button is pressed
if (digitalRead(startButtonPin) == LOW && !machineRunning) {
machineRunning = true;
digitalWrite(hopperControlPin, HIGH); // Turn on the coin hopper
lastCoinTime = millis(); // Reset the coin detection timer
}
// Check if the stop button is pressed
if (digitalRead(stopButtonPin) == LOW && machineRunning) {
stopMachine();
}
// Check if the reset button is pressed
if (digitalRead(resetButtonPin) == LOW) {
resetCounts();
displayPausedScreen(); // Refresh display with reset values
}
// Detect coins when the machine is running
if (machineRunning) {
detectCoin();
// Auto-stop the machine if no coins are detected for a certain period
if (millis() - lastCoinTime > autoStopInterval) {
stopMachine(); // Stop the machine if no coins are detected within the auto-stop interval
}
}
}
// Function to stop the machine
void stopMachine() {
machineRunning = false;
digitalWrite(hopperControlPin, LOW); // Turn off the coin hopper
}
// Function to display the start screen
void displayStartScreen() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("COIN SORTER v1.0");
lcd.setCursor(0, 1);
lcd.print("by Elexhub");
lcd.setCursor(0, 2);
lcd.print("Electronics");
}
// Function to display the paused screen with counts
void displayPausedScreen() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PAUSED +Total: ");
lcd.print(totalCoins); // Show the total number of coins
// Display individual counts for each denomination
lcd.setCursor(0, 1);
lcd.print(" 1 Peso: ");
lcd.print(peso1);
lcd.setCursor(0, 2);
lcd.print(" 5 Peso: ");
lcd.print(peso5);
lcd.setCursor(0, 3);
lcd.print("10 Peso: ");
lcd.print(peso10);
}
// Function to reset all counts
void resetCounts() {
peso1 = 0;
peso5 = 0;
peso10 = 0;
totalCoins = 0;
}
// Function to detect and count coins
void detectCoin() {
// Example detection logic for a coin slot
// Here you would add the actual code to detect which coin was inserted
// For now, we'll simulate this with random coin detections
int detectedCoin = random(1, 4); // Simulate detecting 1 Peso, 5 Peso, or 10 Peso coins
if (detectedCoin == 1) {
peso1++;
} else if (detectedCoin == 2) {
peso5++;
} else if (detectedCoin == 3) {
peso10++;
}
totalCoins = peso1 + peso5 + peso10;
displayPausedScreen(); // Update display after detecting a coin
lastCoinTime = millis(); // Reset the coin detection timer
delay(1000); // Simulate delay between coin insertions
}