Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*input radio codes from 10 key remote remote and actions*/
- #define TEN_KEY_REMOTE_LIGHT_ON "13944860"
- #define TEN_KEY_REMOTE_LIGHT_OFF "13944852"
- #define TEN_KEY_REMOTE_FAN_TOGGLE "13944858"
- #define TEN_KEY_REMOTE_FAN_OFF "13944850"
- #define TEN_KEY_REMOTE_HEAD_RAISE "13944857"
- #define TEN_KEY_REMOTE_LEGS_RAISE "13944849"
- #define TEN_KEY_REMOTE_HEAD_LOWER "13944861"
- #define TEN_KEY_REMOTE_LEGS_LOWER "13944853"
- /*input radio codes from "Star" remote remote and actions*/
- #define STAR_REMOTE_LIGHT_ON "16125200"
- #define STAR_REMOTE_LIGHT_OFF "16125408"
- #define STAR_REMOTE_FAN_1 "16125376"
- #define STAR_REMOTE_FAN_2 "16125248"
- #define STAR_REMOTE_FAN_3 "16125312"
- #define STAR_REMOTE_FAN_OFF "16125328"
- ///*output deciaml radio codes to fan/light control unit and their actions*/
- //#define CONTROL_CODE_LIGHT_ON 16125200
- //#define CONTROL_CODE_LIGHT_OFF 16125408
- //#define CONTROL_CODE_FAN_1 16125376
- //#define CONTROL_CODE_FAN_2 16125248
- //#define CONTROL_CODE_FAN_3 16125312
- //#define CONTROL_CODE_FAN_OFF 16125328
- ///*output radio codes to fan/light control unit and their actions*/
- #define CONTROL_CODE_LIGHT_ON "111101100000110100010000"
- #define CONTROL_CODE_LIGHT_OFF "111101100000110111100000"
- #define CONTROL_CODE_FAN_1 "111101100000110111000000"
- #define CONTROL_CODE_FAN_2 "111101100000110101000000"
- #define CONTROL_CODE_FAN_3 "111101100000110110000000"
- #define CONTROL_CODE_FAN_OFF "111101100000110110010000"
- /*the output pins for the transistor switches and their actions*/
- #define OUTPUT_PIN_HEAD_RAISE 9
- #define OUTPUT_PIN_HEAD_LOWER 10
- #define OUTPUT_PIN_LEGS_RAISE 11
- #define OUTPUT_PIN_LEGS_LOWER 12
- /*the pins for radio transmitter and reciever modules and radio transmission protocol and pulse length*/
- #define RECEIVER_PIN 2
- #define RECEIVER_INTERRUPT 0 //pin #2 -> interrupt 0; pin #3 -> interrupt 1. use one of these two for receiver.
- #define TRANSMITTER_PIN 3
- #define PULSE_LENGTH 462
- #define PROTOCOL 6
- #define N_OF_LIGHT_INPUT_CODES 4
- #define N_OF_FAN_INPUT_CODES 6
- #define N_OF_BED_INPUT_CODES 4
- #define N_OF_LIGHT_OUTPUT_CODES 2
- #define N_OF_FAN_OUTPUT_CODES 4
- #define N_OF_BED_OUTPUT_PINS 4
- String lightInputCodes[] = {
- TEN_KEY_REMOTE_LIGHT_ON,
- STAR_REMOTE_LIGHT_ON,
- TEN_KEY_REMOTE_LIGHT_OFF,
- STAR_REMOTE_LIGHT_OFF
- };
- String fanInputCodes[] = {
- TEN_KEY_REMOTE_FAN_TOGGLE,
- STAR_REMOTE_FAN_1,
- STAR_REMOTE_FAN_2,
- STAR_REMOTE_FAN_3,
- STAR_REMOTE_FAN_OFF,
- TEN_KEY_REMOTE_FAN_OFF
- };
- String bedInputCodes[] = {
- TEN_KEY_REMOTE_HEAD_RAISE,
- TEN_KEY_REMOTE_HEAD_LOWER,
- TEN_KEY_REMOTE_LEGS_RAISE,
- TEN_KEY_REMOTE_LEGS_LOWER
- };
- const char* lightOutputCodes[] = {
- CONTROL_CODE_LIGHT_ON,
- CONTROL_CODE_LIGHT_OFF,
- };
- const char* fanOutputCodes[] = {
- CONTROL_CODE_FAN_OFF,
- CONTROL_CODE_FAN_1,
- CONTROL_CODE_FAN_2,
- CONTROL_CODE_FAN_3
- };
- uint8_t bedOutputPins[] = {
- OUTPUT_PIN_HEAD_RAISE,
- OUTPUT_PIN_HEAD_LOWER,
- OUTPUT_PIN_LEGS_RAISE,
- OUTPUT_PIN_LEGS_LOWER
- };
- /*required for radio functionality*/
- #include <RCSwitch.h>
- void light( int );
- void fan( int );
- void bed( int );
- RCSwitch mySwitch = RCSwitch();
- bool codeFound = false;
- unsigned long receivedValue = 0l;
- String receivedValueString = "";
- int fanSpeed = 0;
- void setup() {
- Serial.begin(9600);
- codeFound = false;
- receivedValue = 0l;
- receivedValueString = "";
- fanSpeed = 0;
- // Initialising the pins that control the relay to digitial output
- for ( int i = 0; i < N_OF_BED_OUTPUT_PINS; i++) {
- pinMode( bedOutputPins[ i ], OUTPUT );
- digitalWrite( bedOutputPins[ i ], HIGH );
- }
- // The receiver is connected to pin #2 and so has interrupt 0
- mySwitch.enableReceive( RECEIVER_INTERRUPT );
- // The transmitter is connected to TRANSMITTER_PIN; values for pulse and protocol from analysing the ceiling fan's remote control signals
- mySwitch.enableTransmit( TRANSMITTER_PIN );
- mySwitch.setPulseLength( PULSE_LENGTH );
- mySwitch.setProtocol( PROTOCOL );
- // if (!driver.init())
- // Serial.println("init failed");
- }
- void loop() {
- receivedValue = 0l;
- receivedValueString = "";
- codeFound = false;
- if ( mySwitch.available() ) {
- Serial.print( " got code \n" );
- output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());
- receivedValue = mySwitch.getReceivedValue();
- receivedValueString = String( receivedValue );
- mySwitch.resetAvailable();
- for ( int i = 0; i < N_OF_LIGHT_INPUT_CODES; i++ ) {
- Serial.print( " check light codes \n" );
- if( receivedValueString.equals( lightInputCodes[ i ] ) ){
- Serial.print( " light code found \n" );
- light( i );
- codeFound = true;
- break;
- }
- }
- if( !codeFound ){
- for ( int i = 0; i < N_OF_FAN_INPUT_CODES; i++ ) {
- Serial.print( " check fan codes \n" );
- if( receivedValueString.equals( fanInputCodes[ i ] ) ){
- Serial.print( " fan code found \n" );
- fan( i );
- codeFound = true;
- break;
- }
- }
- }
- if( !codeFound ){
- Serial.print( " check bed codes \n" );
- for ( int i = 0; i < N_OF_BED_INPUT_CODES; i++ ) {
- if( receivedValueString.equals( bedInputCodes[ i ] ) ){
- Serial.print( " bed code found \n" );
- bed( i );
- codeFound = true;
- break;
- }
- }
- }
- }
- }
- // switches the lights
- // int whichCode index for lightOutputCodes[], indicates required action
- void light( int whichCode ) {
- if ( whichCode <= 1) {
- Serial.print( "light on \n");
- // driver.send( lightOutputCodes[ 0 ], strlen( lightOutputCodes[ 0 ] ) );
- // driver.waitPacketSent();
- mySwitch.setPulseLength( 458 );
- mySwitch.send( lightOutputCodes[ 0 ] );
- }
- else {
- Serial.print( "light off \n");
- // driver.send( lightOutputCodes[ 1 ], strlen( lightOutputCodes[ 1 ] ) );
- // driver.waitPacketSent();
- mySwitch.setPulseLength( 459 );
- mySwitch.send( lightOutputCodes[ 1 ] );
- }
- return;
- }
- // operates the fan
- // int whichCode index for fanOutputCodes[], indicates the required action
- void fan( int whichCode ) {
- switch ( whichCode ) {
- case 0: {
- // "toggle fan speed" signal from any non-native remote without "rolling code" functionality
- Serial.print( "fan speed \n");
- toggleFanSpeed();
- break;
- }
- case 1: case 2: case 3: {
- // "toggle fan speed" signal from fan's native remote (rolling code functionality); doesn't require further action
- Serial.print( "fan speed \n");
- fanSpeed = whichCode;
- break;
- }
- case 4: case 5: {
- // "fan off" signal from any remote
- Serial.print( "fan stop \n");
- mySwitch.setPulseLength( 462 );
- mySwitch.send( fanOutputCodes[ 0 ] );
- fanSpeed = 0;
- break;
- }
- }
- return;
- }
- // operates the bed controls; takes over input signal processing while remote button is pressed
- // int whichCode index for bedOutputPins[], indicates the output pin to operate
- void bed( int whichPin ) {
- digitalWrite( bedOutputPins[ whichPin ], LOW);
- do{
- // Delay for mechanical bed operation
- delay( 150 );
- receivedValue = mySwitch.getReceivedValue();
- receivedValueString = String( receivedValue );
- mySwitch.resetAvailable();
- } while( bedInputCodes[ whichPin ].equals( receivedValueString ) );
- digitalWrite( bedOutputPins[ whichPin ], HIGH );
- codeFound = true;
- return;
- }
- // provides "rolling code"-like functionality for incapable remotes by tracking fanSpeed
- void toggleFanSpeed() {
- switch ( fanSpeed ) {
- case 0: case 3: {
- // fan is off or at 3rd speed
- mySwitch.setPulseLength( 460 );
- mySwitch.send( fanOutputCodes[ 1 ] );
- fanSpeed = 1;
- break;
- }
- case 1: {
- // fan is on 1st speed
- mySwitch.setPulseLength( 459 );
- mySwitch.send( fanOutputCodes[ 2 ] );
- fanSpeed = 2;
- break;
- }
- case 2: {
- // fan on 2nd speed
- mySwitch.setPulseLength( 454 );
- mySwitch.send( fanOutputCodes[ 3 ] );
- fanSpeed = 3;
- break;
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement