Advertisement
pleasedontcode

Button Timer rev_09

Feb 11th, 2024
39
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 Timer
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2024-02-11 13:10:28
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* lcd_LCD1602I2C_I2C line one: 1CZAS,   second line: */
  21.     /* 2CZAS  every 1 second subtract the value 0:01 from */
  22.     /* 1CZAS  2_PushButton held down starts subtracting */
  23.     /* the value 0:01 from 2CZAS every 1 second.  The */
  24.     /* minimum value of 1CZAS and 2CZAS is 0:00. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <EasyButton.h>    // https://github.com/evert-arias/EasyButton
  30. #include <LiquidCrystal_I2C.h>    // https://github.com/marcoschwartz/LiquidCrystal_I2C
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void update1Czas(void);
  36. void update2Czas(void);
  37. void onPressedForDuration1(void);
  38. void onPressedForDuration2(void);
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t BUTTON_PIN_D2 = 2;
  42. const uint8_t BUTTON_PIN_D5 = 5;
  43.  
  44. /***** DEFINITION OF I2C PINS *****/
  45. const uint8_t I2C_PIN_SDA_A4 = A4;
  46. const uint8_t I2C_PIN_SCL_A5 = A5;
  47. const uint8_t I2C_SLAVE_ADDRESS = 39;
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. const uint8_t LED_PIN_D3 = 3;
  51. const uint8_t BUZZER_PIN_D4 = 4;
  52. float LED_PIN_D3_phyData = 0.0;
  53. float BUZZER_PIN_D4_phyData = 0.0;
  54.  
  55. /***** DEFINITION OF TIME VARIABLES *****/
  56. unsigned long previousMillis1Czas = 0;
  57. unsigned long previousMillis2Czas = 0;
  58. const unsigned long interval1Czas = 1000;
  59. const unsigned long interval2Czas = 1000;
  60. int minutes1Czas = 0;
  61. int seconds1Czas = 0;
  62. int minutes2Czas = 0;
  63. int seconds2Czas = 0;
  64.  
  65. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  66. EasyButton button1(BUTTON_PIN_D2);
  67. EasyButton button2(BUTTON_PIN_D5);
  68. LiquidCrystal_I2C lcd(I2C_SLAVE_ADDRESS, 16, 2); // Changed the parameters for lcd.begin()
  69.  
  70. void setup(void)
  71. {
  72.   // put your setup code here, to run once:
  73.  
  74.   // Initialize the buttons
  75.   button1.begin();
  76.   button2.begin();
  77.  
  78.   // Set button callbacks
  79.   button1.onPressedFor(2000, onPressedForDuration1);
  80.   button2.onPressedFor(2000, onPressedForDuration2);
  81.  
  82.   // Initialize the LCD
  83.   lcd.begin(16, 2); // Changed the parameters for lcd.begin()
  84.   lcd.setBacklight(LOW);
  85.  
  86.   // Set up the pin modes
  87.   pinMode(LED_PIN_D3, OUTPUT);
  88.   pinMode(BUZZER_PIN_D4, OUTPUT);
  89. }
  90.  
  91. void loop(void)
  92. {
  93.   // put your main code here, to run repeatedly:
  94.  
  95.   // Update button states
  96.   button1.read();
  97.   button2.read();
  98.  
  99.   // Update 1CZAS
  100.   update1Czas();
  101.  
  102.   // Update 2CZAS
  103.   update2Czas();
  104. }
  105.  
  106. void update1Czas()
  107. {
  108.   unsigned long currentMillis = millis();
  109.  
  110.   if (currentMillis - previousMillis1Czas >= interval1Czas)
  111.   {
  112.     previousMillis1Czas = currentMillis;
  113.  
  114.     if (button1.isPressed())
  115.     {
  116.       if (minutes1Czas > 0 || seconds1Czas > 0)
  117.       {
  118.         seconds1Czas--;
  119.         if (seconds1Czas < 0)
  120.         {
  121.           seconds1Czas = 59;
  122.           minutes1Czas--;
  123.           if (minutes1Czas < 0)
  124.           {
  125.             minutes1Czas = 0;
  126.             seconds1Czas = 0;
  127.           }
  128.         }
  129.       }
  130.     }
  131.     else
  132.     {
  133.       seconds1Czas++;
  134.       if (seconds1Czas > 59)
  135.       {
  136.         seconds1Czas = 0;
  137.         minutes1Czas++;
  138.       }
  139.     }
  140.  
  141.     lcd.setCursor(0, 0);
  142.     lcd.print("1CZAS: ");
  143.     if (minutes1Czas < 10)
  144.     {
  145.       lcd.print("0");
  146.     }
  147.     lcd.print(minutes1Czas);
  148.     lcd.print(":");
  149.     if (seconds1Czas < 10)
  150.     {
  151.       lcd.print("0");
  152.     }
  153.     lcd.print(seconds1Czas);
  154.   }
  155. }
  156.  
  157. void update2Czas()
  158. {
  159.   unsigned long currentMillis = millis();
  160.  
  161.   if (currentMillis - previousMillis2Czas >= interval2Czas)
  162.   {
  163.     previousMillis2Czas = currentMillis;
  164.  
  165.     if (button2.isPressed())
  166.     {
  167.       if (minutes2Czas > 0 || seconds2Czas > 0)
  168.       {
  169.         seconds2Czas--;
  170.         if (seconds2Czas < 0)
  171.         {
  172.           seconds2Czas = 59;
  173.           minutes2Czas--;
  174.           if (minutes2Czas < 0)
  175.           {
  176.             minutes2Czas = 0;
  177.             seconds2Czas = 0;
  178.           }
  179.         }
  180.       }
  181.     }
  182.     else
  183.     {
  184.       seconds2Czas++;
  185.       if (seconds2Czas > 59)
  186.       {
  187.         seconds2Czas = 0;
  188.         minutes2Czas++;
  189.       }
  190.     }
  191.  
  192.     lcd.setCursor(0, 1);
  193.     lcd.print("2CZAS: ");
  194.     if (minutes2Czas < 10)
  195.     {
  196.       lcd.print("0");
  197.     }
  198.     lcd.print(minutes2Czas);
  199.     lcd.print(":");
  200.     if (seconds2Czas < 10)
  201.     {
  202.       lcd.print("0");
  203.     }
  204.     lcd.print(seconds2Czas);
  205.   }
  206. }
  207.  
  208. void onPressedForDuration1()
  209. {
  210.   // Code to be executed when button1 is pressed for the given duration.
  211.   // Add your code here
  212. }
  213.  
  214. void onPressedForDuration2()
  215. {
  216.   // Code to be executed when button2 is pressed for the given duration.
  217.   // Add your code here
  218. }
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement