So my project is for school, and we're basically trying to send an integer value, which is from a potentiometer/joystick, to another Arduino, using radio transmitter and receiver. Everything works fine using characters, like sending our names, but when using an integer everything gets messed up.
Here is the tx code:
#include <RH_ASK.h>
#include <SPI.h>
#define JOYY A0
#define JOYX A1
RH_ASK rf_driver;
int pos_x;
int pos_y;
void setup() {
Serial.begin(9600);
rf_driver.init();
pinMode(13, OUTPUT);
pinMode(JOYY, INPUT);
pinMode(JOYX, INPUT);
}
void loop() {
readJoy();
//Serial.println(pos_y);
char *msg = (char*)pos_y;
Serial.println(*msg);
rf_driver.send(msg, strlen(msg));
rf_driver.waitPacketSent();
}
void readJoy() {
pos_x = analogRead(JOYX);
pos_y = analogRead(JOYY);
pos_x = map(pos_x, 497, 1023, 1, 173);
pos_y = map(pos_y, 497, 1023, 1, 173);
//Serial.println(pos_y);
//Serial.println(pos_x);
}
and this is the rx code:
#include <RH_ASK.h>
#include <SPI.h>
#include <Servo.h>
int val = 0;
int x = 0;
RH_ASK rf_driver;
Servo myservo;
void setup() {
rf_driver.init();
Serial.begin(9600);
myservo.attach(6);
}
void loop() {
uint8_t buf[10];
uint8_t buflen = sizeof(buf);
if (rf_driver.recv(buf, &buflen))
{
Serial.print("Message: ");
Serial.println((char*)buf);
//myservo.write(buf);
}
}
The problem I'm having is with
char *msg = (char*)pos_y;
Serial.println(*msg);
I'm trying to convert the integer into a char, and then print it. There are no errors, but looking in the serial monitor, the expected value 0 is actually just nothing, and pushing the joystick up or down just writes a bunch of these ⸮, backwards question marks. I've tried lots of different methods, most of them just doing the same thing.
Can anyone help me? Thanks.