Advertisement
pleasedontcode

**Relay Control** rev_01

May 15th, 2025
455
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: **Relay Control**
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2025-05-15 05:50:58
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Control a relay module using digital output pin */
  21.     /* D2, allowing for on/off switching based on sensor */
  22.     /* data from the BMX280 environmental sensor. Ensure */
  23.     /* proper initialization and data handling for */
  24.     /* reliable operation. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Relay.h>  //http://github.com/rafaelnsantos/Relay
  31. #include <forcedBMX280.h>   //http://github.com/soylentOrange/Forced-BMX280
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t My32relay_RelayModule_Signal_PIN_D2       = 2;
  39.  
  40. // USER CODE: Define relay pins for 32 relays
  41. const int relayPins[32] = {
  42.   // First relay module (Relays 1–16)
  43.   22, 23, 24, 25, 26, 27, 28, 29,
  44.   30, 31, 32, 33, 34, 35, 36, 37,
  45.   // Second relay module (Relays 17–32)
  46.   38, 39, 40, 41, 42, 43, 44, 45,
  47.   46, 47, 48, 49, 50, 51, 52, 53
  48. };
  49.  
  50. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  51. /***** used to store raw data *****/
  52. bool    My32relay_RelayModule_Signal_PIN_D2_rawData     = 0;
  53.  
  54. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  55. /***** used to store data after characteristic curve transformation *****/
  56. float   My32relay_RelayModule_Signal_PIN_D2_phyData     = 0.0;
  57.  
  58. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  59. // Instantiate the sensor object
  60. ForcedBMX280 sensor; // Default I2C address is 0x76
  61.  
  62. void setup(void)
  63. {
  64.     // put your setup code here, to run once:
  65.     pinMode(My32relay_RelayModule_Signal_PIN_D2, OUTPUT);
  66.  
  67.     // USER CODE: Initialize relay pins
  68.     for (int i = 0; i < 32; i++) {
  69.         pinMode(relayPins[i], OUTPUT);
  70.         digitalWrite(relayPins[i], HIGH); // OFF (active LOW)
  71.     }
  72.  
  73.     // Initialize the sensor
  74.     if (sensor.begin() != ERROR_OK) {
  75.         // Handle sensor initialization error
  76.         while (1); // Stop execution if sensor fails to initialize
  77.     }
  78. }
  79.  
  80. void loop(void)
  81. {
  82.     // put your main code here, to run repeatedly:
  83.     updateOutputs(); // Refresh output data
  84.  
  85.     // Read sensor data
  86.     My32relay_RelayModule_Signal_PIN_D2_phyData = sensor.getTemperatureCelsius(); // Get temperature
  87.  
  88.     // Control relay based on temperature threshold
  89.     if (My32relay_RelayModule_Signal_PIN_D2_phyData > 25.0) { // Example threshold
  90.         My32relay_RelayModule_Signal_PIN_D2_rawData = true; // Turn ON relay
  91.     } else {
  92.         My32relay_RelayModule_Signal_PIN_D2_rawData = false; // Turn OFF relay
  93.     }
  94.  
  95.     // Update relay state
  96.     digitalWrite(My32relay_RelayModule_Signal_PIN_D2, My32relay_RelayModule_Signal_PIN_D2_rawData);
  97.  
  98.     // USER CODE: Control multiple relays
  99.     int group1[] = {5, 13, 2, 10};
  100.     int group2[] = {6, 14, 3, 11};
  101.     int group3[] = {7, 15, 4, 12};
  102.     int group4[] = {8, 16, 1, 9};
  103.  
  104.     // Extend groups by adding the same numbers +16
  105.     int* allGroups[] = {
  106.         group1, group2, group3, group4,
  107.     };
  108.  
  109.     int groupSizes[] = {
  110.         4, 4, 4, 4,
  111.     };
  112.  
  113.     for (int i = 0; i < 4; i++) {
  114.         int size = groupSizes[i];
  115.         int fullGroup[32];
  116.         for (int j = 0; j < size; j++) {
  117.             fullGroup[j] = allGroups[i][j];
  118.             fullGroup[j + size] = allGroups[i][j] + 16;
  119.         }
  120.         setRelays(fullGroup, size * 2, true, 4000);
  121.     }
  122.  
  123.     while (false); // stop repeating
  124. }
  125.  
  126. void updateOutputs()
  127. {
  128.     digitalWrite(My32relay_RelayModule_Signal_PIN_D2, My32relay_RelayModule_Signal_PIN_D2_rawData);
  129. }
  130.  
  131. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement