Power LED’s using RF transmitter and receiver with Arduino

arduinoreceiverRFtransmitterwireless

I'm working on a project that I have two sensors, two Arduinos (a YUN and an UNO), two LED's, and an RF transmitter/receiver

It's a pretty simple procedure: Activate a force sensor, send it via RF to the receiver and light up the corresponding LED. So my design looks like this –

Sensor 1 and Sensor 2 —> Arduino YUN —> RF Transmitter –> Wireless

RF Receiver —> Arduino UNO —> LED 1 and LED 2

So if Sensor 1 is activated LED 1 lights up and if Sensor 2 is activated LED 2 lights up.

So far I am able to send the signal and distinguish between each sensor by viewing the serial monitor. If I hit sensor 1, the Uno receives sensor 1's signal. Same with sensor 2. I'm just stuck on how to get the LED's to activate. I tried Serial.read() code so whenever I send Sensor 1 or Sensor 2, it would read the serial then digitalWrite the led high. But that didn't work. Any advice would be much appreciated. Here's my code if you would like to look at it.

Transmitter:

    // Transmitter Arduino Yun
    #include <VirtualWire.h>
    int pin0 = 0;
    int pin1 = 1;


    void setup()
    {
        vw_set_ptt_inverted(true);  // Required by the RF module
        vw_setup(2000);            // bps connection speed
        Serial.begin (9600);
        vw_set_tx_pin(3);         // Arduino pin to connect the transmitter data pin
    }

    void loop()
    {
       int force = analogRead(pin0); //  analog reading from the Force sense resistor
       int force1 = analogRead(pin1); //  analog reading from the Force1 sense resistor

       const char *msg = "Sensor 1";
       const char *ms = "Sensor 2";


       if (force > 100) {
       vw_send((uint8_t *)msg, strlen(msg));
       digitalWrite(led_pin, HIGH);

       vw_wait_tx();        // We wait to finish sending the message
       delay(200);         // We wait to send the message again                
    }

      if (force1 > 100) {
       vw_send((uint8_t *)ms, strlen(ms));
       digitalWrite(led_pin1, HIGH);


       vw_wait_tx();        // We wait to finish sending the message
       delay(200);         // We wait to send the message again                
    }


    }

And the Receiver:

    //Reciever

    #include <VirtualWire.h>

    const int led_pin = 3;
    const int led_pin1 = 4;
    char data[20];
    void setup()
    {
        pinMode (3,OUTPUT);
        pinMode (4, OUTPUT);
      Serial.begin(9600);  //Debugging
      vw_set_ptt_inverted(true); //Required
      vw_setup(2000);  //bps
      vw_set_rx_pin(2);
      vw_rx_start();  //Start receiver


    }

    void loop()
    {

      uint8_t buf[VW_MAX_MESSAGE_LEN];
      uint8_t buflen = VW_MAX_MESSAGE_LEN;

      if (vw_get_message(buf, &buflen))  
      {
        int i;


         for (i = 0; i < buflen; i++)
        {
          Serial.write(buf[i]);  // The received data is stored in the buffer
                                // and sent through the serial port to the computer
        }

      }

      if (Serial.available()) {
         char ser = Serial.read();

         if(ser == 'Sensor 1') {
             digitalWrite(led_pin, HIGH);
          }
          else if(ser == 'Sensor 2') {
             digitalWrite(led_pin1, HIGH);
          }
       }
    }

Thanks again for any insight.

Best Answer

you are comparing a single character, which may be 'S' which is the first character in teh string "Sensor 1", with, again, A string literal which you have declared as a character.

In C programming a single quote ' mark means that its an ASCII character, like '&' or 's'. If you want a string, you use doublequote, "Sensor" and even single character strings work "S".

What you need to do is set up a char buffer as an empty "string" for the received data.

char rxbuffer[64]; //64 byte long char buffer!
memset (rxbuffer,0,64); //this clears the bytes to 0. otherwise could be garbage data

64 is arbitrary, but means it can hold 63 character long string, and a null terminator at the end.

then you should use the arduino Serial.readBytesUntil() function. http://arduino.cc/en/Serial/ReadBytesUntil

This would looke like:

//you need to include this at the top!! 
#include <string.h>

if(Serial.available()){
    Serial.readBytesUntil('\0', rxbuffer, 64);
    //use the C++ standard function call strstr to search buffer for matchin
    //input string

    //was it sensor 1 command?
    if( strstr(rxbuffer, "Sensor 1") ){
        digitalWrite(LED1, HIGH);
    }else{
        //maybe it was sensor 2?
        if( strstr(rxbuffer, "Sensor 2") ){
            digitalWrite(LED2, HIGH);
        }

    } //end if it wasnt first sensor message

}//end serial available, processing message after read

So there you go, that should work. change the LED pin numbers/names though