Advertisement
pleasedontcode

**Charging System** rev_01

Nov 5th, 2024
82
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: **Charging System**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-06 01:12:07
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* charging station */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h> // Added 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.  
  33. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  34. LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the I2C address of the LCD
  35.  
  36. // Pin definitions
  37. const int relayPins[4] = {2, 3, 4, 5}; // Relay pins
  38. const int buttonPins[4] = {6, 7, 8, 9}; // Button pins
  39. const int buzzerPin = 10; // Buzzer pin
  40. const int coinSlotPin = 11; // Coin slot pin
  41.  
  42. // Timing variables
  43. unsigned long relayEndTimes[4] = {0, 0, 0, 0}; // End time for each relay
  44. const int chargeTimePerCoin = 120; // 2 minutes per coin in seconds
  45. #define Relay_On(b) digitalWrite(relayPins[b], LOW)
  46. #define RelayOff(b) digitalWrite(relayPins[b], HIGH)
  47.  
  48. void setup(void)
  49. {
  50.     // put your setup code here, to run once:
  51.     lcd.init(); // Initialize the LCD
  52.     lcd.backlight(); // Turn on the backlight
  53.  
  54.     // Set relay and button pins as output/input
  55.     for (int i = 0; i < 4; i++) {
  56.         digitalWrite(relayPins[i], HIGH); // Ensure relays are off
  57.         pinMode(relayPins[i], OUTPUT);
  58.         pinMode(buttonPins[i], INPUT_PULLUP); // Buttons are active LOW
  59.     }
  60.     pinMode(buzzerPin, OUTPUT);
  61.     pinMode(coinSlotPin, INPUT_PULLUP); // Use internal pull-up resistor
  62.  
  63.     // Display initial message on the LCD
  64.     lcd.setCursor(0, 0);
  65.     lcd.print("E-Piso Charger");
  66.     lcd.setCursor(0, 1);
  67.     lcd.print("2 mins/PHP    C: 0");
  68.     lcd.setCursor(0, 2);
  69.     lcd.print("1|00:00 2|00:00");
  70.     lcd.setCursor(0, 3);
  71.     lcd.print("3|00:00 4|00:00");
  72. }
  73.  
  74. void loop(void)
  75. {
  76.     // put your main code here, to run repeatedly:
  77.     const uint32_t tausend = 1000UL;
  78.     static uint32_t CoinDelay = 0;
  79.     static int coinsInserted = 0;
  80.     static uint32_t lastUpdateTime = 0;
  81.  
  82.     // Check for coin insertion
  83.     if (digitalRead(coinSlotPin) == LOW)
  84.         if (millis() - CoinDelay >= 300) { // Confirm coin detection
  85.             coinsInserted++;
  86.             lcd.setCursor(17, 1);
  87.             lcd.print(coinsInserted > 99 ? 99 : coinsInserted);
  88.             tone(buzzerPin, tausend, 100); // Beep the buzzer
  89.             CoinDelay = millis();
  90.         }
  91.  
  92.     for (int i = 0; i < 4; i++) {
  93.         // Check buttons for starting charging
  94.         if (coinsInserted > 0 && digitalRead(buttonPins[i]) == LOW) {
  95.             Relay_On(i); // Turn on the relay
  96.             if (!relayEndTimes[i]) relayEndTimes[i] = millis();
  97.             relayEndTimes[i] += tausend * coinsInserted * chargeTimePerCoin; // Set the end time for the relay based on the number of coins inserted
  98.             coinsInserted = 0; // Reset the coin count after starting the charging
  99.             lcd.setCursor(17, 1);
  100.             lcd.print("0 ");
  101.         }
  102.         // Update relay timing
  103.         if (relayEndTimes[i] > 0)
  104.             if (millis() >= relayEndTimes[i]) {
  105.                 RelayOff(i); // Turn off the relay
  106.                 relayEndTimes[i] = 0; // Reset the relay end time
  107.             }
  108.     }
  109.  
  110.     // Only update the LCD every second
  111.     if (millis() - lastUpdateTime >= tausend) {
  112.         char buffer[6];
  113.         // Update only if values have changed
  114.         for (uint8_t i = 0; i < 4; i++) {
  115.             uint16_t remainingTime = relayEndTimes[i] > 0 ? (relayEndTimes[i] - millis()) / tausend : 0;
  116.             uint8_t row = i < 2 ? 2 : 3;
  117.             uint8_t col = i % 2 == 0 ? 2 : 10;
  118.             lcd.setCursor(col, row);
  119.             sprintf(buffer, "%02u:%02u", remainingTime / 60, remainingTime % 60);
  120.             lcd.print(buffer);
  121.         }
  122.         lastUpdateTime += tausend;
  123.     }
  124. }
  125.  
  126. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement