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);
}
}
}
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...
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!
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!
/*
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();
}
}
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?
/*
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???
}
}