Issue with converting String to char

Hey, im kinda new to all of this so sorry if i made a stupid mistake in advance.
I am currently playing around with 433Mhz wireless modules and am using the "RadioHead" Library. I want to send a message from one arduino to another which i have already done successfully previously. However, i cant figure out how to send a message that i dont hardcode into the code. I have connected a potentiometer to my arduino with the transmitter and am trying to send a value (0-255) to the receiver based on the potentiometer. But i cant figure out how to do that. Here is the problematic code:

// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library 
#include <SPI.h> 

// Def needed variables
int val;
int new_val;
 
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
 
void setup()
{
    // Initialize ASK Object
    rf_driver.init();

    pinMode(A0, INPUT);
}
 
void loop()
{
  val = analogRead(A0);
  new_val = map(val, 0, 1023, 0, 255);
    const char *msg = new_val;
    rf_driver.send((uint8_t *)msg, strlen(msg));
    rf_driver.waitPacketSent();
    delay(10);
}

my problem is at const char *msg = new_val;. If i do it like that, i receive random letters.
If i just do const char *msg = "test;", it works.

I tried to solve this by doing const char *msg = String(new_val);
but then i just get the
cannot convert 'String' to 'const char*' in initialization
Error.

a char string represents numeric values with ASCII characters (e.g. "123")

how about

void loop()
{
    val = analogRead(A0);
    new_val = map(val, 0, 1023, 0, 255);

    char msg [30];
    sprintf (msg, "%d\n", new_val);

    rf_driver.send((uint8_t *)msg, strlen(msg));
    rf_driver.waitPacketSent();
    delay(10);
}

1 Like

Perhaps you meant

new_val = val / 4;

?

Hey, thanks! that works. Would you mind explaining what exactly you did there?

Actually, i now have a different problem that i didnt notice right away. The code on the receiver end relies on knowing how many letters the message has. previously i just always used 4 letters, but i cant really do that with a number. Is there any way thta i can just extend the number so that it always has 4 digits? like turning 200 into 0200

No, I'm pretty sure he is a baller and ment:

new_val = val >> 2;

:sunglasses:

But the compiler would recognise that, and do it for him/her.

But, I think we probably agree that the map was not what was wanted.

    sprintf (msg, "%04d\n", new_val);
1 Like

What i wanted was to measure the state of the potentiometer and then convert that onto a scale that i could use on the receiver to directly put into an analogWrite. I wasnt sure what my program would do if i used a float instead of an integer. thats why i used the map instead of new_var = var/4

200 - 72 = 0200 :wink:

You don't use a float in there, and neither does analogWrite.
Either way, the "map" will almost certainly not give you the results you expect (unless you expect slightly odd results).

wouldnt var/4 return a float in some cases? like if var was 6 it would return 1.5, right?

No, never.

snprintf is a safer choice as it guards against buffer overflows and will tell you how big a buffer you actually need.

because both var and 4 are integers and an integer division is performed. if you want a float result, you may need to specify 4 as a float, var/4.0

...but analogWrite still doesn't take a float.

i never said it did

Great.

Ok anyways. i now have another and hopefully my last issue for now. In my receiver code i want to use the message as input for analogWrite. But when i try to convert it to an integer to use it i get "cannot convert 'String' to 'int' for argument '2' to 'void analogWrite(uint8_t, int)'"
here is the part of the code i use for that:

    msg.remove(4, 1);
    Serial.println(msg);
    msg.toInt();
    analogWrite(3, msg);

the msg.remove is there because the string i receive has a weird rectangle on its end otherwise.

it is not common practice in these cases (and especially with arduino).

qualcomm made us a take a course on using these types of functions to protect against string buffer overflows intentionally attempted by hackers who send extra long strings over the radio interface.