TX to RX analogWrite(led1,......);

Hi

I am sending 3 byte's from the tx module to the rx module, I want to use the byte's to ramp up 3 led's, from
0 to 255. I can't remember how to make the code to read it. Has something
to do with the analogWrite(led1, ); Here is my code

TX

#include <VirtualWire.h>

int RF_TX_PIN = 2;

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_set_tx_pin(RF_TX_PIN);
       vw_setup(2400);    // 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);
 }

Rx

#include <VirtualWire.h>

int RF_RX_PIN = 2;

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

void setup()
{
pinMode(led1, OUTPUT);     
pinMode(led2, OUTPUT); 
pinMode(led3, OUTPUT);
Serial.begin(9600);
vw_set_rx_pin(RF_RX_PIN);
vw_setup(2400);
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("");
}

 analogWrite(led1,        );
 analogWrite(led2,        );
 analogWrite(led3,         );

}

vw_set_ptt_inverted(true); // Required for RX Link Module
Does your transmit module have a Push To Talk input pin? If not, get rid of this line.

char fade1msg[12];
Where do you actually put some data in here?

analogWrite(led1, ); // put in buf[x] here, find the byte that represents 0-255 from the incoming data

On the Tx side, why not make
byte fade1msg[3];
fade1msg[0] = analogRead(A1)>>2; // 10 bits down to 8 bits
fade1msg[1] = analogRead(A2)>>2;
fade1msg[2] = analogRead(A3)>>2;

On the receive side:

analogWrite (led1, (buf[0]);
analogWrite (led2, (buf[1]);
analogWrite (led3, (buf[2]);

Something along those lines.

So something like this.....

TX

#include <VirtualWire.h>

int RF_TX_PIN = 2;

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


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_set_tx_pin(RF_TX_PIN);
       vw_setup(2400);    // Bits per sec

 }
 
 void loop(){

     byte fade1msg[3];
        fade1msg[0] = analogRead(A1)>>2; // 10 bits down to 8 bits
        fade1msg[1] = analogRead(A2)>>2;
        fade1msg[2] = analogRead(A3)>>2;

     sprintf(fade1msg, "%03d:%03d:%03d", fade1, fade2, fade3);
       
     vw_send((uint8_t *)fade1msg, strlen(fade1msg));
     vw_wait_tx();
   
   Serial.println(fade1msg);
 }

RX

#include <VirtualWire.h>

int RF_RX_PIN = 2;

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

void setup()
{
pinMode(led1, OUTPUT);     
pinMode(led2, OUTPUT); 
pinMode(led3, OUTPUT);
Serial.begin(9600);
vw_set_rx_pin(RF_RX_PIN);
vw_setup(2400);
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("");
}

analogWrite (led1, (buf[0]);
analogWrite (led2, (buf[1]);
analogWrite (led3, (buf[2]); 

}

Yes, something like that.

You have a Tx with Push To Transmit pin?
If not, delete this
vw_set_ptt_inverted(true); // Required for RX Link Module

Is there someone out there, that can tell me why the analogWrite (led1, (buf[0])); isn't reading the (buf) and
making the led's go from 0 to 255. I am Sorry......

#include <VirtualWire.h>

int RF_TX_PIN = 2;

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

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

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

 void setup(){
       Serial.begin(9600);
       vw_set_tx_pin(RF_TX_PIN);
       vw_setup(2400);    // Bits per sec

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

     fade1 = sen1 / 4;
     fade2 = sen2 / 4;
     fade3 = sen3 / 4;
     fade4 = sen4 / 4;
       
     char fade1msg[12];
   

     sprintf(fade1msg, "%03d:%03d:%03d:%03d", fade1, fade2, fade3, fade4);
       
     vw_send((uint8_t *)fade1msg, strlen(fade1msg));
     vw_wait_tx();
   
   Serial.println(fade1msg);
 }
#include <VirtualWire.h>

int RF_RX_PIN = 8;

int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;

void setup()
{
pinMode(led1, OUTPUT);     
pinMode(led2, OUTPUT); 
pinMode(led3, OUTPUT);
pinMode(led3, OUTPUT);

Serial.begin(9600);

vw_set_rx_pin(RF_RX_PIN);
vw_setup(2400);
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("");
}

analogWrite (led1, (buf[0]));
analogWrite (led2, (buf[1]));
analogWrite (led3, (buf[2]));
analogWrite (led4, (buf[3]));
}

In your receiver sketch, I think the value in buff[] is a character. You may need to covert this to an integer before using it in the analogWrite statement.

I just posed two sketches that do what you are looking for:

http://forum.arduino.cc/index.php?topic=186155.0

Its near the bottom of the thread.

Dave

BenBenBen:
One person is helping me out of 94, great odds.

Not everyone that reads the thread knows how to help you. And this attitude will put off most of the others.

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

You have serial prints there, how about sharing the output with us?

How to use this forum

See point 11.

Anyway, what is the above supposed to be doing? What is char () for?

On the Rx side:

int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;

these are not all PWM capable pins. Choices are 3,5,6,9,10,11 on an Uno.

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led3, OUTPUT); <<< This should be led4

If I use this RX code with out the //analogWrite (led1, (buf[0]));//analogWrite (led2, (buf[1]));
//analogWrite (led3, (buf[2]));//analogWrite (led4, (buf[3]));. The Serial Monitor on the Arduino looks like this.

As I move the pot's the #'s ramp up form 0 to 255.

000:000:112:087
000:000:063:064
000:000:001:035
002:000:107:088
053:000:093:081
111:000:003:039
143:000:100:087
173:000:110:092
181:000:006:042
181:000:069:070
181:000:116:093
180:000:012:041
180:000:034:051
196:000:116:090
255:000:017:041
255:000:008:035
255:000:111:085
255:000:041:051
255:000:002:032
255:000:109:085
255:000:076:070
255:000:001:035
255:000:103:086
255:000:105:087
255:000:006:040
255:000:090:080
255:000:115:093
255:000:011:042
255:000:053:061
255:000:120:093
255:000:017:042
255:000:022:043
255:000:116:089
255:000:025:043
255:000:006:034
255:000:112:086
255:000:058:060
255:000:001:034
255:000:106:086
255:000:091:079
255:000:003:037
255:000:100:086
255:000:008:041
255:000:072:071
255:000:014:042
255:000:036:051
255:000:119:091
255:000:020:042

If I unblock the analogWrite (led1, (buf[0]));. It reads blank....(Serial Monitor of Arduino).

Here is the RX code I am using right now....

#include <VirtualWire.h>

int RF_RX_PIN = 8;

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

void setup()
{
pinMode(led1, OUTPUT);     
pinMode(led2, OUTPUT); 
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);

Serial.begin(9600);

vw_set_rx_pin(RF_RX_PIN);
vw_setup(2400);
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("");
}

//analogWrite (led1, (buf[0]));
//analogWrite (led2, (buf[1]));
//analogWrite (led3, (buf[2]));
//analogWrite (led4, (buf[3]));
}

