CCCPaste

**Relay Control** rev_01

/********* 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 Uno
	- Source Code created on: 2024-11-12 23:08:43

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* The system shall utilize the EasyButton library to */
	/* manage button presses, control the Relay module */
	/* for switching, and read temperature data from the */
	/* DS18B20 sensor, ensuring seamless integration with */
	/* the Adafruit Microbit. */
/****** END SYSTEM REQUIREMENTS *****/

/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include 	//https://github.com/evert-arias/EasyButton
#include 	//https://github.com/rafaelnsantos/Relay
#include  // Include the IR remote library
#include   // Include the OneWire library for DS18B20
#include  // Include the DallasTemperature library for DS18B20

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t pushbutton_PushButton_PIN_D2 = 2;

// Define the pins for the relays
#define RELAY1 3
#define RELAY2 4
#define RELAY3 5
#define RELAY4 6
#define RELAY5 7
#define RELAY6 8
#define RELAY7 9
#define RELAY8 10

// Define the pin for the infrared motion sensor
#define MOTION_SENSOR 11 // Changed to a different pin to avoid conflict

// Define the pin for the DS18B20 sensor
#define ONE_WIRE_BUS 12 // Pin for the DS18B20

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/

IRrecv irrecv(MOTION_SENSOR); // Initialize the IR remote object
decode_results results; // Initialize the variable to store the received code

OneWire oneWire(ONE_WIRE_BUS); // Create OneWire instance for DS18B20
DallasTemperature sensors(&oneWire); // Create DallasTemperature instance

void setup(void)
{
    // put your setup code here, to run once:
    pinMode(pushbutton_PushButton_PIN_D2, INPUT_PULLUP);

    // Set the relay pins as output
    pinMode(RELAY1, OUTPUT);
    pinMode(RELAY2, OUTPUT);
    pinMode(RELAY3, OUTPUT);
    pinMode(RELAY4, OUTPUT);
    pinMode(RELAY5, OUTPUT);
    pinMode(RELAY6, OUTPUT);
    pinMode(RELAY7, OUTPUT);
    pinMode(RELAY8, OUTPUT);

    // Set the motion sensor pin as input
    pinMode(MOTION_SENSOR, INPUT);

    // Start the IR remote receiver
    irrecv.enableIRIn();

    // Start the DS18B20 sensor
    sensors.begin();
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    // Check if the motion sensor is triggered
    if (digitalRead(MOTION_SENSOR) == HIGH) {
        // Activate the relays in sequence with the defined time intervals
        digitalWrite(RELAY1, HIGH);
        delay(INTERVAL1);
        digitalWrite(RELAY2, HIGH);
        delay(INTERVAL2);
        digitalWrite(RELAY3, HIGH);
        delay(INTERVAL3);
        digitalWrite(RELAY4, HIGH);
        delay(INTERVAL4);
        digitalWrite(RELAY5, HIGH);
        delay(INTERVAL5);
        digitalWrite(RELAY6, HIGH);
        delay(INTERVAL6);
        digitalWrite(RELAY7, HIGH);
        delay(INTERVAL7);
        digitalWrite(RELAY8, HIGH);
        delay(INTERVAL8);

        // Wait for the IR remote code to be received
        if (irrecv.decode(&results)) {
            // Check if the received code is the ON code
            if (results.value == ON_CODE) {
                // Deactivate the relays in reverse sequence
                digitalWrite(RELAY8, LOW);
                delay(INTERVAL8);
                digitalWrite(RELAY7, LOW);
                delay(INTERVAL7);
                digitalWrite(RELAY6, LOW);
                delay(INTERVAL6);
                digitalWrite(RELAY5, LOW);
                delay(INTERVAL5);
                digitalWrite(RELAY4, LOW);
                delay(INTERVAL4);
                digitalWrite(RELAY3, LOW);
                delay(INTERVAL3);
                digitalWrite(RELAY2, LOW);
                delay(INTERVAL2);
                digitalWrite(RELAY1, LOW);
                delay(INTERVAL1);
            }
            // Reset the IR remote receiver
            irrecv.resume();
        }
    }

    // Read temperature from DS18B20
    sensors.requestTemperatures(); // Request temperature readings
    float temperature = sensors.getTempCByIndex(0); // Get temperature in Celsius
    // You can add code to use the temperature value as needed
}

/* END CODE */