The compiler or FHT library magical problem

Hi,

I am using the Open Music Labs FHT library in my recent (and first Arduino) project and I am stuck on the function below. The problem is that as soon as I call fht_mag_lin(), it changes the value of FHTnum variable. Actually adds 512 to it. Also, if I put Serial.print (FHTnum) right before calling the doFHT(), everything works fine.

Thanks for help,
Pavel.

void calculateAverageFHT (){
#define FHT_GRAPH_POINTSPERCHAR 45.0
unsigned int n = 0;
unsigned int pointer = (bufferPointer + 1) % BUFFER_LENGTH;

#ifdef TRACE_INPUT_BUFFER
showBuffer ();
#endif

for (unsigned int FHTnum = 1; FHTnum <= FHTS; ++FHTnum)
{
for (unsigned int curSample = 0; curSample < FHT_N; ++curSample)
{
fht_input[curSample] = inputBuffer[pointer] * 32;
pointer = (pointer + 1) % BUFFER_LENGTH;
}

#ifdef TRACE_FHT_SWITCH
Serial.println (" ");
Serial.println ("*** FHT *********************************************************************************************");
Serial.print (FHTnum);
Serial.print (" / ");
Serial.println (FHTS);
#endif
// Serial.print (FHTnum);
doFHT ();
for (unsigned int curBin = 2; curBin < FHT_N/2; ++curBin)
{
if (FHTnum == 1) {
averageFHT[curBin] = fht_lin_out [curBin];
}
else if (FHTnum == FHTS) {
averageFHT[curBin] = (averageFHT[curBin] + fht_lin_out [curBin]) / FHTS;
}
else {
averageFHT[curBin] = (averageFHT[curBin] + fht_lin_out [curBin]);
}

#ifdef TRACE_FHT_SWITCH
Serial.print (curBin);
if (curBin < 10) Serial.print (": ");
else Serial.print (": ");
n = fht_lin_out [curBin] / FHT_GRAPH_POINTSPERCHAR;
for (unsigned int j = 1; j < n; ++j)
{
Serial.print ("‒");
}
Serial.print ("● ");
Serial.println (fht_lin_out [curBin]);
#endif
}
}

void doFHT () {
fht_window(); // window the data for better frequency response
fht_reorder(); // reorder the data before doing the fht
fht_run(); // process the data in the fht
fht_mag_lin(); // take the output of the fht
}

... also, the Serial.print shows FHTnum content 513 even when I define it as a byte and when I define it as unsigned long, there are no problems. It could be a solution but quite bad as I really don't know what's happening.

Thanks, Pavel.

Post ALL the code, using code tags. See the "How to use this forum" post.

You may be exceeding an array bound somewhere.

Hi and thanks for the reply.

The code is quite big to ask anyone to go trought it . I thought someone might encounter similar weird behaviour and give me some pointers. The arrays are the first thing I went after, everything looks clear.

But if the Serial.print gives 512 for a byte variable, it looks to me that the problem must be in the compiler. It should not print two bytes value for one byte variable.