Loading...
Pages: [1] 2   Go Down
Author Topic: VirtualWire Guidance  (Read 354 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am completely lost!!!!
Can someone please explain to me the highlighted section of code and how they work?

Quote
// transmitter.pde
//
// Simple example of how to use VirtualWire to transmit messages
// Implements a simplex (one-way) transmitter with an TX-C1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@open.com.au)
// Copyright (C) 2008 Mike McCauley
// $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

#include <VirtualWire.h>

const int led_pin = 11;
const int transmit_pin = 12;
const int receive_pin = 2;
//const int transmit_en_pin = 3;

void setup()
{
  // Initialise the IO and ISR
  vw_set_tx_pin(transmit_pin);
  vw_set_rx_pin(receive_pin);
  //vw_set_ptt_pin(transmit_en_pin);
  //vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000);    // Bits per sec
}

byte count = 1;
void loop()
{
  char msg[7] = {'h','e','l','l','o',' ','#'};

  msg[6] = count;

  digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
  vw_send((uint8_t *)msg, 7);  
  vw_wait_tx(); // Wait until the whole message is gone
  digitalWrite(led_pin, LOW);
  delay(1000);
  count = count + 1;
}
Quote
// receiver.pde
//
// Simple example of how to use VirtualWire to receive messages
// Implements a simplex (one-way) receiver with an Rx-B1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@open.com.au)
// Copyright (C) 2008 Mike McCauley
// $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

#include <VirtualWire.h>

const int led_pin = 6;
const int transmit_pin = 12;
const int receive_pin = 11;
//const int transmit_en_pin = 3;

void setup()
{
    delay(1000);
    Serial.begin(9600);   // Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR
    vw_set_tx_pin(transmit_pin);
    vw_set_rx_pin(receive_pin);
    //vw_set_ptt_pin(transmit_en_pin);
    //vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);    // Bits per sec

    vw_rx_start();       // Start the receiver PLL running
}

void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
   int i;
   
   digitalWrite(led_pin, HIGH); // Flash a light to show received good message
   // Message with a good checksum received, print it.
   Serial.print("Got: ");
   
   for (i = 0; i < buflen; i++)
   {
       Serial.print(buf, HEX);
       Serial.print(' ');
   }
   Serial.println();

        digitalWrite(led_pin, LOW);
    }
}
« Last Edit: January 21, 2013, 11:19:49 pm by modsbyus » Logged

0
Offline Offline
Tesla Member
***
Karma: 71
Posts: 6596
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, because its entirely unreadable in yellow....
Logged

USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Changed color highlighting to brown... Sorry
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1001
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

transmittor:
The count is incremented with every loop.
The msg[6] is set a '#' to begin with but replaced with count.
The vw_send() function sends the bytes in 'msg[]', and uses '7' for the number of bytes to send.


receiver:
The library is able to receive a maximum of VW_MAX_MESSAGE_LEN bytes.
So the receiver should be able to receive all those bytes.
The 'buf' is therefor declared with the size of VW_MAX_MESSAGE_LEN.
The vw_get_message() fills the buffer and the buffer size if a complete message is received.
Every byte is printed and followed by a newline.
Logged

USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In the Transmitter sketch, it says "char msg[7] = {'h','e','l','l','o',' ','#'};" but I never see that come through on the receiver. instead all I see is
Got: 68 65 6C 6C 6F 20 58
Got: 68 65 6C 6C 6F 20 59
Got: 68 65 6C 6C 6F 20 5A
Got: 68 65 6C 6C 6F 20 5B
Got: 68 65 6C 6C 6F 20 5C
Got: 68 65 6C 6C 6F 20 5D
Got: 68 65 6C 6C 6F 20 5E
Got: 68 65 6C 6C 6F 20 5F
Got: 68 65 6C 6C 6F 20 60
Got: 68 65 6C 6C 6F 20 61
 The last set of numbers increments and the 68 65 6C 6C 6F 20 stays the same. Where does 68 65 6C 6C 6F 20 come from and why don't I see "hello #" come through?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 311
Posts: 35470
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Go here: http://www.asciitable.com/
See if you can't figure out what 68, 65, 6C, 6C, 6F, 20, and 58 correspond to.

