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