pleasedontcode

Relay Controller rev_01

Sep 4th, 2025
285
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 Controller
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-09-04 18:06:33
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* clontrol 8 SSD relay with IR remote and MPR121 */
  21.     /* touch pad */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  29. #include <Wire.h>
  30. #include <Adafruit_MPR121.h>
  31. #include <IRrecv.h>
  32. #include <IRutils.h>
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. // MPR121 touch support
  39. #define NUM_RELAYS 8
  40.  
  41. Adafruit_MPR121 cap = Adafruit_MPR121();
  42.  
  43. // IR receiver
  44. #define IR_PIN 27
  45. IRrecv irrecv(IR_PIN);
  46. decode_results results;
  47.  
  48. // Relay modules
  49. Relay relays[NUM_RELAYS] = {
  50.   Relay(2, true),
  51.   Relay(4, true),
  52.   Relay(5, true),
  53.   Relay(12, true),
  54.   Relay(13, true),
  55.   Relay(14, true),
  56.   Relay(15, true),
  57.   Relay(16, true)
  58. };
  59.  
  60. // Previous touch states for MPR121 electrodes (we use first 8 electrodes)
  61. bool prevTouch[NUM_RELAYS];
  62.  
  63. // Helper to toggle a relay state
  64. void toggleRelay(uint8_t idx) {
  65.   if (idx >= NUM_RELAYS) return;
  66.   if (relays[idx].getState()) {
  67.     relays[idx].turnOff();
  68.   } else {
  69.     relays[idx].turnOn();
  70.   }
  71. }
  72.  
  73. // Process IR remote codes (example mappings for common NEC-like remotes)
  74. void processIR(uint32_t code) {
  75.   switch (code) {
  76.     case 0xFF30CF: toggleRelay(0); break; // Key 1
  77.     case 0xFF18E7: toggleRelay(1); break; // Key 2
  78.     case 0xFF7A85: toggleRelay(2); break; // Key 3
  79.     case 0xFF10EF: toggleRelay(3); break; // Key 4
  80.     case 0xFF38C7: toggleRelay(4); break; // Key 5
  81.     case 0xFF5AA5: toggleRelay(5); break; // Key 6
  82.     case 0xFF42BD: toggleRelay(6); break; // Key 7
  83.     case 0xFF4AB5: toggleRelay(7); break; // Key 8
  84.     default: break;
  85.   }
  86. }
  87.  
  88. void setup(void)
  89. {
  90.   // Initialize serial for debugging
  91.   Serial.begin(115200);
  92.   while (!Serial) { ; }
  93.  
  94.   // Initialize Relay modules
  95.   for (uint8_t i = 0; i < NUM_RELAYS; i++) {
  96.     relays[i].begin();
  97.     prevTouch[i] = false;
  98.   }
  99.  
  100.   // Initialize I2C for MPR121
  101.   Wire.begin();
  102.   if (!cap.begin(0x5A)) {
  103.     Serial.println("MPR121 not found");
  104.     while (1);
  105.   }
  106.  
  107.   // Initialize IR receiver
  108.   irrecv.enableIRIn();
  109.  
  110.   Serial.println("System ready");
  111. }
  112.  
  113. void loop(void)
  114. {
  115.   // IR remote handling
  116.   if (irrecv.decode(&results)) {
  117.     processIR(results.value);
  118.     irrecv.resume();
  119.   }
  120.  
  121.   // MPR121 touch handling (toggle relays on touch edge)
  122.   uint16_t touches = cap.touched();
  123.   for (uint8_t i = 0; i < NUM_RELAYS; i++) {
  124.     bool isTouched = (touches & (1 << i)) != 0;
  125.     if (isTouched && !prevTouch[i]) {
  126.       toggleRelay(i);
  127.     }
  128.     prevTouch[i] = isTouched;
  129.   }
  130.  
  131.   delay(20);
  132. }
  133.  
  134. /* END CODE */
  135.  
Advertisement
Add Comment
Please, Sign In to add comment