The following code has 33 floats that I want to fill with data coming in via serial messages from 11 remote units. The serial messages will have a "pointer" byte that is the offset from the first variable (in this case it would be "T_mainin"), eg, the variable "T_OAT" would have an offset of 4. However, when I try to print out what the pointers are, no matter which variables I pick, the LCD always prints:
521
517
513
509
The integers change when I try other lines in the code, but they are always 4 digits apart (which I expect for floats). I don't understand why they aren't other multiples of 4 when I pick variables that are not adjacent in the declarations.
// "01234567890123456789"
char this_file[] = "pointer_anal" ;
char descp[] = "pointers-variables" ;
char ver[] = "ver1 2/15/21 w/44780" ;
// Load Libraries for 4x20 LCD =======================
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip
int status ;
// LCD geometry
const int LCD_COLS = 20;
const int LCD_ROWS = 4;
float T_mainin ;
float T_mainout ;
float T_bsmtin ;
float T_bsmtout ;
float T_OAT ;
float T_BFlr ;
float T_UFlr ;
float T_Foot ;
float T_Art ;
float HB_set ;
float T_Lab ;
float T_Wine ;
float T_Dirt ;
float T_Ebed ;
float T_Wbed ;
float T_Gym ;
float T_Gymlo ;
float T_Gymhi ;
float T_Fplace ;
float T_Main2 ;
float T_Mainhi ;
float T_Cool1 ;
float T_Cool2 ;
float CG_Set ;
float HM_Set ;
float T_Main ;
float T_MBR ;
float CB_Set ;
float T_OAT2 ;
float T_Car ;
float T_Studio ;
float T_Paint ;
float T_Painthi ;
float T_Shop ;
float T_Shophi ;
float T_OAT3 ;
float T_Plants ;
float keys1 ;
float keys2 ;
float keys3 ;
float *p ; // pointer
int ptr ;
// ------------------------------------------------------------------------
// ---------------- $$$$$$$$$$$$$$$$$$$ ----------------
void setup() { // ---------------- Setup() --------------------------------
pinMode(13,OUTPUT) ; // LED
Serial.begin(9600); // reserved for laptop comm link
// ----------------version message ------------------------
Serial.println() ; //line feed
Serial.println(this_file) ; //file name
Serial.println(descp) ; //description
Serial.println(ver) ; //version and date
// ------------ end of version message --------------------
status = lcd.begin(LCD_COLS, LCD_ROWS);
// -------------- Show version on LCD ----------
lcd.clear() ;
lcd.home() ;
lcd.print(this_file) ;
lcd.setCursor(0,1) ;
lcd.print(descp) ;
lcd.setCursor(0,2) ;
lcd.print(ver) ;
lcd.setCursor(0,3) ;
lcd.print("----- LCD Test -----") ;
delay(2000) ;
lcd.clear() ;
lcd.home() ;
T_mainin = 0 ;
T_mainout = 0 ;
T_bsmtin = 0 ;
T_bsmtout = 0 ;
T_OAT = 0 ;
T_BFlr = 0 ;
T_UFlr = 0 ;
T_Foot = 0 ;
T_Art = 0 ;
HB_set = 0 ;
T_Lab = 0 ;
T_Wine = 0 ;
T_Dirt = 0 ;
T_Ebed = 0 ;
T_Wbed = 0 ;
T_Gym = 0 ;
T_Gymlo = 0 ;
T_Gymhi = 0 ;
T_Fplace = 0 ;
T_Main2 = 0 ;
T_Mainhi = 0 ;
T_Cool1 = 0 ;
T_Cool2 = 0 ;
CG_Set = 0 ;
HM_Set = 0 ;
T_Main = 0 ;
T_MBR = 0 ;
CB_Set = 0 ;
T_OAT2 = 0 ;
T_Car = 0 ;
T_Studio = 0 ;
T_Paint = 0 ;
T_Painthi = 0 ;
T_Shop = 0 ;
T_Shophi = 0 ;
T_OAT3 = 0 ;
T_Plants = 0 ;
}
// ========================== end of setup =============================
// =================== Loop ===========================================
void loop() {
lcd.home() ;
lcd.clear() ;
p = &T_Art ;
ptr = (int)p ;
lcd.print(ptr) ; // this is line 141
lcd.setCursor(0,1) ;
p = (int)&T_Ebed ;
ptr = (int)p ;
lcd.print(ptr) ;
lcd.setCursor(0,2) ;
p = (int)&T_Wbed ;
ptr = (int)p ;
lcd.print(ptr) ;
lcd.setCursor(0,3) ;
p = (int)&T_OAT3 ;
ptr = (int)p ;
lcd.print(ptr) ;
*p = 29.0 ;
lcd.print(" ") ;
lcd.print(T_OAT3) ;
p = 521 ; // 521 is the address printed by line 141
*p = 75.4 ;
lcd.print(" ") ;
lcd.print(T_Art) ;
while(1) ;
}
I'm also getting warning about converting ints to ints and floats to floats, but the code compiles and runs. I don't understand why a pointer (in this case *p) is ever a float, just because it's pointing at a float. Seems like it would be an integer.