OSC char concat issues

Hey all,

I'm trying to send some sensor values using OSC. an example of what I'd like to send is the following:

Blockquote
cue/1/go

My full code looks like this:

#include <SPI.h>
#include <Ethernet.h>

#include <ArdOSC.h>

#include "SR04.h" // this is the ultrasound lib

// this is the ultrasonic pin setup
#define TRIG_PIN 7
#define ECHO_PIN 6 


//set up the ultrasonic bits
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;


byte myMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte myIp[]  = { 192, 168, 1, 177 }; 


int destPort=12000;
byte destIp[]  = { 192, 168, 1, 69 };

OSCClient client;

//create new osc message
OSCMessage global_mes;
  
void setup(){ 

 Serial.begin(9600);//Initialization of Serial Port

 
 Ethernet.begin(myMac ,myIp); 
 

}
  
void loop(){

    int distance_value=sr04.Distance();  // This is the distance value in int format
    char* start_text = "/cue/";
    char* end_text = "/go";
    char formatted_message[30];

    Serial.print(end_text);
    strcpy (formatted_message,start_text); //
    strcat (formatted_message,distance_value);
    strcat (formatted_message,end_text);
     
   
   
   Serial.print(formatted_message);
 
  global_mes.setAddress(destIp,destPort);
  
  global_mes.beginMessage(formatted_message);

  client.send(&global_mes);
  
  global_mes.flush(); //object data clear
  }

My issues is trying to merge the start_text, distance_value, and end_text into one variable. I've had a look in the library, and the function will only accept a char type. The console currently looks like this:

Blockquote
13:06:58.018 -> /cue//go
13:06:58.066 -> /cue/⸮⸮⸮⸮⸮Eޭ⸮⸮⸮⸮/go
13:06:58.066 -> /cue/⸮⸮⸮⸮⸮Eޭ⸮⸮⸮⸮/go
13:06:58.112 -> /cue/⸮⸮⸮/go
13:06:58.159 -> /cue/⸮⸮⸮/go
13:06:58.301 -> /cue//go
13:06:58.348 -> /cue//go
13:06:58.348 -> /cue/⸮/go
13:06:58.394 -> /cue/⸮/go
13:06:58.394 -> /cue/⸮/go
13:06:58.441 -> /cue/⸮/go
13:06:58.441 -> /cue/⸮/go

What do I need to do to the distance_value variable to make this work?

Strcat expects a pointer to a cString. An int won’t be converted automagically (the compiler probably gives you a warning)
Use sprintf() or itoa()

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.