I am completely lost!!!!
Can someone please explain to me the highlighted section of code and how they work?
// 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
}
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;
}
// 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
}
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);*
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.
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?
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.
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.
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.
/*
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);
}
}