Serial.print makes code work?!

Hey guys

I'm having a bit of trouble with some code I'm writing with a friend.
The trouble is that the code only works as we'd like it to if the Serial.print statement as indicated in the example is present. It won't correctly work without it

long unsigned int number=0,converted_number=0;
const int shift[24] = {7,15,23,6,14,22,5,13,21,4,12,20,3,11,19,2,10,18,1,9,17,0,8,16};


void setup() {
 Serial.begin(9600);               
}


void loop() {
 number=0xDAF2B6;
 //Serial.println(number,HEX);              // CODE ONLY WORKS WITH THIS SERIAL.PRINT
 converted_number = convert(number);      
 Serial.println(converted_number,HEX);    // right answer would be 0x38D7F3
 delay(1000);
}


unsigned long int convert(unsigned long int someBINnumber){
  unsigned long int convertedBINnumber;
  unsigned long int tempnumber;
  for(int i=0;i<24;i++){
    tempnumber = (unsigned long int) (someBINnumber >> (23-i));//get first digit at base 2^(23), then 2^(22) etc.
    someBINnumber -= (unsigned long int) (tempnumber << (23-i));//remove this digit from original number
    convertedBINnumber += (unsigned long int) (tempnumber << shift[i]);//add this digit to the new number with new base
    tempnumber = 0;
    }
  return convertedBINnumber;
}

The idea of the convert-function is to shift around the bits in a certain way (the shift-array holds the positions the bits get in the convert step). The whole reason for this piece of code is a badly wired led-driver shift-register with 24 bits -> we need a different bit order than we originally thought.
We know that the convert function usually does its job - this is one case where it only does with this Serial.print statement.

We'd be very grateful for some help, we've been working on this for hours and hours without results.
best regards,
wiseman

Works for me with or without the print statement on Nano and Arduino 1.6.13

Pete

What happens if you don't try to force HEX for the first println() ?

Ahhh. I didn't see the compiler warning (the IDE doesn't summarize number of errors and warnings at the end).

C:\Users\Peter\Documents\Arduino\Blink\Blink.ino:26:71: warning: 'convertedBINnumber' may be used uninitialized in this function [-Wmaybe-uninitialized]

     convertedBINnumber += (unsigned long int) (tempnumber << shift[i]);//add this digit to the new number with new base

You need to initialize it:

  unsigned long int convertedBINnumber = 0;

Pete

omg
I don't even ... thank you, thank you, thank you

why the hell would it work with the print statement? Anyway, that'll teach me checking for compiler errors, I thought there was something flashing red but didn't think of checking - I feel rather stupid now :confused:

thanks again, I didn't dare to hope it'd be so simple :slight_smile:

I'm honestly just glad it does, those kinds of error drive me nuts - works when you try to check why it doesn't - ARG! :wink:

maybe we can implement an older version of our code after all - that would save us quite a bit of time