Advertisement
pleasedontcode

Button Control rev_03

Jan 28th, 2024
70
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 compiled for: Arduino Nano
  14.     - Source Code created on: 2024-01-28 16:52:47
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* On LCD1602_LCD1602I2C_I2C display   on the first */
  21.     /* line: CZERWONY "+resultC"  on the second line: */
  22.     /* ZOLTY "+resultZ"  the initial value of resultC and */
  23.     /* resultZ is 10000. */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* Holding down the czerwonyP_PushButton subtracts */
  26.     /* from the resultC. Holding down the */
  27.     /* zielonyP_PushButton subtracts from the resultZ. */
  28.     /* Length of hold subtracts value: 1 second subtracts */
  29.     /* 10, 5 seconds subtracts 50, 10 seconds subtracts */
  30.     /* 100. */
  31. /****** END SYSTEM REQUIREMENTS *****/
  32.  
  33. /****** DEFINITION OF LIBRARIES *****/
  34. #include <Arduino.h>
  35. #include <Wire.h>
  36. #include <EasyButton.h>
  37. #include <LiquidCrystal_I2C.h>
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42. void updateDisplay(int resultC, int resultZ);
  43. void onPressedForZielonyP();
  44. void onPressedForCzerwonyP();
  45.  
  46. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  47. const uint8_t zielonyP_PushButton_PIN_D2 = 2;
  48. const uint8_t czerwonyP_PushButton_PIN_D3 = 3;
  49.  
  50. /***** DEFINITION OF I2C PINS *****/
  51. const uint8_t LCD1602_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  52. const uint8_t LCD1602_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  53. const uint8_t LCD1602_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
  54.  
  55. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  56. EasyButton zielonyP_PushButton(zielonyP_PushButton_PIN_D2);
  57. EasyButton czerwonyP_PushButton(czerwonyP_PushButton_PIN_D3);
  58.  
  59. LiquidCrystal_I2C lcd(LCD1602_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // Initialize the LiquidCrystal_I2C object
  60.  
  61. int resultC = 10000; // Initial value of resultC
  62. int resultZ = 10000; // Initial value of resultZ
  63.  
  64. void setup(void)
  65. {
  66.   // put your setup code here, to run once:
  67.   pinMode(zielonyP_PushButton_PIN_D2, INPUT_PULLUP);
  68.   pinMode(czerwonyP_PushButton_PIN_D3, INPUT_PULLUP);
  69.  
  70.   lcd.begin(16, 2); // Initialize the LCD
  71.  
  72.   // Initialize the button objects
  73.   zielonyP_PushButton.begin();
  74.   czerwonyP_PushButton.begin();
  75.  
  76.   // Attach the callback functions for onPressedFor
  77.   zielonyP_PushButton.onPressedFor(1000, onPressedForZielonyP);
  78.   czerwonyP_PushButton.onPressedFor(1000, onPressedForCzerwonyP);
  79.  
  80.   // Update the display with the initial values
  81.   updateDisplay(resultC, resultZ);
  82. }
  83.  
  84. void loop(void)
  85. {
  86.   // put your main code here, to run repeatedly:
  87.  
  88.   // Continuously read the status of the buttons
  89.   zielonyP_PushButton.read();
  90.   czerwonyP_PushButton.read();
  91. }
  92.  
  93. void onPressedForZielonyP()
  94. {
  95.   resultZ -= 10;
  96.   updateDisplay(resultC, resultZ);
  97.   delay(100); // Delay for debounce
  98. }
  99.  
  100. void onPressedForCzerwonyP()
  101. {
  102.   resultC -= 10;
  103.   updateDisplay(resultC, resultZ);
  104.   delay(100); // Delay for debounce
  105. }
  106.  
  107. void updateDisplay(int resultC, int resultZ)
  108. {
  109.   lcd.clear(); // Clear the LCD display
  110.  
  111.   lcd.setCursor(0, 0); // Set the cursor to the first line
  112.   lcd.print("CZERWONY +"); // Print the text for resultC
  113.   lcd.print(resultC);
  114.  
  115.   lcd.setCursor(0, 1); // Set the cursor to the second line
  116.   lcd.print("ZOLTY +"); // Print the text for resultZ
  117.   lcd.print(resultZ);
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement