control 3 led (PWM) w/ 3 potentio on rf link 433mhz

hi,
i've been trying controlling wireless 3 pwm led with 3 potentio using rf link kit 433mhz
and still not succes yet.

TX code:

#include <VirtualWire.h>

int pot1 = A1;
int pot2 = A2;
int pot3 = A3;

int sen1 = 0;
int sen2 = 0;
int sen3 = 0;

void setup(){
Serial.begin(9600);
vw_set_ptt_inverted(true); // Required for RX Link Module
vw_setup(2000); // Bits per sec
}

void loop(){
sen1 = analogRead(pot1);
sen2 = analogRead(pot2);
sen3 = analogRead(pot3);

char fade1msg[4];
itoa(sen1,fade1msg,10);
char fade2msg[4];
itoa(sen2,fade2msg,10);
char fade3msg[4];
itoa(sen3,fade3msg,10);

vw_send((uint8_t *)fade1msg, strlen(fade1msg));
vw_send((uint8_t *)fade2msg, strlen(fade2msg));
vw_send((uint8_t *)fade3msg, strlen(fade3msg));
vw_wait_tx();

}

RX code:

#include <VirtualWire.h>
int led1 = 3;
int led2 = 5;
int led3 = 6;

int hitung=0,hasil[3];
int fade1;
char fade1msg[4];

void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
vw_setup(2000);
vw_rx_start();
}

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++)
{
fade1msg = char(buf*);*
}
fade1msg[buflen] = '\0';
fade1 = atoi(fade1msg);
hasil[hitung]=fade1;
hitung++;
if(hitung > 2){
analogWrite(led1,hasil[0]);
analogWrite(led2,hasil[1]);
analogWrite(led3,hasil[2]);
vw_wait_rx();
hitung=0; // reset counter
}
}
}
can anybody help me?
thanks

       char fade1msg[4];
       itoa(sen1,fade1msg,10);

Suppose that you read 1023 from the potentiometer. '1', '0', '2', '3', and the trailing NULL are 5 characters. Not a good fit in a 4 element array.

As soon as that code gets done sending data, it starts again. You're not giving the receiver much time to do anything.

On the sender, you are sending up to 12 characters (should be 15). On the receiver, you are expecting to receive no more than 4. I sense a problem there.

Because you failed to post your code correctly (inside code tags posted by the # icon), the forum software helpfully mangled it for you. Therefore, I can't tell if you are properly handling data in the for loop.

The receiver code is set up to receive one value, not 3.

You need to change the sender to send 1 message, containing comma-delimited values. Look at sprintf() for a clue on how to put the three ints in the char array, separated by commas.

On the receiver, it is not necessary to copy the data from a byte array to a char array. Use a char array, and cast it to a byte array as needed.

char buf[VW_MAX_MESSAGE_LEN];
if (vw_get_message((byte *)buf, &buflen))

Then, look at strtok() (NOT strtok_r()) to parse the string you receive.

thanks PaulS,
yes, i forgot to change the 1023 value.
i've been modified TX code:

#include <VirtualWire.h>

int pot1 = A1;
int pot2 = A2;
int pot3 = A3;

int sen1 = 0;
int sen2 = 0;
int sen3 = 0;

int fade1 = 0;
int fade2 = 0;
int fade3 = 0;

void setup(){
Serial.begin(9600);
vw_set_ptt_inverted(true); // Required for RX Link Module
vw_setup(2000); // Bits per sec

}

void loop(){
sen1 = analogRead(pot1);
sen2 = analogRead(pot2);
sen3 = analogRead(pot3);
fade1 = sen1 / 4;
fade2 = sen2 / 4;
fade3 = sen3 / 4;

char fade1msg[12];

sprintf(fade1msg, "%03d:%03d:%03d", fade1, fade2, fade3);

vw_send((uint8_t *)fade1msg, strlen(fade1msg));
vw_wait_tx();

Serial.println(fade1msg);
}

result on serial monitor TX:

053:211:117
053:211:117
053:211:117
053:211:117

i still not understand to use this code on RX :slight_smile:

my RX code:

#include <VirtualWire.h>

int led1 = 3;
int led2 = 5;
int led3 = 6;

void setup()
{
pinMode(led1, OUTPUT);      
pinMode(led2, OUTPUT);  
pinMode(led3, OUTPUT); 
Serial.begin(9600);
vw_setup(2000); 
vw_rx_start(); 
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message((byte *)buf, &buflen)) 
{
int i;


for (i = 0; i < buflen; i++)
{
Serial.print(buf[i], char());
}
Serial.println("");
}
}

and the serial output are:

069:148:117
069:148:117
069:148:117
069:148:117
069:148:117
069:148:117
069:148:117

any sample code to extract these code with strtok() ?
thanks

any sample code to extract these code with strtok() ?

What have you tried? strtok() only takes two arguments - the string to parse and an array of delimiters. You only have two delimiters - the colon and the NULL, and the NULL is implied.

char *token1 = strtok(buf, ":");
if(token1)
{
   char *token2 = strtok(NULL, ":");
   if(token2)
   {
      char *token3 = strtok(NULL, ":");
      if(token3)
      {
          // Got all the tokens...
      }
   }
}
#include <VirtualWire.h>

void setup()
{
Serial.begin(9600);
Serial.println("setup");
vw_setup(2000); 
vw_rx_start(); 
}

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++)
{
char *token1 = strtok(buf, ":");
if(token1)
{
   char *token2 = strtok(NULL, ":");
   if(token2)
   {
      char *token3 = strtok(NULL, ":");
      if(token3)
      {
          // Got all the tokens...
      }
   }
}
}
}
}

error: invalid conversion from 'uint8_t*' to 'char*'

where to put this code?
(sorry i'am noob)

char *token1 = strtok((char *)buf, ":");