Program out of control?

Hi

New to Arduino, C, and last programmed in anger over 20 years ago, so please bear with me.

I am trying to develop an engine monitoring system, (engine hours, oil pressue, water temperature, fuel level, battery voltage, etc. with alarms) but finding the programming environment a little challenging.

I have been having a quirky problem and thought it was the 'for' loop going out of control - as one did before, but it is evidently the assignment of the zero to the last character in charbuff. If you comment out that statement, the loop works ok: As it is, it compiles, but jumps out of loop() back into setup()!

#include <SoftwareSerial.h>;

#define TRACE1
#ifdef TRACE1
#define trace1(s) Serial.print(s)
#define traceln1(s) Serial.println(s)
#else
#define trace1(s)
#define traceln1(s)
#endif

char charbuff[6];

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

traceln1("Starting");

}

void loop() {
// put your main code here, to run repeatedly:
traceln1("here ok");

for (int k = 1; k < 6; k ++ ){ //I am simply trying to initialise a buffer to spaces, prior to putting an integer converted to ascii - ultimately to output to LCD
charbuff[k] = 32;
trace1 (k); traceln1(" In k-loop");
delay(1000);
}
charbuff[6] = 0; //comment this out and it executes ok
trace1(charbuff); traceln1(" On Exit k");
// delay(2000);
}

I am probably misunderstanding the nature of char as opposed to char * as I have not got my head around what you can and cannot do.

In fact I am ludicrously spending hours trying to format a message including hours, minutes to log events, so need to get an integer into ascii an ascii string, and concatenate elements for output to LCD and to the monitor, possiblly a web page later. It can't be this hard, surely?

Anyoone who has time to mentor me with my silly questions without being too rude in the process would be most welcome.

Arrays in C index from zero, not one

(Which means that there is no "charbuff[6]"

so need to get an integer into ascii an ascii string, and concatenate elements for output to LCD

Why are you converting them to ASCII and concatenating them ? Just print them individually to the LCD and/or the Serial monitor. To make the code neater put the commands in a function and call it when required.

If you do need to convert the integers to ASCII you can use the itoa() function for each of them or do it all in one go using the sprintf() (or snprintf()) function but either of them takes more memory than itoa().

TolpuddleSartre:
Arrays in C index from zero, not one

(Which means that there is no "charbuff[6]"

Yes it does exist, but it's being used for something else. The return address of the function, probably, which is why it jumps to address zero and re-starts.

This kind of problem is more obvious if you use a named constant for the size of the array. It's also easier to fix because you don't have to search for every example of '6' in your code.

Thanks for the input..

I'll look up itoa - there have to be some useful routines in the language, but it's hard to see the whole picture via the net, not quite like having a 600 page programmng reference in front of you..

Take a look at http://www.cplusplus.com/, particularly the reference section