Skip to content

Instantly share code, notes, and snippets.

@Weathergadget
Created May 22, 2016 02:23
Show Gist options
  • Save Weathergadget/f508919f2c04cb595c3402a30d767424 to your computer and use it in GitHub Desktop.
Save Weathergadget/f508919f2c04cb595c3402a30d767424 to your computer and use it in GitHub Desktop.
Float_Disassemble_Reassemble
/*
*
* Sketch to show how to get the single bytes out of a float variable and resemble them to a new variable with the original Value.
* Can be used e. g. for the transfer via SPI, where only one byte at a time can be sent
*
* Robert Mollik 2016
*
* Created in the content of the development of my weather station and the need to tranfser values via SPI.
*
*/
float temperature = -34.3; // Original variable with example value
// Single bytes of the float variable. Can be a local variable. However I want to send the values in an interrupt.
// For that reason, I want to avoid unnecessary calculations in the interrupt and instead calulate it in the main loop,
// so that they can be assessed in the interrup.
byte temp_0 = 0;
byte temp_1 = 0;
byte temp_2 = 0;
byte temp_3 = 0;
void setup() {
Serial.begin(115200); // start serial monitor
}
void loop() {
// Create pointer variables for temperature
// Cast pointer variables into byte
byte * tempPTR = (byte*)&temperature;
// Read all 4 bytes of the floats and write them into the transfer variable (byte)
temp_0 = tempPTR[0]; // temp_0 = *(tempPTR);
temp_1 = tempPTR[1]; // temp_1 = *(tempPTR+1);
temp_2 = tempPTR[2]; // temp_2 = *(tempPTR+2);
temp_3 = tempPTR[3]; // temp_3 = *(tempPTR+3);
// Create new variables for the temperature
float newTemp = 0;
// Create pointer variables for the new temperature
// Cast pointer variables into byte
byte * newTempPTR = (byte*)&newTemp;
// write transfered bytes into the position of the new float variables
newTempPTR[0]=temp_0;
newTempPTR[1]=temp_1;
newTempPTR[2]=temp_2;
newTempPTR[3]=temp_3;
// Display the results via the serial monitor:
Serial.print("Temperature: ");
Serial.print(temperature,DEC);
Serial.println();
Serial.print("temp_0: ");
Serial.print(temp_0,BIN);
Serial.print(" || ");
Serial.print("temp_1: ");
Serial.print(temp_1,BIN);
Serial.print(" || ");
Serial.print("temp_2: ");
Serial.print(temp_2,BIN);
Serial.print(" || ");
Serial.print("temp_3: ");
Serial.print(temp_3,BIN);
Serial.println();
Serial.print("temp_0: ");
Serial.print(temp_0,DEC);
Serial.print(" || ");
Serial.print("temp_1: ");
Serial.print(temp_1,DEC);
Serial.print(" || ");
Serial.print("temp_2: ");
Serial.print(temp_2,DEC);
Serial.print(" || ");
Serial.print("temp_3: ");
Serial.print(temp_3,DEC);
Serial.println();
Serial.print("tempPTR: ");
Serial.print((uint16_t) tempPTR,HEX);
Serial.print(" || ");
Serial.print("newTempPTR: ");
Serial.print((uint16_t) newTempPTR,HEX);
Serial.println();
Serial.print("New Temperature: ");
Serial.print(newTemp,DEC);
Serial.println();
Serial.println();
Serial.println("#############################################################################");
Serial.println();
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment