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: MPR121 Relays
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-09-04 18:20:10
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* clontrol 8 SSD relay with IR remote and MPR121 */
- /* touch pad */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Relay.h> //https://github.com/rafaelnsantos/Relay
- #include <Wire.h>
- #include <Adafruit_MPR121.h>
- #include <IRrecv.h>
- #include <IRutils.h>
- /****** CONSTANTS AND PIN DEFINITIONS *****/
- #define NUM_RELAYS 8
- // Relay pin mapping (ESP32 GPIOs). Ensure these pins are suitable for your board and relay driver wiring.
- const uint8_t RELAY_PINS[NUM_RELAYS] = {
- 2, 4, 5, 12, 13, 14, 15, 16
- };
- // I2C pins for MPR121
- #define MPR_SDA_PIN 21
- #define MPR_SCL_PIN 22
- // IR receiver
- #define IR_PIN 27
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // MPR121 touch support
- Adafruit_MPR121 cap = Adafruit_MPR121();
- // IR receiver object
- IRrecv irrecv(IR_PIN);
- decode_results results;
- // Relay modules (explicit pin mapping for clarity)
- Relay relays[NUM_RELAYS] = {
- Relay(2, true),
- Relay(4, true),
- Relay(5, true),
- Relay(12, true),
- Relay(13, true),
- Relay(14, true),
- Relay(15, true),
- Relay(16, true)
- };
- // Previous touch states for MPR121 electrodes (we use first 8 electrodes)
- bool prevTouch[NUM_RELAYS];
- // Helper to toggle a relay state
- void toggleRelay(uint8_t idx) {
- if (idx >= NUM_RELAYS) return;
- if (relays[idx].getState()) {
- relays[idx].turnOff();
- } else {
- relays[idx].turnOn();
- }
- }
- // Process IR remote codes (example mappings for common NEC-like remotes)
- void processIR(uint32_t code) {
- switch (code) {
- case 0xFF30CF: toggleRelay(0); break; // Key 1
- case 0xFF18E7: toggleRelay(1); break; // Key 2
- case 0xFF7A85: toggleRelay(2); break; // Key 3
- case 0xFF10EF: toggleRelay(3); break; // Key 4
- case 0xFF38C7: toggleRelay(4); break; // Key 5
- case 0xFF5AA5: toggleRelay(5); break; // Key 6
- case 0xFF42BD: toggleRelay(6); break; // Key 7
- case 0xFF4AB5: toggleRelay(7); break; // Key 8
- default: break;
- }
- }
- void setup(void)
- {
- // Initialize serial for debugging
- Serial.begin(115200);
- while (!Serial) { ; }
- // Initialize Relay modules and reset touch states
- for (uint8_t i = 0; i < NUM_RELAYS; i++) {
- relays[i].begin();
- prevTouch[i] = false;
- }
- // Initialize I2C for MPR121 on defined pins
- Wire.begin(MPR_SDA_PIN, MPR_SCL_PIN);
- if (!cap.begin(0x5A)) {
- Serial.println("MPR121 not found");
- while (1);
- }
- // Initialize IR receiver
- irrecv.enableIRIn();
- Serial.println("System ready");
- }
- void loop(void)
- {
- // IR remote handling
- if (irrecv.decode(&results)) {
- processIR(results.value);
- irrecv.resume();
- }
- // MPR121 touch handling (toggle relays on touch edge)
- uint16_t touches = cap.touched();
- for (uint8_t i = 0; i < NUM_RELAYS; i++) {
- bool isTouched = (touches & (1 << i)) != 0;
- if (isTouched && !prevTouch[i]) {
- toggleRelay(i);
- }
- prevTouch[i] = isTouched;
- }
- delay(20);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment