Send rf433 signal when reading rfid card


I am trying to send a signal with an rf433 transmitter when a parralax rfid reader detects an rfid card. My code seems to stop working when I scan a card.

Can anybody tell me why?

#include <RH_ASK.h>
#include <SPI.h>

RH_ASK driver;

int  val = 0;
char code[10];
int bytesread = 0;

void setup()
  //  Serial.begin(9600);    // Debugging only
  //  if (!driver.init())
  //    Serial.println("init failed");

  Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
  pinMode(2, OUTPUT);  // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
  digitalWrite(2, LOW);                  // Activate the RFID reader

void loop()
  rf433Trans("Hello world!");

void rf433Trans(char * msg) {
  //  char *msg = "Hello World!"; // doesn't need to be constant
  driver.send((uint8_t *)msg, strlen(msg));

void rfidReader() {

  if (Serial.available() > 0) {         // if data available from reader
    if ((val = == 10) {  // check for header
      bytesread = 0;
      while (bytesread < 10) {           // read 10 digit code
        if ( Serial.available() > 0) {
          val =;
          if ((val == 10) || (val == 13)) { // if header or stop bytes before the 10 digit reading
            break;                       // stop reading
          code[bytesread] = val;         // add the digit
          bytesread++;                   // ready to read next digit
      if (bytesread == 10) {             // if 10 digit read is complete
        Serial.print("TAG code is: ");   // possibly a good TAG
        Serial.println(code);            // print the TAG code
      bytesread = 0;
      digitalWrite(2, HIGH);                  // deactivate the RFID reader for a moment so it will not flood
      delay(1500);                       // wait for a bit
      digitalWrite(2, LOW);                  // Activate the RFID reader

Have you tried a RadioHead example sketch to ensure that the hardware is working? Same for the card reader, have you tested it separately?

Thank you for the reply! Yes I have tried both independently and they work fine on their own.

Okay, please edit your original post and put the code in code tags so it can gain some commentary.

Also, do you get any serial output when it runs?

I get the rfid code once in the serial monitor instead of in a loop, and then the code just seems to stop - I can put in other code instead of the rf433() and that works fine, but something about sending the message over rf433 seems to stop the rfid code from working.. I have considered using two Arduinos communicating over i2c instead, where one Arduino is running the rfid code and the other is running the rf433 code. I just really would prefer to just use 1

Why did you #include spi.h?

Try printing the value of 'bytesread'. It may be referencing memory outside of the 'code' array. In connection with that, try increasing the size of 'code'.

Generally, add a lot more serial debug statements.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.