Advertisement
pleasedontcode

**Coin Counter** rev_01

Nov 5th, 2024
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Coin Counter**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-06 02:07:31
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* this is for arduino uno coin counter */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>                  // Required for I2C communication
  27. #include <LiquidCrystal_I2C.h>    // Library for I2C LCD
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateLCD();                // Function prototype for updating LCD
  33. void displayCounting();           // Function prototype for displaying counting
  34.  
  35. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  36. LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address, 16 column, 2 row display
  37.  
  38. const int incrementButton = 25;   // GPIO pin for Increment button
  39. const int startStopButton = 26;    // GPIO pin for Start/Stop button
  40. const int relayPin = 27;           // GPIO pin for Relay to control coin hopper
  41. const int coinHopperPin = 14;      // GPIO pin for Coin hopper pulse sensor
  42. int coinCountLimit = 0;            // Max coins to be counted
  43. int currentCount = 0;              // Current count of coins
  44. bool counting = false;             // State variable to check if counting is active
  45. bool coinDetected = false;         // Variable to track coin pulse
  46.  
  47. void setup(void)
  48. {
  49.     // put your setup code here, to run once:
  50.     // Initialize LCD
  51.     lcd.init();
  52.     lcd.backlight();
  53.  
  54.     // Initialize button and coin hopper sensor pins
  55.     pinMode(incrementButton, INPUT_PULLUP);
  56.     pinMode(startStopButton, INPUT_PULLUP);
  57.     pinMode(relayPin, OUTPUT);
  58.     pinMode(coinHopperPin, INPUT_PULLUP);
  59.  
  60.     // Initialize relay to be off
  61.     digitalWrite(relayPin, LOW);
  62.  
  63.     // Initial display
  64.     updateLCD(); // Set initial state on the LCD
  65. }
  66.  
  67. void loop(void)
  68. {
  69.     // Handle increment button
  70.     if (digitalRead(incrementButton) == LOW) {
  71.         delay(200); // Debounce delay
  72.         coinCountLimit++;
  73.         updateLCD(); // Only update the coin count limit on the LCD
  74.     }
  75.  
  76.     // Handle start/stop button
  77.     if (digitalRead(startStopButton) == LOW) {
  78.         delay(200); // Debounce delay
  79.         if (!counting) {
  80.             // Start counting
  81.             counting = true;
  82.             currentCount = 0; // Reset the current count to 0 when starting
  83.             digitalWrite(relayPin, HIGH); // Turn on the relay to start hopper
  84.             displayCounting(); // Update the LCD to show counting state
  85.         } else {
  86.             // Stop counting
  87.             counting = false;
  88.             digitalWrite(relayPin, LOW); // Turn off the relay
  89.             updateLCD(); // Update back to the original screen
  90.         }
  91.     }
  92.  
  93.     // Detect coin hopper pulse
  94.     if (counting && digitalRead(coinHopperPin) == LOW && !coinDetected) {
  95.         // A coin is detected
  96.         coinDetected = true;
  97.         currentCount++;
  98.         displayCounting(); // Update the counting process on the LCD
  99.         delay(200); // Debounce for coin detection
  100.         // Stop if the limit is reached
  101.         if (currentCount >= coinCountLimit) {
  102.             counting = false;
  103.             digitalWrite(relayPin, LOW); // Stop the coin hopper
  104.             updateLCD(); // Update the LCD to reflect the stop
  105.         }
  106.     }
  107.  
  108.     // Reset coin detection after pulse is cleared
  109.     if (digitalRead(coinHopperPin) == HIGH) {
  110.         coinDetected = false;
  111.     }
  112. }
  113.  
  114. // Function to update the LCD display for coin count limit screen
  115. void updateLCD() {
  116.     lcd.setCursor(0, 0);
  117.     lcd.print("Count: ");
  118.     lcd.print(coinCountLimit);
  119.     lcd.print("        "); // Clear any leftover characters
  120.     lcd.setCursor(0, 1);
  121.     lcd.print("Press To Start");
  122. }
  123.  
  124. // Function to display the counting process on the LCD
  125. void displayCounting() {
  126.     lcd.setCursor(0, 0);
  127.     lcd.print("Count: ");
  128.     lcd.print(currentCount);
  129.     lcd.print("        "); // Clear any leftover characters
  130.     lcd.setCursor(0, 1);
  131.     lcd.print(coinCountLimit);
  132.     lcd.print(" o Press Stop");
  133. }
  134.  
  135. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement