Advertisement
pleasedontcode

"LED Toggle" rev_01

Jun 13th, 2024
347
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: "LED Toggle"
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-06-13 13:10:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* when b1_pushbutton is pressed log to internal */
  21.     /* memory and write how many times and the state to */
  22.     /* mysql using php */
  23. /****** SYSTEM REQUIREMENT 2 *****/
  24.     /* when b1_pushbutton or b2_Pushbutton has updated */
  25.     /* mysql database turn on led5_LED for 2 seconds */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h>  // https://github.com/evert-arias/EasyButton
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35. void onButton1Pressed(void);
  36. void onButton2Pressed(void);
  37. void logButtonPressToMemoryAndDatabase(int buttonID);
  38. void turnOnLed5For2Seconds(void);
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t b1_PushButton_PIN_D2 = 2;
  42. const uint8_t b2_PushButton_PIN_D3 = 3;
  43.  
  44. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  45. const uint8_t led1b1_LED_PIN_D4 = 4;
  46. const uint8_t led2b2_LED_PIN_D5 = 5;
  47. const uint8_t led5_LED_PIN_D6 = 6;
  48.  
  49. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  50. /***** used to store raw data *****/
  51. bool led1b1_LED_PIN_D4_rawData = 0;
  52. bool led2b2_LED_PIN_D5_rawData = 0;
  53. bool led5_LED_PIN_D6_rawData = 0;
  54.  
  55. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  56. /***** used to store data after characteristic curve transformation *****/
  57. float led1b1_LED_PIN_D4_phyData = 0.0;
  58. float led2b2_LED_PIN_D5_phyData = 0.0;
  59. float led5_LED_PIN_D6_phyData = 0.0;
  60.  
  61. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  62. EasyButton button1(b1_PushButton_PIN_D2);  // Initialize EasyButton for button 1
  63. EasyButton button2(b2_PushButton_PIN_D3);  // Initialize EasyButton for button 2
  64.  
  65. void setup(void)
  66. {
  67.   // put your setup code here, to run once:
  68.  
  69.   Serial.begin(115200);  // Initialize serial communication
  70.   Serial.println();
  71.   Serial.println(">>> EasyButton multiple buttons example <<<");
  72.  
  73.   pinMode(b1_PushButton_PIN_D2, INPUT_PULLUP);
  74.   pinMode(b2_PushButton_PIN_D3, INPUT_PULLUP);
  75.  
  76.   pinMode(led1b1_LED_PIN_D4, OUTPUT);
  77.   pinMode(led2b2_LED_PIN_D5, OUTPUT);
  78.   pinMode(led5_LED_PIN_D6, OUTPUT);
  79.  
  80.   button1.begin();  // Initialize button 1
  81.   button2.begin();  // Initialize button 2
  82.  
  83.   button1.onPressed(onButton1Pressed);  // Attach callback for button 1
  84.   button2.onPressed(onButton2Pressed);  // Attach callback for button 2
  85. }
  86.  
  87. void loop(void)
  88. {
  89.   // put your main code here, to run repeatedly:
  90.  
  91.   button1.read();  // Read the state of button 1
  92.   button2.read();  // Read the state of button 2
  93.  
  94.   updateOutputs(); // Refresh output data
  95. }
  96.  
  97. void updateOutputs()
  98. {
  99.   digitalWrite(led1b1_LED_PIN_D4, led1b1_LED_PIN_D4_rawData);
  100.   digitalWrite(led2b2_LED_PIN_D5, led2b2_LED_PIN_D5_rawData);
  101.   digitalWrite(led5_LED_PIN_D6, led5_LED_PIN_D6_rawData);
  102. }
  103.  
  104. void onButton1Pressed() {
  105.   Serial.println("Button1 pressed");
  106.   led1b1_LED_PIN_D4_rawData = !led1b1_LED_PIN_D4_rawData;  // Toggle LED state
  107.   logButtonPressToMemoryAndDatabase(1);  // Log button press to memory and database
  108.   turnOnLed5For2Seconds();  // Turn on LED5 for 2 seconds
  109. }
  110.  
  111. void onButton2Pressed() {
  112.   Serial.println("Button2 pressed");
  113.   led2b2_LED_PIN_D5_rawData = !led2b2_LED_PIN_D5_rawData;  // Toggle LED state
  114.   logButtonPressToMemoryAndDatabase(2);  // Log button press to memory and database
  115.   turnOnLed5For2Seconds();  // Turn on LED5 for 2 seconds
  116. }
  117.  
  118. void logButtonPressToMemoryAndDatabase(int buttonID) {
  119.   // Log to internal memory (this is a placeholder, replace with actual logging code)
  120.   static int button1PressCount = 0;
  121.   static int button2PressCount = 0;
  122.  
  123.   if (buttonID == 1) {
  124.     button1PressCount++;
  125.     Serial.print("Button 1 Press Count: ");
  126.     Serial.println(button1PressCount);
  127.   } else if (buttonID == 2) {
  128.     button2PressCount++;
  129.     Serial.print("Button 2 Press Count: ");
  130.     Serial.println(button2PressCount);
  131.   }
  132.  
  133.   // Write to MySQL database using PHP (this is a placeholder, replace with actual database code)
  134.   // Example: http://yourserver.com/log_button_press.php?button_id=1&count=button1PressCount
  135. }
  136.  
  137. void turnOnLed5For2Seconds() {
  138.   led5_LED_PIN_D6_rawData = 1;  // Turn on LED5
  139.   updateOutputs();  // Update outputs to reflect the change
  140.   delay(2000);  // Wait for 2 seconds
  141.   led5_LED_PIN_D6_rawData = 0;  // Turn off LED5
  142.   updateOutputs();  // Update outputs to reflect the change
  143. }
  144.  
  145. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement