Sending a variable via RF communication

Hello Everyone!

I have trouble with a program;

I want to send a variable form one Arduino to another.

The first Arduino only sends a number [0 / 1 / 2], canged in the program itself (No imput or anything else)

The second one reads the state and turns a red or green LED on or off.
[0= red off, green off / 1= red on, green off / 2= red off, green one]

I have found a tutorial, but i don´t understand the “Split data” part.
I don´t need it, but it seems to be nessesary for the code to work.

my actual state:

transmitter:

#include <VirtualWire.h>

int var = 1;
char CharMsg[21];// The string that we are going to send trought rf 

void setup() {
  // VirtualWire setup
  vw_set_ptt_inverted(true);

  vw_setup(2000); // Bits per sec
  vw_set_tx_pin(12);// Set the Tx pin. Default is 12
}

void loop() {
  sprintf(CharMsg, "%d,%d,%d,%d,%d,%d", var);
  delay(5);

  vw_send((uint8_t *)CharMsg, strlen(CharMsg));
  vw_wait_tx(); // Wait until the whole message is gone
 
  delay(40);
}

reciever:

#include <VirtualWire.h>


const int receive_pin = 12;
int maxSensorData = 6;
int recieved;
char StringReceived[22];

int LEDred = 10;
int LEDgreen = 11;


void setup() {

  pinMode(LEDred, OUTPUT);
  pinMode(LEDgreen, OUTPUT);
  
  Serial.begin(9600);                        // initialising serial
  vw_set_rx_pin(receive_pin);                // virtual wire setup
  vw_set_ptt_inverted(true);                 // virtual wire setup
  vw_setup(2000);                            // bits per second
  vw_rx_start();                             // Start the receiver PLL running





  
} // END void setup

void loop(){

  
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if (vw_get_message(buf, &buflen)) {        //Taking the data from the control base
    int i;
    for (i = 0; i < buflen; i++) {            
      StringReceived[i] = char(buf[i]);      // data to array
    }
    // split data into array sensorData
    sscanf(StringReceived, "%d,%d,%d,%d,%d,%d", &recieved); // Converts a string to an array
  }



  if (recieved == 1){
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
  } else {
    if (recieved == 2) {
      digitalWrite(LEDred, LOW);
      digitalWrite(LEDgreen, HIGH);
    } else {
      digitalWrite(LEDred, LOW);
      digitalWrite(LEDgreen, LOW);
    }
  }

  
  Serial.print(recieved);
  Serial.println(""); 
  
  
 
  // reset variable StringReceived
  memset(StringReceived, 0, sizeof(StringReceived));
  delay(5);
}

and the link of the tutorial:

http://www.simsso.de/?type=arduino/mx-05v-mx-fs-03v

what do i wrong?

can you fix the conde and explain it to me?

Thank you very much,
Atvilar

what do i wrong?

You didn't say. What happens when you run the program?

If you will be using the function sprintf(), you should learn how it works. Start here: http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm http://www.cplusplus.com/reference/cstdio/sprintf/

Hello,

I sorry...

Both codes are running without any decompiling problems or so, but there is no LED shining, and if i read the serial monitor on the reciever side, it only recieves "0" (nothing?).

The var on the transmitter side = 1

thanks

Atvilar

  sprintf(CharMsg, "%d,%d,%d,%d,%d,%d", var);

For every format specifier, you are supposed to supply a variable. 6 format specifiers means 6 variables are supposed to be listed.

    for (i = 0; i < buflen; i++) {           
      StringReceived[i] = char(buf[i]);      // data to array
    }

Completely unnecessary.

    sscanf(StringReceived, "%d,%d,%d,%d,%d,%d", &recieved);

Same problem as sprintf. The number of variables needs to match the number of format specifiers.

Fix the sender and receiver and test again.

I would recommend that you start with the simple examples providing with VirtualWire.

The very simplest just sends a character string, which is all you need for your example.

Hello,

Thanks jremington, but it is not compartible with my module(s)

Thanks PaulS, I will hav a look at it after work and report to you.

Yours

Atvilar

but it is not compartible with my module(s)

You [u]already using[/u] VirtualWire, just incorrectly.