My microcontroller UART has a small buffer to process data, therefore i would like sent data in small chunks, in string format using sprintf. sample[k] = voltage sample not random number, and comma is required to separate voltages.
So I would like to fill up the sample array with random numbers, instead of if() else
statement. How to make sprintf()
use dynamic index ?
char str[512];
int sample[512];
printf("T1[");
int i = 0;
for (int j = 0; j < 64; j++)
{
for(int k = 0; k < 8; k++)
{
sample[k] = rand() % 256;
}
sprintf( str, "%d,%d,%d,%d,%d,%d,%d,%d", sample[0],sample[1],sample[2],sample[3],sample[4],sample[5],sample[6],sample[7]);
printf(str);
}
printf("]\r\n");
i could do something like this
if (j == 0)
sprintf( str, "%d,%d,%d,%d,%d,%d,%d,%d", sample[0],sample[1],sample[2],sample[3],sample[4],sample[5],sample[6],sample[7]);
if (j == 1)
sprintf( str, "%d,%d,%d,%d,%d,%d,%d,%d", sample[8],sample[9],...
I have tried:
sprintf( str, "%d,%d,%d,%d,%d,%d,%d,%d", sample[i++],sample[i++],sample[i++],sample[i++],sample[i++],sample[i++],sample[i++],sample[i++]);