Receiving with RCSwitch

Hi,

I am trying to implement a matching Nano Rx/Tx using RCSwitch. The transmitter works great. However, I cannot get the receiver to work.

My goal is to have the receiver blink an LED on A0 if it receives a binary "0001" or blink a different LED on D9 if it receives a binary "0010".

Any help would be appreciated! My code is as follows:

RCSwitch mySwitch = RCSwitch();

void setup() {
Serial.begin(9600);
pinMode(12, OUTPUT);
pinMode(A1, OUTPUT);
mySwitch.enableReceive(0);
}

void loop() {
if(mySwitch.available()) {
  int value=mySwitch.getReceivedValue();
  if (value == 0001) {
    digitalWrite(9, HIGH);
    delay(5000);
    digitalWrite(9, LOW);
    delay(1000);
  }
  else if (value == 0010) {
    digitalWrite(A0, HIGH);
    delay(5000);
    digitalWrite(A0, LOW);
    delay(1000);
  }
}

delay(1000);

mySwitch.resetAvailable();
}

try using serial.readstring()

may also need to change Serial.setTimeout() default is 1000ms

void setup() {
  Serial.begin(9600); // whatever the serial speed is on transmitting device
  pinMode(9, OUTPUT);
  pinMode(A0, OUTPUT);
  char value;
  Serial.setTimeout(1000);

}

void loop() {
  value = serial.readstring();

  if (value == "0001") {
    digitalWrite(9, HIGH);
    delay(5000);
    digitalWrite(9, LOW);
    delay(1000);
  }
  if (value == "0010") {
    digitalWrite(A0, HIGH);
    delay(5000);
    digitalWrite(A0, LOW);
    delay(1000);
  }
// no need for delay, if no input it will loop again waiting 1000ms at the readstring til there is an input
}

ffejrxx: try using serial.readstring()

Please don't. That's one of the most terrible additions to Arduino. If you want to use serial you're wayyy better of following Robin2's Serial Input Basics (updated).

And I don't see how it will help here (where no Serial is used...)

@ohemgee, can you please edit the post to include code-tags/ See How to use the forum for that.

And can you also explain what the problem is and what you tried?

Hello,

I've edited the post with code-tags.

I have tried to send binary data from one Nano to another. The transmitter side seems to work just fine. However, the receiver side isn't processing the binary it receives. I can see the receiver picking it up; however, the Arduino never processes that it receives a "0001" by a HIGH output on A0 or "0010" by a HIGH output on D9.

To troubleshoot and see if it's just my coding, I have tried the SendDemo and ReceiveDemo_Simple and _Advanced. SendDemo worked. I connected an oscope and was able to see the pulses from the transmitter and picked up by the receiver. However, the ReceiveDemos aren't working. I have had no output on the serial monitor. I have tried this wirelessly as well as hard wired (TX → RX, RX → TX, GND → GND from each Nano).

I have also searched Dr. Google extensively and nothing that's out there applies or has worked. So if anyone has any insight or experience with RCSwitch, I'd greatly appreciate a push in the right direction.

It's rather pointless to call Serial.begin() if you are never going to Serial.print() anything.

It's rather pointless to debug by guesswork. Serial.print() values that you wonder about.

I tried to Serial.print() some lines during debug, such as availability and received value. The serial monitor was still blank.

Then please show us the code with which you tried to debug. And a schematic.

And you do know that "value == 0010" is NOT binary 0b0010 aka decimal 2? NOR is it just plain decimal 10. It's decimal 8, because it starts with a 0 so the compiler sees it as an octal representation.