Advertisement
Guest User

bedAndLightControlArduino

a guest
Dec 6th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*input radio codes from 10 key remote remote and actions*/
  2. #define TEN_KEY_REMOTE_LIGHT_ON "13944860"
  3. #define TEN_KEY_REMOTE_LIGHT_OFF "13944852"
  4. #define TEN_KEY_REMOTE_FAN_TOGGLE "13944858"
  5. #define TEN_KEY_REMOTE_FAN_OFF "13944850"
  6. #define TEN_KEY_REMOTE_HEAD_RAISE "13944857"
  7. #define TEN_KEY_REMOTE_LEGS_RAISE "13944849"
  8. #define TEN_KEY_REMOTE_HEAD_LOWER "13944861"
  9. #define TEN_KEY_REMOTE_LEGS_LOWER "13944853"
  10. /*required for radio functionality*/
  11. /*input radio codes from "Star" remote remote and actions*/
  12. #define STAR_REMOTE_LIGHT_ON "16125200"
  13. #define STAR_REMOTE_LIGHT_OFF "16125408"
  14. #define STAR_REMOTE_FAN_1 "16125376"
  15. #define STAR_REMOTE_FAN_2 "16125248"
  16. #define STAR_REMOTE_FAN_3 "16125312"
  17. #define STAR_REMOTE_FAN_OFF "16125328"
  18.  
  19. ///*output deciaml radio codes to fan/light control unit and their actions*/
  20. //#define CONTROL_CODE_LIGHT_ON 16125200
  21. //#define CONTROL_CODE_LIGHT_OFF 16125408
  22. //#define CONTROL_CODE_FAN_1 16125376
  23. //#define CONTROL_CODE_FAN_2 16125248
  24. //#define CONTROL_CODE_FAN_3 16125312
  25. //#define CONTROL_CODE_FAN_OFF 16125328
  26.  
  27. ///*output radio codes to fan/light control unit and their actions*/
  28. #define CONTROL_CODE_LIGHT_ON "111101100000110100010000"
  29. #define CONTROL_CODE_LIGHT_OFF "111101100000110111100000"
  30. #define CONTROL_CODE_FAN_1 "111101100000110111000000"
  31. #define CONTROL_CODE_FAN_2 "111101100000110101000000"
  32. #define CONTROL_CODE_FAN_3 "111101100000110110000000"
  33. #define CONTROL_CODE_FAN_OFF "111101100000110110010000"
  34.  
  35. /*the output pins for the transistor switches and their actions*/
  36. #define OUTPUT_PIN_HEAD_RAISE 9
  37. #define OUTPUT_PIN_HEAD_LOWER 10
  38. #define OUTPUT_PIN_LEGS_RAISE 11
  39. #define OUTPUT_PIN_LEGS_LOWER 12
  40.  
  41. /*the pins for radio transmitter and reciever modules and radio transmission protocol and pulse length*/
  42. #define RECEIVER_PIN 2
  43. #define RECEIVER_INTERRUPT 0 //pin #2 -> interrupt 0; pin #3 -> interrupt 1. use one of these two for receiver.
  44. #define TRANSMITTER_PIN 3
  45. #define PULSE_LENGTH 462
  46. #define PROTOCOL 6
  47.  
  48. #define N_OF_LIGHT_INPUT_CODES 4
  49. #define N_OF_FAN_INPUT_CODES 6
  50. #define N_OF_BED_INPUT_CODES 4
  51. #define N_OF_LIGHT_OUTPUT_CODES 2
  52. #define N_OF_FAN_OUTPUT_CODES 4
  53. #define N_OF_BED_OUTPUT_PINS 4
  54.  
  55. String lightInputCodes[] = {
  56.   TEN_KEY_REMOTE_LIGHT_ON,
  57.   STAR_REMOTE_LIGHT_ON,
  58.   TEN_KEY_REMOTE_LIGHT_OFF,
  59.   STAR_REMOTE_LIGHT_OFF
  60. };
  61.  
  62. String fanInputCodes[] = {
  63.   TEN_KEY_REMOTE_FAN_TOGGLE,
  64.   STAR_REMOTE_FAN_1,
  65.   STAR_REMOTE_FAN_2,
  66.   STAR_REMOTE_FAN_3,
  67.   STAR_REMOTE_FAN_OFF,
  68.   TEN_KEY_REMOTE_FAN_OFF
  69. };
  70.  
  71. String bedInputCodes[] = {
  72.   TEN_KEY_REMOTE_HEAD_RAISE,
  73.   TEN_KEY_REMOTE_HEAD_LOWER,
  74.   TEN_KEY_REMOTE_LEGS_RAISE,
  75.   TEN_KEY_REMOTE_LEGS_LOWER
  76. };
  77.  
  78. const char* lightOutputCodes[] = {
  79.   CONTROL_CODE_LIGHT_ON,
  80.   CONTROL_CODE_LIGHT_OFF,
  81. };
  82.  
  83. const char* fanOutputCodes[] = {
  84.   CONTROL_CODE_FAN_OFF,
  85.   CONTROL_CODE_FAN_1,
  86.   CONTROL_CODE_FAN_2,
  87.   CONTROL_CODE_FAN_3
  88. };
  89.  
  90. uint8_t bedOutputPins[] = {
  91.   OUTPUT_PIN_HEAD_RAISE,
  92.   OUTPUT_PIN_HEAD_LOWER,
  93.   OUTPUT_PIN_LEGS_RAISE,
  94.   OUTPUT_PIN_LEGS_LOWER
  95. };
  96.  
  97. /*required for radio functionality*/
  98. #include <RCSwitch.h>
  99.  
  100. void light( int );
  101. void fan( int );
  102. void bed( int );
  103.  
  104. RCSwitch mySwitch = RCSwitch();
  105.  
  106. bool codeFound = false;
  107. unsigned long receivedValue = 0l;
  108. String receivedValueString = "";
  109. int fanSpeed = 0;
  110.  
  111.  
  112. void setup() {
  113.   Serial.begin(9600);
  114.  
  115.   codeFound = false;
  116.   receivedValue = 0l;
  117.   receivedValueString = "";
  118.   fanSpeed = 0;
  119.  
  120.   // Initialising the pins that control the relay to digitial output
  121.   for ( int i = 0; i < N_OF_BED_OUTPUT_PINS; i++) {
  122.     pinMode( bedOutputPins[ i ], OUTPUT );
  123.     digitalWrite( bedOutputPins[ i ], HIGH );
  124.   }            
  125.  
  126.   // The receiver is connected to pin #2 and so has interrupt 0
  127.   mySwitch.enableReceive( RECEIVER_INTERRUPT );
  128.  
  129.   // The transmitter is connected to TRANSMITTER_PIN; values for pulse and protocol from analysing the ceiling fan's remote control signals
  130.   mySwitch.enableTransmit( TRANSMITTER_PIN );
  131.   mySwitch.setPulseLength( PULSE_LENGTH );
  132.   mySwitch.setProtocol( PROTOCOL );
  133.  
  134. //    if (!driver.init())
  135. //         Serial.println("init failed");
  136. }
  137.  
  138. void loop() {
  139.   receivedValue = 0l;
  140.   receivedValueString = "";
  141.   codeFound = false;
  142.   if ( mySwitch.available() ) {
  143.     Serial.print( " got code \n" );
  144.     output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());
  145.     receivedValue = mySwitch.getReceivedValue();
  146.     receivedValueString = String( receivedValue );
  147.     mySwitch.resetAvailable();
  148.     for ( int i = 0; i < N_OF_LIGHT_INPUT_CODES; i++ ) {
  149.       Serial.print( " check light codes \n" );
  150.       if( receivedValueString.equals( lightInputCodes[ i ] ) ){
  151.         Serial.print( " light code found \n" );
  152.         light( i );
  153.         codeFound = true;
  154.         break;
  155.       }
  156.     }
  157.     if( !codeFound ){
  158.       for ( int i = 0; i < N_OF_FAN_INPUT_CODES; i++ ) {
  159.         Serial.print( " check fan codes \n" );
  160.         if( receivedValueString.equals( fanInputCodes[ i ] ) ){
  161.           Serial.print( " fan code found \n" );
  162.           fan( i );
  163.           codeFound = true;
  164.           break;
  165.         }
  166.       }
  167.     }
  168.     if( !codeFound ){
  169.       Serial.print( " check bed codes \n" );
  170.       for ( int i = 0; i < N_OF_BED_INPUT_CODES; i++ ) {
  171.         if( receivedValueString.equals( bedInputCodes[ i ] ) ){
  172.           Serial.print( " bed code found \n" );
  173.           bed( i );
  174.           codeFound = true;
  175.           break;
  176.         }
  177.       }
  178.     }
  179.   }
  180. }
  181.  
  182. // switches the lights
  183. // int whichCode index for lightOutputCodes[], indicates required action
  184. void light( int whichCode ) {
  185.   if ( whichCode <= 1) {
  186.     Serial.print( "light on \n");
  187. //    driver.send( lightOutputCodes[ 0 ], strlen( lightOutputCodes[ 0 ] ) );
  188. //    driver.waitPacketSent();
  189.     mySwitch.setPulseLength( 458 );
  190.     mySwitch.send( lightOutputCodes[ 0 ] );
  191.   }
  192.   else {
  193.     Serial.print( "light off \n");
  194. //    driver.send( lightOutputCodes[ 1 ], strlen( lightOutputCodes[ 1 ] ) );
  195. //    driver.waitPacketSent();
  196.     mySwitch.setPulseLength( 459 );
  197.     mySwitch.send( lightOutputCodes[ 1 ] );
  198.   }
  199.       return;
  200. }
  201.  
  202. // operates the fan
  203. // int whichCode index for fanOutputCodes[], indicates the required action
  204. void fan( int whichCode ) {
  205.   switch ( whichCode ) {
  206.     case 0: {
  207.       // "toggle fan speed" signal from any non-native remote without "rolling code" functionality
  208.       Serial.print( "fan speed \n");
  209.       toggleFanSpeed();
  210.       break;
  211.     }
  212.     case 1: case 2: case 3: {
  213.       // "toggle fan speed" signal from fan's native remote (rolling code functionality); doesn't require further action
  214.       Serial.print( "fan speed \n");
  215.       fanSpeed =  whichCode;
  216.       break;
  217.     }
  218.     case 4: case 5: {
  219.       // "fan off" signal from any remote
  220.       Serial.print( "fan stop \n");
  221.       mySwitch.setPulseLength( 462 );
  222.       mySwitch.send( fanOutputCodes[ 0 ] );
  223.       fanSpeed = 0;
  224.       break;
  225.     }
  226.   }
  227.   return;
  228. }
  229.  
  230. // operates the bed controls; takes over input signal processing while remote button is pressed
  231. // int whichCode index for bedOutputPins[], indicates the output pin to operate
  232. void bed( int whichPin ) {
  233.   digitalWrite( bedOutputPins[ whichPin ], LOW);
  234.   do{
  235.     // Delay for mechanical bed operation
  236.     delay( 150 );
  237.     receivedValue = mySwitch.getReceivedValue();
  238.     receivedValueString = String( receivedValue );
  239.     mySwitch.resetAvailable();
  240.   } while( bedInputCodes[ whichPin ].equals( receivedValueString ) );
  241.   digitalWrite( bedOutputPins[ whichPin ], HIGH );
  242.   codeFound = true;
  243.   return;
  244. }
  245.  
  246. // provides "rolling code"-like functionality for incapable remotes by tracking fanSpeed
  247. void toggleFanSpeed() {
  248.   switch ( fanSpeed ) {
  249.     case 0: case 3: {
  250.       // fan is off or at 3rd speed
  251.       mySwitch.setPulseLength( 460 );
  252.       mySwitch.send( fanOutputCodes[ 1 ] );
  253.       fanSpeed = 1;
  254.       break;
  255.     }
  256.     case 1: {
  257.       // fan is on 1st speed
  258.       mySwitch.setPulseLength( 459 );
  259.       mySwitch.send( fanOutputCodes[ 2 ] );
  260.       fanSpeed = 2;
  261.       break;
  262.     }
  263.     case 2: {
  264.       // fan on 2nd speed
  265.       mySwitch.setPulseLength( 454 );
  266.       mySwitch.send( fanOutputCodes[ 3 ] );
  267.       fanSpeed = 3;
  268.       break;
  269.     }
  270.   }
  271.   return;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement