Advertisement
pleasedontcode

**Button Control** rev_01

Nov 22nd, 2024
249
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: **Button Control**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-22 15:34:59
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* 20*4 lcd i2c, ds3231 rtc, 3 switches to 3 outputs, */
  21.     /* 1 switch to pump motor with low level and high */
  22.     /* level input controls */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <LiquidCrystal_I2C.h>
  30. #include <RTClib.h>
  31. #include <EasyButton.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. // Constants for pin assignments
  38. const int switch1Pin = 2; // Pin for switch 1
  39. const int switch2Pin = 3; // Pin for switch 2
  40. const int switch3Pin = 4; // Pin for switch 3
  41. const int pumpSwitchPin = 5; // Pin for pump control
  42. const int output1Pin = 6; // Output pin for switch 1
  43. const int output2Pin = 7; // Output pin for switch 2
  44. const int output3Pin = 8; // Output pin for switch 3
  45.  
  46. // Create LCD object
  47. LiquidCrystal_I2C lcd(0x27, 20, 4); // Change the address according to your LCD
  48.  
  49. // Create RTC object
  50. RTC_DS3231 rtc;
  51.  
  52. // Create EasyButton objects
  53. EasyButton button1(switch1Pin);
  54. EasyButton button2(switch2Pin);
  55. EasyButton button3(switch3Pin);
  56. EasyButton pumpButton(pumpSwitchPin);
  57.  
  58. void setup(void)
  59. {
  60.     // Initialize the LCD
  61.     lcd.begin(20, 4);
  62.     lcd.backlight();
  63.     lcd.print("System Init...");
  64.  
  65.     // Initialize the RTC
  66.     if (!rtc.begin()) {
  67.         lcd.print("RTC not found!");
  68.         while (1);
  69.     }
  70.  
  71.     // Initialize buttons
  72.     button1.begin();
  73.     button2.begin();
  74.     button3.begin();
  75.     pumpButton.begin();
  76.  
  77.     // Set pin modes for outputs
  78.     pinMode(output1Pin, OUTPUT);
  79.     pinMode(output2Pin, OUTPUT);
  80.     pinMode(output3Pin, OUTPUT);
  81.     pinMode(pumpSwitchPin, OUTPUT);
  82. }
  83.  
  84. void loop(void)
  85. {
  86.     // Update button states
  87.     button1.update();
  88.     button2.update();
  89.     button3.update();
  90.     pumpButton.update();
  91.  
  92.     // Handle button presses
  93.     if (button1.isPressed()) {
  94.         digitalWrite(output1Pin, HIGH);
  95.         lcd.setCursor(0, 1);
  96.         lcd.print("Output 1 ON   ");
  97.     } else {
  98.         digitalWrite(output1Pin, LOW);
  99.         lcd.setCursor(0, 1);
  100.         lcd.print("Output 1 OFF  ");
  101.     }
  102.  
  103.     if (button2.isPressed()) {
  104.         digitalWrite(output2Pin, HIGH);
  105.         lcd.setCursor(0, 2);
  106.         lcd.print("Output 2 ON   ");
  107.     } else {
  108.         digitalWrite(output2Pin, LOW);
  109.         lcd.setCursor(0, 2);
  110.         lcd.print("Output 2 OFF  ");
  111.     }
  112.  
  113.     if (button3.isPressed()) {
  114.         digitalWrite(output3Pin, HIGH);
  115.         lcd.setCursor(0, 3);
  116.         lcd.print("Output 3 ON   ");
  117.     } else {
  118.         digitalWrite(output3Pin, LOW);
  119.         lcd.setCursor(0, 3);
  120.         lcd.print("Output 3 OFF  ");
  121.     }
  122.  
  123.     // Handle pump control
  124.     if (pumpButton.isPressed()) {
  125.         digitalWrite(pumpSwitchPin, HIGH); // Activate pump
  126.         lcd.setCursor(0, 0);
  127.         lcd.print("Pump ON       ");
  128.     } else {
  129.         digitalWrite(pumpSwitchPin, LOW); // Deactivate pump
  130.         lcd.setCursor(0, 0);
  131.         lcd.print("Pump OFF      ");
  132.     }
  133.  
  134.     delay(100); // Small delay to debounce
  135. }
  136.  
  137. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement