Hi,
I'm developing an application where I want to send the value of a potenctiometer trought RF.
Everything works just fine, except that I'm receiving only the first character of the value.
Transmitter code
#include <VirtualWire.h>
int potPin = A0;
int sensorValue = 0;
//TRANSMITTER 12
void setup()
{
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
Serial.begin(9600);
Serial.println("Hello world");
// declare the ledPin as an OUTPUT:
pinMode(potPin, INPUT);
}
void loop()
{
int newValue = analogRead(potPin);
if(abs(newValue - sensorValue) > 2){
// read the value from the sensor:
sensorValue = newValue;
String stringOne = "Pot: ";
String myString = String(sensorValue);
char charArray[9];
((String)myString).toCharArray(charArray, 9);
send(charArray);
Serial.print("Send: ");
Serial.println(charArray);
}
}
void send (char *message)
{
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message is gone
}
Receiver code
#include <VirtualWire.h>
/*
SimpleReceive
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
*/
int enablePin = 7;
int in1Pin = 6;
int in2Pin = 5;
int normalizedSpeed= 0;
boolean reverse = false;
int length = VW_MAX_MESSAGE_LEN ;
uint8_t message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = length; // the size of the message
char inData[20]; // Allocate some space for the string
byte index = 0; // Index into array; where to store the character
void setup()
{
Serial.begin(9600);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
}
void loop()
{
char strValue[100];
int index = 0;
if (vw_get_message(message, &messageLength)) // Non-blocking
{
Serial.print("Received: ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
inData[index++] = message[i]; // Store it
}
inData[index] = '\0'; // Null terminate the string
char s[VW_MAX_MESSAGE_LEN];
Serial.println();
Serial.print("Get: ");
Serial.println(inData);
//strValue[index] = 0;
//int intSpeed = atoi(strValue);
//normalizedSpeed = map(intSpeed,0,1023,0,255);
// Serial.println("Speed: "+normalizedSpeed);
// boolean reverse = digitalRead(switchPin);
// reverse = false;
}
// setMotor(normalizedSpeed, reverse);
}
//http://www.buildcircuit.com/how-to-use-rf-module-with-arduino/#sthash.G3G7mRVK.dpuf
void setMotor(int speed, boolean reverse)
{
analogWrite(enablePin, speed);
digitalWrite(in1Pin, ! reverse);
digitalWrite(in2Pin, reverse);
}
Transmitter output example:
Hello world
Send: 444
Send: 347
Send: 244
Receiver output example
Received: 444
Received: 347
Received: 2
Received: 0
Get: 0
Received: 3
Get: 3
Sometimes I receive the whole message, but sometimes only the first char.
Any ideas?
thanks in advance