Advertisement
pleasedontcode

**Relay Control** rev_01

Nov 12th, 2024
91
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 Uno
  14.     - Source Code created on: 2024-11-12 23:08:43
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall utilize the EasyButton library to */
  21.     /* manage button presses, control the Relay module */
  22.     /* for switching, and read temperature data from the */
  23.     /* DS18B20 sensor, ensuring seamless integration with */
  24.     /* the Adafruit Microbit. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  31. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  32. #include <IRremote.h> // Include the IR remote library
  33. #include <OneWire.h>  // Include the OneWire library for DS18B20
  34. #include <DallasTemperature.h> // Include the DallasTemperature library for DS18B20
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t pushbutton_PushButton_PIN_D2 = 2;
  42.  
  43. // Define the pins for the relays
  44. #define RELAY1 3
  45. #define RELAY2 4
  46. #define RELAY3 5
  47. #define RELAY4 6
  48. #define RELAY5 7
  49. #define RELAY6 8
  50. #define RELAY7 9
  51. #define RELAY8 10
  52.  
  53. // Define the pin for the infrared motion sensor
  54. #define MOTION_SENSOR 11 // Changed to a different pin to avoid conflict
  55.  
  56. // Define the pin for the DS18B20 sensor
  57. #define ONE_WIRE_BUS 12 // Pin for the DS18B20
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  60.  
  61. IRrecv irrecv(MOTION_SENSOR); // Initialize the IR remote object
  62. decode_results results; // Initialize the variable to store the received code
  63.  
  64. OneWire oneWire(ONE_WIRE_BUS); // Create OneWire instance for DS18B20
  65. DallasTemperature sensors(&oneWire); // Create DallasTemperature instance
  66.  
  67. void setup(void)
  68. {
  69.     // put your setup code here, to run once:
  70.     pinMode(pushbutton_PushButton_PIN_D2, INPUT_PULLUP);
  71.  
  72.     // Set the relay pins as output
  73.     pinMode(RELAY1, OUTPUT);
  74.     pinMode(RELAY2, OUTPUT);
  75.     pinMode(RELAY3, OUTPUT);
  76.     pinMode(RELAY4, OUTPUT);
  77.     pinMode(RELAY5, OUTPUT);
  78.     pinMode(RELAY6, OUTPUT);
  79.     pinMode(RELAY7, OUTPUT);
  80.     pinMode(RELAY8, OUTPUT);
  81.  
  82.     // Set the motion sensor pin as input
  83.     pinMode(MOTION_SENSOR, INPUT);
  84.  
  85.     // Start the IR remote receiver
  86.     irrecv.enableIRIn();
  87.  
  88.     // Start the DS18B20 sensor
  89.     sensors.begin();
  90. }
  91.  
  92. void loop(void)
  93. {
  94.     // put your main code here, to run repeatedly:
  95.     // Check if the motion sensor is triggered
  96.     if (digitalRead(MOTION_SENSOR) == HIGH) {
  97.         // Activate the relays in sequence with the defined time intervals
  98.         digitalWrite(RELAY1, HIGH);
  99.         delay(INTERVAL1);
  100.         digitalWrite(RELAY2, HIGH);
  101.         delay(INTERVAL2);
  102.         digitalWrite(RELAY3, HIGH);
  103.         delay(INTERVAL3);
  104.         digitalWrite(RELAY4, HIGH);
  105.         delay(INTERVAL4);
  106.         digitalWrite(RELAY5, HIGH);
  107.         delay(INTERVAL5);
  108.         digitalWrite(RELAY6, HIGH);
  109.         delay(INTERVAL6);
  110.         digitalWrite(RELAY7, HIGH);
  111.         delay(INTERVAL7);
  112.         digitalWrite(RELAY8, HIGH);
  113.         delay(INTERVAL8);
  114.  
  115.         // Wait for the IR remote code to be received
  116.         if (irrecv.decode(&results)) {
  117.             // Check if the received code is the ON code
  118.             if (results.value == ON_CODE) {
  119.                 // Deactivate the relays in reverse sequence
  120.                 digitalWrite(RELAY8, LOW);
  121.                 delay(INTERVAL8);
  122.                 digitalWrite(RELAY7, LOW);
  123.                 delay(INTERVAL7);
  124.                 digitalWrite(RELAY6, LOW);
  125.                 delay(INTERVAL6);
  126.                 digitalWrite(RELAY5, LOW);
  127.                 delay(INTERVAL5);
  128.                 digitalWrite(RELAY4, LOW);
  129.                 delay(INTERVAL4);
  130.                 digitalWrite(RELAY3, LOW);
  131.                 delay(INTERVAL3);
  132.                 digitalWrite(RELAY2, LOW);
  133.                 delay(INTERVAL2);
  134.                 digitalWrite(RELAY1, LOW);
  135.                 delay(INTERVAL1);
  136.             }
  137.             // Reset the IR remote receiver
  138.             irrecv.resume();
  139.         }
  140.     }
  141.  
  142.     // Read temperature from DS18B20
  143.     sensors.requestTemperatures(); // Request temperature readings
  144.     float temperature = sensors.getTempCByIndex(0); // Get temperature in Celsius
  145.     // You can add code to use the temperature value as needed
  146. }
  147.  
  148. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement