Help changing this IR sketch to RF

Hi all,

I have a generic 12 channel rf keyfob that I want to use to toggle relays on and off via the 433 Mhz receiver I have.

I can run RC Switch sketches to obtain the codes and stuff that this remote sends from each different button press so I was wondering what sort of sketch I would need to write, in order to designate a set code/button to a set relay.

Using this sketch I used a while back to do the same sort of thing with IR, is there going to be a sketch like this needed or something much more complex considering it is RF and does that mean noise becomes a problem?

Thank you very much for your help!

/*
* IRremote Library - Copyright 2009 Ken Shirriff
* created by Rui Santos, http://randomnerdtutorials.wordpress.com
* Control LED's with a remote control
* 2013
*/

#include <IRremote.h>

int IR_Recv = 3;   //IR Receiver Pin 3
int g_ledPin = 5;  //green LED pin 5
int y_ledPin = 6;  //yellow LED pin 6
int r_ledPin = 9;  //red LED pin 9
int b_ledPin = 10; //blue LED pin 10
int ledPins[] = {5, 6, 9, 10};  //array with all the LED's pins
int ledStates[] ={0, 0, 0, 0};  //this means the LED's states at first is 0 = LOW
int i=0;  //LED index for the arrays

IRrecv irrecv(IR_Recv);
decode_results results;

//variables to make the LED blink when selected
int ledState = LOW;             // ledState to turn the LED on or off
long previousMillis = 0;        // stores last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)

void setup(){
  Serial.begin(9600);  //starts serial communication
  irrecv.enableIRIn(); // Starts the receiver
  pinMode(g_ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(y_ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(r_ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(b_ledPin, OUTPUT);      // sets the digital pin as output
}

void loop(){
  //decodes the infrared input
  if (irrecv.decode(&results)){
    long int decCode = results.value;
    Serial.println(decCode);
   //switch case to use the selected remote control button
   switch (results.value){
      case 16580863: //when you press the POWER button
       //this if/else statement makes sure that LED is ON or OFF before move to the next LED
       if(ledStates[i]==0)
          digitalWrite(ledPins[i], LOW);
       else
          digitalWrite(ledPins[i], HIGH);
       Serial.println("Next LED");
       //makes sure that when we reach the last LED it goes to the first LED again
       if(i>=3)
          i=-1; 
       i+=1;             
       break;
       
      case 16582903: //when you press the 1 button
        //this if/else statement makes sure that LED is ON or OFF before move to the previous LED
        if(ledStates[i]==0)
          digitalWrite(ledPins[i], LOW);
        else
          digitalWrite(ledPins[i], HIGH);
        Serial.println("Previous LED");
        //makes sure that when we reach the first LED it goes to the last LED
        if(i<=0)
          i=4;
        i-=1;
        break;
       
      case 16615543: //when you press the 2 button
        if(ledStates[i]==0){ //if the LED is off, It will turn on
          Serial.println("Turns ON the LED Selected");
          digitalWrite(ledPins[i], HIGH);  //sets the LED on
          ledStates[i]=1;                  //updates the LED state
        }
        else{
          Serial.println("Turns OFF the LED Selected"); //else: the LED is on, It will turn off
          digitalWrite(ledPins[i], LOW);   //sets the LED off
          ledStates[i]=0;                  //updates the LED state
        }       
        break; 
       
      case 16599223: //when you press the 3 button
        Serial.println("Turns OFF all the LED's");
        digitalWrite(g_ledPin, LOW);   // sets the green LED off
        ledStates[0] =0;               // updates the LED state
        digitalWrite(y_ledPin, LOW);   // sets the yellow LED off
        ledStates[1] =0;               // updates the LED state
        digitalWrite(r_ledPin, LOW);   // sets the red LED off
        ledStates[2] =0;               // updates the LED state
        digitalWrite(b_ledPin, LOW);   // sets the blue LED off
        ledStates[3] =0;               // updates the LED state
        break; 
       
      default:
        Serial.println("Waiting");
    }
    irrecv.resume(); // Receives the next value from the button you press
  }
  //this if statment makes the LED blink if it's selected and off
  if(ledStates[i]==0){
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;  
      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;
      // set the LED with the ledState of the variable:
      digitalWrite(ledPins[i], ledState);
    }
  }
}

I can run RC Switch sketches to obtain the codes and stuff that this remote sends from each different button press

So, use that class and those "codes and stuff" in place of the IRRecv class and results(.value).

So you are saying that even though the sketch for the IR was designed for IR, the rf code is exactly the same because that is what the rf module is designed to do? To handle the receiving of the signal and represent that signal as a code which form then can be utilised.

So in this instance, the IR pin is effectively going to become the rf signal/data pin and all I need to do is change the code that the rf remote is throwing out with the code I had for the IR remote buttons?

Can it really be as easy as this substitution and changing the codes per button in that very sketch?

If so, I feel silly for overlooking it and very narrowminded...

DOH!

Thank you very much and I will try it asap!

PaulS:

I can run RC Switch sketches to obtain the codes and stuff that this remote sends from each different button press

So, use that class and those "codes and stuff" in place of the IRRecv class and results(.value).

Paul,

Could you please elaborate on what you mean by class and the values and stuff?

Am I able to supply you with the information and values you need and you could write the sketch for me?

I'm really lost! :confused:

Here is a list of keywords used to control RCSwitch.

#######################################

Syntax Coloring Map For RCSwitch

#######################################

#######################################

Datatypes (KEYWORD1)

#######################################

RCSwitch KEYWORD1

#######################################

Methods and Functions (KEYWORD2)

#######################################

##########
#SENDS Begin
##########
switchOn KEYWORD2
switchOff KEYWORD2
sendTriState KEYWORD2
send KEYWORD2
##########
#SENDS End
##########

##########
#RECEIVE Begin
##########
enableReceive KEYWORD2
disableReceive KEYWORD2
available KEYWORD2
resetAvailable KEYWORD2
setReceiveTolerance KEYWORD2
getReceivedValue KEYWORD2
getReceivedBitlength KEYWORD2
getReceivedDelay KEYWORD2
getReceivedProtocol KEYWORD2
getReceivedRawdata KEYWORD2
##########
#RECEIVE End
##########

##########
#OTHERS Begin
##########
enableTransmit KEYWORD2
disableTransmit KEYWORD2
setPulseLength KEYWORD2
setProtocol KEYWORD2
setRepeatTransmit KEYWORD2
##########
#OTHERS End
##########

#######################################

Constants (LITERAL1)

#######################################

I can run RC Switch sketches to obtain the codes and stuff that this remote sends from each different button press

Where is the sketch to prove this?

/*
  Simple example for receiving
  
  http://code.google.com/p/rc-switch/
*/

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
}

void loop() {
  if (mySwitch.available()) {
    
    int value = mySwitch.getReceivedValue();
    
    if (value == 0) {
      Serial.print("Unknown encoding");
    } else {
      Serial.print("Received ");
      Serial.print( mySwitch.getReceivedValue() );
      Serial.print(" / ");
      Serial.print( mySwitch.getReceivedBitlength() );
      Serial.print("bit ");
      Serial.print("Protocol: ");
      Serial.println( mySwitch.getReceivedProtocol() );
    }

    mySwitch.resetAvailable();
  }
}
      Serial.print( mySwitch.getReceivedValue() );

If you can print this value, please explain why you can't also store this value and use this value.

Because I don't know how to write the code to say when I receive this value, turn relay on and then when I receive again, turn it off. I tried using IR kind of code that I used to do same thing with ir remote but the readings in serial monitor were showing it giving HEAPS of different codes. I'm assuming that was because I was using an IR class instead of RF?

Either way, could you PLEASE write me a code so I can simply put in my code from the rf transmitter and see how you write the sketch?

I've tried nearly everything I know and it's so hard learning all of this when you have no one to help, except you guys on here! :wink:

So you're saying that something like this could work? I have forgotten how to make the arduino read the state of the relayswitch pin and toggle it on and off so I just wrote this as an example of what I think you might be asking me to do?

Thanks again! :smiley:

/*
  Simple example for receiving
  
  http://code.google.com/p/rc-switch/
*/

#include <RCSwitch.h>

int RELAYSWITCH = 5; //Pin # that my relay's trigger/switching
                 //terminal is connected to

RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
  pinMode(RELAYSWITCH, OUTPUT); //setting relay switch pin as output
}

