I would like to use the sprintf function to convert the value from the Arduino ADC to a string, however I am getting an error and can replicate the problem with a small sketch.
In my code I have a rx_Buffer, if I use the sprintf function, the value of rx_Buffer changes.
What am I doing wrong?
#define update_time 2000
int int_Input_Registers[10];
long time_now = 0;
long time_lastupdate = 0;
int test_values = 0;
int counter_update = 0;
char* rx_Buffer[]={"0000"};
char* str_Analog_Input_Registers[]={"0000", "0000", "0000","0000",
"0000", "0000", "0000", "0000", "0000", "0000" };
int int_ADC_Active_Read[]={A0,A1,A2,A3,A6,A7}; // Arrays of PINS to Read
int int_adc_Reads = 0;
void setup()
{
// Initialize the programme variables
int_adc_Reads = (sizeof(int_ADC_Active_Read)/sizeof(int));
// Setup the Serial Port
Serial.begin(115200);
delay(100);
// Software Version
Serial.println("Test Code");
}
void loop()
{
// Avoid delay() loops
time_now = millis();
if (time_now - time_lastupdate > update_time) {
Serial.print("rx_Buffer: ");
Serial.println(rx_Buffer[0]);
// Simulate some ADC Values
for (int i = 0; i < int_adc_Reads; i++){
int_Input_Registers[i] = test_values;
sprintf(str_Analog_Input_Registers[i], "%04X", test_values); // ?? How can the value of rx_Buffer[0] change?
//str_Analog_Input_Registers[i] = "0100"; // The value of rx_Buffer does not change.
// Debug - Output the values
Serial.print(i);
Serial.print(" ADC[");
Serial.print( int_ADC_Active_Read[i] ); // Displays the PIN number
Serial.print("] Value: ");
Serial.print(str_Analog_Input_Registers[i]);
Serial.println();
test_values++;
}
// Set the counter and time values
time_lastupdate = time_now;
counter_update++;
} // End of Update Timer Loop
}
Thank you Sir. Your initial response send me in the right direction. Saw your update, I made it work like this, and thank you for the example. I want to iterate the rx_Buffer as it is made from a couple of protocol elements.
The String class brings a lot to the table, but it also eats a lot while at the table. Try this example:
//#define STRINGCLASS
void setup()
{
Serial.begin(9600);
#ifdef STRINGCLASS // C++ String class: 3752 bytes
int index;
String msg = "Hello world!";
index = msg.indexOf('!');
if (index > 0) {
Serial.print("Found it at: ");
Serial.println(index);
}
#else // C string code: 2300 bytes
char msg[] = "Hello world!";
char *ptr = strchr(msg, '!');
if (ptr) {
Serial.print("Found it at: ");
Serial.println(ptr - msg);
}
#endif
}
void loop()
{
}
Both programs do pretty much the same thing: find the '!' character in a string. The #define determines which block of code gets compiled into the program. The String (note uppercase 'S') class generates 3752 bytes of code, while the C string (lowercase 's') generates 2300 bytes. Given the limited resources on many uC's, many prefer to use the C string code.
I have been coding Arduino for 2 weeks and did not know the S/s implementation - should try and read more. CPU Execution speed is of the essence and I want to generate as little overhead code as possible, lean on string functions and other code in loop(). Need the horsepower for the callbacks. Really appreciate the help.
If you need more horsepower but still want to use the Arduino IDE, look at the Teensy 3.2. It's clocked at 72MHz instead of 16KHz, has 256K of flash memory, 64K of SRAM, and other features. It's about the size of a Nano. It costs more ($20), but may have what you need. Details about half way down the page at:
I have an interesting project, Arduino Pro Mini 3.3V + ESP8266-12E, TCP Modbus to OpenScada, MqTT to Mosquito and SiteWhere. I am re-writing the bi-directional data transfer between the Pro Mini and ESP8266 to also use TCP Modbus over the I2C interface ( ) to read/write coils and registers from the Scada. I have found that coding goes a bit faster in Amtel Studio with the Visual Micro plug-in. 90% of the code is Libraries or sample code posted on the web. All for what? To install this in my friends craft-beer factory to see how things shape up and use the cheapest devices money can buy.
And then I get stuck between strings and character buffers....