Well, you kind of have conflicting needs there.
You want 0x00 to 0xFF (0-255) for the PWM pins,
but I think you need to print those in HEX format to see them in the monitor??

Serial.print(buf*, HEX);*

And this attitude will put off most of the others.

It's why I skipped it.

What I am missing is that the analogWrite (led1, (buf[0]));. The (buf[0] is not reading the byte. 0 to 255.

The other code, I remove the int pot4 = A4; I make the Serial Monitor on the Arduino, work right.
TX

#include <VirtualWire.h>

int RF_TX_PIN = 2;

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_tx_pin(RF_TX_PIN);
       vw_setup(2400);    // 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);
 }
#include <VirtualWire.h>

int RF_RX_PIN = 8;

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




void setup()
{
pinMode(led1, OUTPUT);     
pinMode(led2, OUTPUT); 
pinMode(led3, OUTPUT);


Serial.begin(9600);

vw_set_rx_pin(RF_RX_PIN);
vw_setup(2400);
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("");
}




analogWrite (led1, (buf[0]));
analogWrite (led2, (buf[1]));
analogWrite (led2, (buf[2]));


}

If you used the auto-format tool your code would be easier to read.

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("");
  }

  analogWrite (led1, (buf[0]));
  analogWrite (led2, (buf[1]));
  analogWrite (led2, (buf[2]));
}

In the above code you are printing buf only if you get a message, but you are doing an analogWrite regardless. Do you see a problem with that? Almost all of the time you are doing analogWrite on undefined values.

You haven't answered my question. What is char() for here?

      Serial.print(buf[i], char());

I put the char because I thought I could just read the value and analogWrite to the Led. The serial monitor
reads (255:000:000), but I see now that not going to work.

It won't work if you ignore my suggestions, that's for sure.

Well It took A bit of time, but I think I got it working. What I would like to know, is. Is this the best way to
send byte's of data from, Tx to Rx, without losing byte's of data. Or is there A better way?

RX

