Advertisement
pleasedontcode

Button Control rev_05

Jan 28th, 2024
76
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 17:23:15
  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.     /* Holding time subtracts value:  1 second subtracts */
  29.     /* randomly 10 to 40, until the button is released. */
  30. /****** END SYSTEM REQUIREMENTS *****/
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <Arduino.h>
  34. #include <Wire.h>
  35. #include <EasyButton.h>
  36. #include <LiquidCrystal_I2C.h>
  37.  
  38. /****** FUNCTION PROTOTYPES *****/
  39. void setup(void);
  40. void loop(void);
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t zielonyP_PushButton_PIN_D2 = 2;
  44. const uint8_t czerwonyP_PushButton_PIN_D3 = 3;
  45.  
  46. /***** DEFINITION OF I2C PINS *****/
  47. const uint8_t LCD1602_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  48. const uint8_t LCD1602_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  49. const uint8_t LCD1602_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
  50.  
  51. /****** DEFINITION OF LIBRARY CLASS INSTANCES *****/
  52. EasyButton zielonyP_PushButton(zielonyP_PushButton_PIN_D2);
  53. EasyButton czerwonyP_PushButton(czerwonyP_PushButton_PIN_D3);
  54. LiquidCrystal_I2C lcd(LCD1602_LCD1602I2C_I2C_SLAVE_ADDRESS, LCD1602_LCD1602I2C_I2C_PIN_SDA_A4, LCD1602_LCD1602I2C_I2C_PIN_SCL_A5);
  55.  
  56. // Variables to hold the result values
  57. int resultC = 10000;
  58. int resultZ = 10000;
  59.  
  60. void setup(void)
  61. {
  62.     // put your setup code here, to run once:
  63.     pinMode(zielonyP_PushButton_PIN_D2, INPUT_PULLUP);
  64.     pinMode(czerwonyP_PushButton_PIN_D3, INPUT_PULLUP);
  65.  
  66.     // Initialize the buttons
  67.     zielonyP_PushButton.begin();
  68.     czerwonyP_PushButton.begin();
  69.  
  70.     // Initialize the LCD
  71.     lcd.begin(16, 2);
  72.     lcd.setBacklight(HIGH);
  73.     lcd.setCursor(0, 0);
  74.     lcd.print("CZERWONY +");
  75.     lcd.setCursor(0, 1);
  76.     lcd.print("ZOLTY +");
  77.  
  78.     // Display the initial values
  79.     lcd.setCursor(10, 0);
  80.     lcd.print(resultC);
  81.     lcd.setCursor(8, 1);
  82.     lcd.print(resultZ);
  83. }
  84.  
  85. void loop(void)
  86. {
  87.     // put your main code here, to run repeatedly:
  88.  
  89.     // Read the status of the buttons
  90.     zielonyP_PushButton.read();
  91.     czerwonyP_PushButton.read();
  92.  
  93.     // Check if the green button is pressed
  94.     if (zielonyP_PushButton.isPressed())
  95.     {
  96.         // Subtract a random value from resultZ
  97.         int randomValue = random(10, 41);
  98.         resultZ -= randomValue;
  99.  
  100.         // Update the LCD display for resultZ
  101.         lcd.setCursor(8, 1);
  102.         lcd.print("     ");
  103.         lcd.setCursor(8, 1);
  104.         lcd.print(resultZ);
  105.     }
  106.  
  107.     // Check if the red button is pressed
  108.     if (czerwonyP_PushButton.isPressed())
  109.     {
  110.         // Subtract a random value from resultC
  111.         int randomValue = random(10, 41);
  112.         resultC -= randomValue;
  113.  
  114.         // Update the LCD display for resultC
  115.         lcd.setCursor(10, 0);
  116.         lcd.print("     ");
  117.         lcd.setCursor(10, 0);
  118.         lcd.print(resultC);
  119.     }
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement