Yep, that is it - wondering how to send wirelessly a number from one arduino to another... Nothing fancy, not a negative, not a fraction, just a number between 1 and 99 and receive a number on the other end, preferably as an integer...
There is an example sketch with 4 buttons on transmitter side and 4 LEDs on receiver, and it work just fine. It is so reliable in fact that after two nights of trying to send an integer I feel like assigning letter codes to numbers on transmitter side and making 99 switch / case statements for receiver
There must be a better way?..
The code below is a 4 buttons/LEDs example, posting my attempts to produce something similar for integers would be a waste of space:
Transmitter:
//
// example 13.2tx - http://tronixstuff.com/tutorials > Chapter 13
//
// basic tx sketch - based on code by Mike McCauley 2010 http://www.open.com.au/mikem/arduino
//
// don't forget the 10k resistor between digital pin 0 and GND for independent operation
// Need these lines below ///////////////////
#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
////////////////////////////////////////////
void setup()
{
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for RF Link module
vw_setup(2400); // Bits per sec
vw_set_tx_pin(1); // pin 1 is the TX pin on our Arduino Duemilanove
pinMode(8, INPUT); // for each of the four buttons
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
delay(2000); // give the radio module time to get going
}
const char *l1 = "a"; // a~h will represent the states of the four buttons, 1~4 high or low
const char *h1 = "b"; // the less data to send wirelessly the better
const char *l2 = "c";
const char *h2 = "d";
const char *l3 = "e";
const char *h3 = "f";
const char *l4 = "g";
const char *h4 = "h";
void loop()
{
if (digitalRead(8)==LOW)
{
vw_send((uint8_t *)l1, strlen(l1)); // send the data out to the world
vw_wait_tx(); // Wait for sending of message to finish.
delay(20);
}
else if (digitalRead(8)==HIGH)
{
vw_send((uint8_t *)h1, strlen(h1)); // send the data out to the world
vw_wait_tx(); // Wait for sending of message to finish.
}
delay(20);
if (digitalRead(9)==LOW)
{
vw_send((uint8_t *)l2, strlen(l2)); // send the data out to the world
vw_wait_tx(); // Wait for sending of message to finish.
delay(20);
}
else if (digitalRead(9)==HIGH)
{
vw_send((uint8_t *)h2, strlen(h2)); // send the data out to the world
vw_wait_tx(); // Wait for sending of message to finish.
}
delay(20);
if (digitalRead(10)==LOW)
{
vw_send((uint8_t *)l3, strlen(l3)); // send the data out to the world
vw_wait_tx(); // Wait for sending of message to finish.
delay(20);
}
else if (digitalRead(10)==HIGH)
{
vw_send((uint8_t *)h3, strlen(h3)); // send the data out to the world
vw_wait_tx(); // Wait for sending of message to finish.
}
delay(20);
if (digitalRead(11)==LOW)
{
vw_send((uint8_t *)l4, strlen(l4)); // send the data out to the world
vw_wait_tx(); // Wait for sending of message to finish.
delay(20);
}
else if (digitalRead(11)==HIGH)
{
vw_send((uint8_t *)h4, strlen(h4)); // send the data out to the world
vw_wait_tx(); // Wait for sending of message to finish.
delay(20);
}
}
And the receiver:
//
// exercise 13.2rx - http://tronixstuff.com/tutorials > Chapter 13
//
// receive and decode data from tx unit, display in serial monitor
//
// based on code by Mike McCauley 2010 http://www.open.com.au/mikem/arduino
//
// Need these lines below ///////////////////
#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
uint8_t buf[VW_MAX_MESSAGE_LEN]; // this is an array of unsigned integers 8-bits long. In other words, bytes between 0 and 65535
uint8_t buflen = VW_MAX_MESSAGE_LEN;
////////////////////////////////////////////
void setup()
{
// wake up the wireless receiver
vw_set_ptt_inverted(true); // need this line
vw_setup(2400); // sets speed of data reception.
vw_set_rx_pin(0); // this is the RX pin number - 0 on a Duemilanove
vw_rx_start(); // start the receiver!
Serial.begin(9600);
}
void loop()
{
// check to see if there is received data in the buffer, and that it came through correctly.
// if the message didn't come through completely, it will be ignored
if (vw_get_message(buf, &buflen))
{
switch(buf[0])
{
case 'a':
Serial.println("Button 1 low");
break;
case 'b':
Serial.println("Button 1 high");
break;
case 'c':
Serial.println("Button 2 low");
break;
case 'd':
Serial.println("Button 2 high");
break;
case 'e':
Serial.println("Button 3 low");
break;
case 'f':
Serial.println("Button 3 high");
break;
case 'g':
Serial.println("Button 4 low");
break;
case 'h':
Serial.println("Button 4 high");
}
}
}