I thought I would make an Arduino controlled Bait Boat using a 433Mhz RF Transmitter and Receiver Module pair. It's going well but I have two questions that I would like to ask if I may, one regarding the Transmitter sketch and one on the Receiver.
My version uses two joysticks with the intention of controlling two 9V DC motors on a model boat. The Arduino takes the output of the Joysticks and converts the readings to strings. An Output string concatenates the two with a comma separator and the RF Driver transmits the Output string using the 433 Mhz RF Transmitter.
The receiver code needs the received string to be a fixed length but the string varies from 2 x single character to 2 x four character as the Joysticks are moved.
So I added this code to include leading Xs where required:
The code works ok, in fact really well, it adds leading 0s instead of Xs and with the comma separator, I get a fixed 9 character Output string irrespective of the JoyStick read values. But I don't understand why it changes the X to a 0!
I'm guessing that the str_Zero = X; sees the word zero as a numerical string for some reason.
Does anyone have an explanation please?
Regards
Graeme
Code:
/*
*
* The Bait Boat
* Rev 02.04 _Transmitter
* 14/04/24
*
* Description
* Prototype to test the Transmitter/Receiver Pair
*
* Rev History
* Rev 02.01_Transmitter with text message string
* Rev 02.02_Transmitter - Transmitter with text message string
* Rev 02.02_Receiver - Receiver
* Rev 02.03_Transmitter - Transmitter with Joystick position string
* Rev 02.03_Receiver - Receiver
* Rev 02.04_Transmitter - Transmitter with Dual Joysticks and 2 x 4 character string
* Rev 02.04_Receiver - Receiver
*
*/
// Libraries
#include <RH_ASK.h>
#include <SPI.h>
// Create RH Driver
RH_ASK rf_driver;
// Variables
int DelayTime = 200;
int JoyStick1Pin = A1;
int JoyStick1Val;
int JoyStick2Pin = A2;
int JoyStick2Val;
int X;
String str_Zero;
String str_JoyStick1Val;
String str_JoyStick2Val;
String str_Output;
void setup()
{
rf_driver.init(); // Arduino Pin 12
Serial.begin (9600);
pinMode(JoyStick1Pin, INPUT);
pinMode(JoyStick2Pin, INPUT);
}
void loop()
{
// Read Joystick 1 value
JoyStick1Val = analogRead (JoyStick1Pin);
str_Zero = X;
// Add 3 leading zeros to 1 character reading
if (JoyStick1Val < 10)
{
str_JoyStick1Val = str_Zero + str_Zero + str_Zero + JoyStick1Val;
}
// Add 2 leading zeros to 2 character reading
if (JoyStick1Val >= 10 && JoyStick1Val < 100)
{
str_JoyStick1Val = str_Zero + str_Zero + JoyStick1Val;
}
// Add 1 leading zero to 3 character reading
if (JoyStick1Val >= 100 && JoyStick1Val < 1000)
{
str_JoyStick1Val = str_Zero + JoyStick1Val;
}
if (JoyStick1Val >= 1000)
{
str_JoyStick1Val = JoyStick1Val;
}
// Read Joystick 2 value
JoyStick2Val = analogRead (JoyStick2Pin);
// Add 3 leading zeros to 1 character reading
if (JoyStick2Val < 10)
{
str_JoyStick2Val = str_Zero + str_Zero + str_Zero + JoyStick2Val;
}
// Add 2 leading zeros to 2 character reading
if (JoyStick2Val >= 10 && JoyStick2Val < 100)
{
str_JoyStick2Val = str_Zero + str_Zero + JoyStick2Val;
}
// Add 1 leading zero to 3 character reading
if (JoyStick2Val >= 100 && JoyStick2Val < 1000)
{
str_JoyStick2Val = str_Zero + JoyStick2Val;
}
if (JoyStick2Val >= 1000)
{
str_JoyStick2Val = JoyStick2Val;
}
// Compose Output string
str_Output = str_JoyStick1Val + "," + str_JoyStick2Val;
// Send 9 character string
static char *msg = str_Output.c_str();
rf_driver.send((uint8_t *)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(DelayTime);
Serial.println (str_Output);
}
// EoF
A much simpler option is to send the variable(s) in binary, rather than a clumsy character string, or worse a String object that leads to program crashes.
For two joystick values something like this will work:
Well the Transmitter code works lovely, Serial.prints of both Joystick readings return a full range of 0 to 1023.
I'm struggling to get the Receiver code to work though. A Serial.print displays 248 for Joystick[0] and 1 for Joystick[1]. When I move the Joysticks I get non linear numbers displayed.
The maximum number displayed is 255 on Joystick 1. My guess is that it's because the results displayed for the Receiver array are 8 bit so unable to deal with the Joystick analogue 10 bit values.
The rf_driver library (Radiohead) needs to see a uint8_t variable to transmit a value. Is there an alternative way of transmitting 10 bit numbers using a uint8_t variable?