void loop() {
  if (mySwitch.available()) {
    
    int value = mySwitch.getReceivedValue();
    
    if (value == 552468953) {
      digitalWrite(RELAYSWITCH, HIGH);
    } else {
      digitalWrite(RELAYSWITCH, LOW);
    }

    mySwitch.resetAvailable();
  }
}

Because I don't know how to write the code to say when I receive this value, turn relay on and then when I receive again, turn it off.

if(value == someKnownValue)
{
   if(currentState == HIGH)
      currentState = LOW;
   else
     currentState = HIGH;

   // Use currentState somehow
}

I don't mean to be expecting to be spoon fed but could you please incorporate that into the sketch above? That way I don't chase my tale by laying out the sketch completely arse about? :smiley:

Thanks!

int value = mySwitch.getReceivedValue();
    
    if (value == 552468953) {

Is this on a Due?

I don't mean to be expecting to be spoon fed

Apparently, you do mean that you want to be spoon fed.

but could you please incorporate that into the sketch above?

No. What you need to change borders on trivial.

That way I don't chase my tale by laying out the sketch completely arse about?

But, that's so amusing...

What's on a due?

Ok, I'll have a go! thanks Paul....smart ass! :wink:

Is this software running on a Due?
If it isn't, the code I highlighted can never work.

/*
  Simple example for receiving
  
  http://code.google.com/p/rc-switch/
*/

#include <RCSwitch.h>

int RELAYSWITCH = 5; //Pin # that my relay's trigger/switching
                 //terminal is connected to

RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
  pinMode(RELAYSWITCH, OUTPUT); //setting relay switch pin as output
}

void loop() {
  if (mySwitch.available()) {
    
    int value = mySwitch.getReceivedValue();
    
    if (value == codefromRFremote) {
      if(currentState == HIGH)
      currentState = LOW;
      else
      currentState = HIGH;
      //what do you mean use currentState somehow? It isn't
      //changing to show that it's a command or anything?
    }

    mySwitch.resetAvailable(); //What does this do???
  }
}

Is this what I need Paul?

AutoElecHack:
Is this what I need Paul?

Does that even compile? You need to replace codefromRFremote with some real value that you KNOW you get from the switch.

And, this:

RCSwitch mySwitch = RCSwitch();

is (still) crap.

RCSwitch mySwitch;

is the right way.

Why is it still crap? Am going to give this a go today and see what I can make happen.

Is there a pdf book I can download that will teach me how to use and write C?

What's the best way to learn because no one I know can write it and I would like to gain atleast a basic understanding.