As a hint, bytes and chars are the same size, but they are not the same type. Functions do different things based on type as well as value. Specifically, different functions (though the name is the same) are called depending on type.
Logged

USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, That cleared things up beautifully.
Logged

USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am still having a little trouble.
In this statement,
Code:
    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
   int i;
What does int i; do/mean?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 311
Posts: 35470
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What does int i; do/mean?
It declares a variable, local to the block, named i, of type int, with no initial value.
Logged

USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Whats the point of it?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 311
Posts: 35470
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Whats the point of it?
In that snippet? None. Perhaps there is in some code you didn't post.
Logged

USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The snippet was copied from the code above. the Receiver code.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is used in the "for" loop as a counter.
If you didn't use it outside of the body of the for loop, you could declare within the for expression itself.
Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So, now I am more confused than when I started... I used a different piece of code from someone else and its there too but there isn't a counter in this code that im aware of.
Code:
/*

Sensor Receiver
By Markus Ulfberg 2012-07-06

Gets a sensor reading 0-1023 in a char array
from RF Transmitter unit via VirtualWire
converts char array back to integer

*/

#include <VirtualWire.h>

// LED's
int ledPin = 13;//Set up pin 13 as the recieve indicator
int ActionLED = 4;//Set up pin for LED


// Sensors
int Sensor1Data;//initialize the variable Sensor1Data for use

// RF Transmission container
 char Sensor1CharMsg[4];
// Trying to use 5 instead to fit trailing null char
// go back to 4 if this does not work.
//char Sensor1CharMsg[5];

void setup() {
  Serial.begin(9600);//Initialize serial communication
  Serial.print("RX Start");//Print RX Start in the Serial Terminal
  
  // sets the digital pin as output
  pinMode(ledPin, OUTPUT);//Set the ledPin as an Output
  pinMode(ActionLED, OUTPUT);//Se up the ActionLED pin as an Output
  digitalWrite(ActionLED, LOW);//Set the ActionLED LOW 'off'
    
    // VirtualWire
    // Initialise the IO and ISR
    // Required for DR3100
    //vw_set_ptt_inverted(true);
    // Bits per sec
    vw_setup(300);//Set up VirualWire Communication Speed
    
    // Start the receiver PLL running
    vw_rx_start();      

} // END void setup

void loop(){
  //Unsigned Integer of 8 bit length buffer Max Message Length
    uint8_t buf[VW_MAX_MESSAGE_LEN];
  //Unsigned Integer of 8 bit length buffer length Max Message Length
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    
    // Non-blocking
    if (vw_get_message(buf, &buflen))
    {
int i;
        // Turn on a light to show received good message
        digitalWrite(13, true);
Serial.print("Remote Temp: ");//Print Remote Temp: in the serial terminal
        // Message with a good checksum received, dump it.
        for (i = 0; i < buflen; i++)
{           
          // Fill Sensor1CharMsg Char array with corresponding
          // chars from buffer.   
         Sensor1CharMsg[i] = char(buf[i]);
}
        
        // Null terminate the char array
        // This needs to be done otherwise problems will occur
        // when the incoming messages has less digits than the
        // one before.
        Sensor1CharMsg[buflen] = '\0';
        
        // Sensor1Data is atoi (ASCII to Integer) from Sensor1CharMsg
        Sensor1Data = atoi(Sensor1CharMsg);
        
        
        // DEBUG
        
        Serial.println(Sensor1Data); //Print Sensor1Data in the serial Terminal
        
        // END DEBUG
                
        // Turn off light to and await next message
        digitalWrite(13, false);
        //If Sensor1Data (temp) is less than 75 degrees turn on the ActionLED
        if (Sensor1Data < 75)
        {
          digitalWrite(ActionLED, HIGH);
        }
        delay(100);
    } //If Sensor1Data (temp)is more than 75 degrees turn off ActionLED
        else if (Sensor1Data > 75)
        {
          digitalWrite(ActionLED, LOW);
        }
      
    
    }

So, what does it do here?
« Last Edit: January 27, 2013, 12:48:30 pm by modsbyus » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 311
Posts: 35470
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So, what does it do here?
You can't color stuff in a code box. This question, on it's own, makes no sense.
Logged

Pages: [1] 2   Go Up
Print
 
Jump to: