Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Relay Control**
- - Source Code NOT compiled for: Arduino Mega
- - Source Code created on: 2025-05-15 05:50:58
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Control a relay module using digital output pin */
- /* D2, allowing for on/off switching based on sensor */
- /* data from the BMX280 environmental sensor. Ensure */
- /* proper initialization and data handling for */
- /* reliable operation. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Relay.h> //http://github.com/rafaelnsantos/Relay
- #include <forcedBMX280.h> //http://github.com/soylentOrange/Forced-BMX280
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t My32relay_RelayModule_Signal_PIN_D2 = 2;
- // USER CODE: Define relay pins for 32 relays
- const int relayPins[32] = {
- // First relay module (Relays 1–16)
- 22, 23, 24, 25, 26, 27, 28, 29,
- 30, 31, 32, 33, 34, 35, 36, 37,
- // Second relay module (Relays 17–32)
- 38, 39, 40, 41, 42, 43, 44, 45,
- 46, 47, 48, 49, 50, 51, 52, 53
- };
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- bool My32relay_RelayModule_Signal_PIN_D2_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float My32relay_RelayModule_Signal_PIN_D2_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Instantiate the sensor object
- ForcedBMX280 sensor; // Default I2C address is 0x76
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(My32relay_RelayModule_Signal_PIN_D2, OUTPUT);
- // USER CODE: Initialize relay pins
- for (int i = 0; i < 32; i++) {
- pinMode(relayPins[i], OUTPUT);
- digitalWrite(relayPins[i], HIGH); // OFF (active LOW)
- }
- // Initialize the sensor
- if (sensor.begin() != ERROR_OK) {
- // Handle sensor initialization error
- while (1); // Stop execution if sensor fails to initialize
- }
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- updateOutputs(); // Refresh output data
- // Read sensor data
- My32relay_RelayModule_Signal_PIN_D2_phyData = sensor.getTemperatureCelsius(); // Get temperature
- // Control relay based on temperature threshold
- if (My32relay_RelayModule_Signal_PIN_D2_phyData > 25.0) { // Example threshold
- My32relay_RelayModule_Signal_PIN_D2_rawData = true; // Turn ON relay
- } else {
- My32relay_RelayModule_Signal_PIN_D2_rawData = false; // Turn OFF relay
- }
- // Update relay state
- digitalWrite(My32relay_RelayModule_Signal_PIN_D2, My32relay_RelayModule_Signal_PIN_D2_rawData);
- // USER CODE: Control multiple relays
- int group1[] = {5, 13, 2, 10};
- int group2[] = {6, 14, 3, 11};
- int group3[] = {7, 15, 4, 12};
- int group4[] = {8, 16, 1, 9};
- // Extend groups by adding the same numbers +16
- int* allGroups[] = {
- group1, group2, group3, group4,
- };
- int groupSizes[] = {
- 4, 4, 4, 4,
- };
- for (int i = 0; i < 4; i++) {
- int size = groupSizes[i];
- int fullGroup[32];
- for (int j = 0; j < size; j++) {
- fullGroup[j] = allGroups[i][j];
- fullGroup[j + size] = allGroups[i][j] + 16;
- }
- setRelays(fullGroup, size * 2, true, 4000);
- }
- while (false); // stop repeating
- }
- void updateOutputs()
- {
- digitalWrite(My32relay_RelayModule_Signal_PIN_D2, My32relay_RelayModule_Signal_PIN_D2_rawData);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement