uint8_t and Spark.variable

Hi Guys,

I am trying to use Spark.variable(“variable”, &variable, INT) with a uint8_t variable, but when I go to retrieve the variable it comes back with a crazy number… when it should be 80

{
  "cmd": "VarReturn",
  "name": "max_bright",
  "result": 285277985,
  "coreInfo": {
    "last_app": "",
    "last_heard": "2015-09-16T10:36:01.886Z",
    "connected": true,
    "last_handshake_at": "2015-09-16T10:34:39.262Z",
    "deviceID": "xxx",
    "product_id": 0
  }
}
2 Likes

You could try using a regular ‘int’ type instead of ‘uint8_t’ for the published variable.

I recently added a change that will be in 0.4.6 that will produce a compiler error when trying to register variables with the wrong type. :slight_smile:

4 Likes

Could we possible get more variable types? I know it works with int but I was trying to use uint8_t because I am only storing a value that is between 0 and 255, so saves on memory and EEPROM etc

You’ll save 3 bytes of RAM but probably cost you a lot more in increased flash use with the additional code, so I’d suggest sticking with a few key variable types. :smile:

Just to add to @mdma’s answer.
You won’t actually save any RAM by doing something like this

uint8_t var1;
uint8_t var2;
uint8_t var3;

compared to using int, since on a 32bit system all variables are (by default) placed on 4-byte boundaries.
You can force them to be packed but this would call for extra machine code to be added when referencing such variables, causing some loss of flash space and paying a performance penalty too,

2 Likes