Advertisement
pleasedontcode

**Coin Counter** rev_01

Nov 5th, 2024
100
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: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2024-11-06 01:01:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* this is a coin counter project the counting coins */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>
  27. #include <LiquidCrystal_I2C.h> // LCD address, 16 column, 2 row display
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateLCD();
  33. void displayCounting();
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t Increment_PushButton_PIN_D1 = 1;
  37. const int incrementButton = 25;  // GPIO pin for Increment button
  38. const int startStopButton = 26;   // GPIO pin for Start/Stop button
  39. const int relayPin = 27;          // GPIO pin for Relay to control coin hopper
  40. const int coinHopperPin = 14;     // GPIO pin for Coin hopper pulse sensor
  41. int coinCountLimit = 0;           // Max coins to be counted
  42. int currentCount = 0;             // Current count of coins
  43. bool counting = false;            // State variable to check if counting is active
  44. bool coinDetected = false;        // Variable to track coin pulse
  45.  
  46. LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD
  47.  
  48. void setup(void)
  49. {
  50.     // put your setup code here, to run once:
  51.     pinMode(Increment_PushButton_PIN_D1, INPUT_PULLUP);
  52.    
  53.     // Initialize LCD
  54.     lcd.init();
  55.     lcd.backlight();
  56.    
  57.     // Initialize button and coin hopper sensor pins
  58.     pinMode(incrementButton, INPUT_PULLUP);
  59.     pinMode(startStopButton, INPUT_PULLUP);
  60.     pinMode(relayPin, OUTPUT);
  61.     pinMode(coinHopperPin, INPUT_PULLUP);
  62.    
  63.     // Initialize relay to be off
  64.     digitalWrite(relayPin, LOW);
  65.    
  66.     // Initial display
  67.     updateLCD(); // Set initial state on the LCD
  68. }
  69.  
  70. void loop(void)
  71. {
  72.     // put your main code here, to run repeatedly:
  73.    
  74.     // Handle increment button
  75.     if (digitalRead(incrementButton) == LOW) {
  76.         delay(200);  // Debounce delay
  77.         coinCountLimit++;
  78.         updateLCD();  // Only update the coin count limit on the LCD
  79.     }
  80.    
  81.     // Handle start/stop button
  82.     if (digitalRead(startStopButton) == LOW) {
  83.         delay(200);  // Debounce delay
  84.         if (!counting) {
  85.             // Start counting
  86.             counting = true;
  87.             currentCount = 0;          // Reset the current count to 0 when starting
  88.             digitalWrite(relayPin, HIGH);  // Turn on the relay to start hopper
  89.             displayCounting();  // Update the LCD to show counting state
  90.         } else {
  91.             // Stop counting
  92.             counting = false;
  93.             digitalWrite(relayPin, LOW);   // Turn off the relay
  94.             updateLCD();                   // Update back to the original screen
  95.         }
  96.     }
  97.    
  98.     // Detect coin hopper pulse
  99.     if (counting && digitalRead(coinHopperPin) == LOW && !coinDetected) {
  100.         // A coin is detected
  101.         coinDetected = true;
  102.         currentCount++;
  103.         displayCounting();  // Update the counting process on the LCD
  104.         delay(200);  // Debounce for coin detection
  105.         // Stop if the limit is reached
  106.         if (currentCount >= coinCountLimit) {
  107.             counting = false;
  108.             digitalWrite(relayPin, LOW);   // Stop the coin hopper
  109.             updateLCD();                   // Update the LCD to reflect the stop
  110.         }
  111.     }
  112.    
  113.     // Reset coin detection after pulse is cleared
  114.     if (digitalRead(coinHopperPin) == HIGH) {
  115.         coinDetected = false;
  116.     }
  117. }
  118.  
  119. // Function to update the LCD display for coin count limit screen
  120. void updateLCD() {
  121.     lcd.setCursor(0, 0);
  122.     lcd.print("Count: ");
  123.     lcd.print(coinCountLimit);
  124.     lcd.print("   ");  // Clear any leftover characters
  125.     lcd.setCursor(0, 1);
  126.     lcd.print("Press To Start");
  127. }
  128.  
  129. // Function to display the counting process on the LCD
  130. void displayCounting() {
  131.     lcd.setCursor(0, 0);
  132.     lcd.print("Count: ");
  133.     lcd.print(currentCount);
  134.     lcd.print("   ");  // Clear any leftover characters
  135.     lcd.setCursor(0, 1);
  136.     lcd.print(coinCountLimit);
  137.     lcd.print(" o Press Stop");
  138. }
  139.  
  140. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement