A
A
Alexey Smirnov2015-01-09 20:55:09
Arduino
Alexey Smirnov, 2015-01-09 20:55:09

How to convert integer to char (Arduino)?

To exchange data between two Arduinos, I use the VirtualWire library - although this is not important.
When transferring data, I need the message itself to contain the message number. For this I use counter j = j +1;
And I need to add the j value to the string that is assigned to *msg.
For example, to get:

const char *msg = "Сообщение номер " + j;  //но если так писать, то выдает ошибку

How can I convert the j value from integer to char so that I can assign it to *msg ?
Program code:
// transmitter.pde
//
// Simple example of how to use VirtualWire to transmit messages
// Implements a simplex (one-way) transmitter with an TX-C1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley ([email protected])
// Copyright (C) 2008 Mike McCauley
// $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $
 
#include <VirtualWire.h>
 
int j=0;
 
void setup()
{
Serial.begin(9600); // Debugging only
//Serial.println("setup");
 
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(200); // Bits per sec
}
 
void loop()
{
  j = j + 1;

const char *msg = "На месте должно быть значение j";
 
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone

digitalWrite(13, false);
delay(200);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2015-01-09
@ERAFY

itoa, for example

Y
yanchumak, 2015-01-10
@yanchumak

char msg[64]={0};
sprintf(msg,"The value should be %d",j);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question