#include <VirtualWire.h>

int RF_RX_PIN = 8;

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

char sensor1Char[4];      
char sensor2Char[4];
char sensor3Char[4];

int sensor1Data;        
int sensor2Data; 
int sensor3Data;


void setup()
{
pinMode(led1, OUTPUT);     
pinMode(led2, OUTPUT); 
pinMode(led3, OUTPUT);


Serial.begin(9600);

vw_set_rx_pin(RF_RX_PIN);
vw_setup(2400);
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],HEX);
}
Serial.println("");
}

int i;

for (i = 0; i < 3; i++)             
	
sensor1Char[i] = char(buf[i]);   
 
sensor1Data = atoi(sensor1Char);


 for (i = 0; i < 3; i++)
        
 sensor2Char[i] = char(buf[i+4]); 
  	
 sensor2Data = atoi(sensor2Char); 


 for (i = 0; i < 3; i++)
        
 sensor3Char[i] = char(buf[i+8]); 
  	
sensor3Data = atoi(sensor3Char); 

Serial.println(sensor1Data);
Serial.println(sensor2Data);
Serial.println(sensor3Data);


analogWrite (led1,sensor1Data);
analogWrite (led2,sensor2Data);
analogWrite (led3,sensor3Data);

}

I would just add a sync byte or 2 so the receiver knows where the real data is.
Like send 0xAA 0x55 and then three bytes.
Receiver loops on a byte until it see AA, if next byte is not 55 then go back to looking for AA.
When it sees AA 55 then the next 3 bytes are the LED data and it can write them to the correct LED.

Hi, there are three more things I want to ask.

One, on the TX code, what is ("%03d:%03d:%03d") in this line of code =
sprintf(fade1msg, "%03d:%03d:%03d",fade1, fade2, fade3);

Two, in the RX code, what does this do (atoi) in this line of code =
sensor1Data = atoi(sensor1Char);

Three, To get the (sync byte or 2). I do I go about this? In the TX code, change this line.
sprintf(fade1msg, "%03d:%03d:%03d",fade1, fade2, fade3); to
sprintf(fade1msg, "%1103d:%2203d:%3303d",fade1, fade2, fade3);

I don't know how to read the sync byte on the rx side.

TX

#include <VirtualWire.h>

int RF_TX_PIN = 2;

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_tx_pin(RF_TX_PIN);
       vw_setup(2400);    // 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);
 }

RX

#include <VirtualWire.h>

int RF_RX_PIN = 8;

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

char sensor1Char[4];      
char sensor2Char[4];
char sensor3Char[4];

int sensor1Data;        
int sensor2Data; 
int sensor3Data;


void setup()
{
pinMode(led1, OUTPUT);     
pinMode(led2, OUTPUT); 
pinMode(led3, OUTPUT);


Serial.begin(9600);

vw_set_rx_pin(RF_RX_PIN);
vw_setup(2400);
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],HEX);
}
//Serial.println("");
}

int i;

for (i = 0; i < 3; i++)             
	
sensor1Char[i] = char(buf[i]);   
 
sensor1Data = atoi(sensor1Char);


 for (i = 0; i < 3; i++)
        
 sensor2Char[i] = char(buf[i+4]); 
  	
 sensor2Data = atoi(sensor2Char); 


 for (i = 0; i < 3; i++)
        
 sensor3Char[i] = char(buf[i+8]); 
  	
 sensor3Data = atoi(sensor3Char); 

//Serial.println(sensor1Data);
//Serial.println(sensor2Data);
Serial.println(sensor3Data);


analogWrite (led1,sensor1Data);
analogWrite (led2,sensor2Data);
analogWrite (led3,sensor3Data);


}

One, on the TX code, what is ("%03d:%03d:%03d") in this line of code =
sprintf(fade1msg, "%03d:%03d:%03d",fade1, fade2, fade3);

Can't be bothered to google that, huh?

Two, in the RX code, what does this do (atoi) in this line of code =
sensor1Data = atoi(sensor1Char);

Can't be bothered to google that either, huh?

Three, To get the (sync byte or 2). I do I go about this? In the TX code, change this line.
sprintf(fade1msg, "%03d:%03d:%03d",fade1, fade2, fade3); to
sprintf(fade1msg, "%1103d:%2203d:%3303d",fade1, fade2, fade3);

No! Read up on format specifiers. The constant data does not go after the %.

I get it, (Arduino Forum's) don't ask ????
Because they will just tell you to google it..........

I guess, I should stop wasting my time and just google it...