Hi Forum,
Please help me overcome this trivial issue with variables. I'm trying to use preferences.h for the first time to store variables. I'm using a Pelco protocol and sending commands works fine but this includes 0x01, 0x02, etc. I'm struggling storing that value then retrieving it and sending it again.
Working string to send to device = Camera1.send_command(SET_PRESET, 0x02);
Storing value = preferences.putString("storedPreset", 0x02);
Retrive and send = storedPreset = preferences.getString("storedPreset"); Camera1.send_command(GOTO_PRESET, storedPreset);
The final storedPreset should hold 0x01 or 0x02, to 0x0A (for 10).
In theory I could use 1, 2, 3, etc and send as binary/hex at the last stage but it's not clear for me. Perhaps I need another stage to say new variable = 0x+storedPreset or something.
"0x0A" or "10" are just other ways to present the binary (byte) value 00001010 used by the MCU. Just save 0x0A as a byte or as an int to preferences.
If you want to see it as 0x0A, then print it in HEX.
That's only for you though. The MCU always works in binary.
Leo..
That is a statement, not a string (a term coming up shortly). The statement comprises a single function call. It's unclear what kind of value SET_PRESET is, but the second argument 0x02 is number.
Note that just 2 also works. In your program code, using hexadecimal or decimal or anything else when used as a number is entirely up to you and what is better in a given situation. For 1 to 10, just use decimal.
Because the value is a number, you don't use putString. That's for strings. In this case, you're sort of attempting to store whatever c-string is at address 2, which could be anything of any size. It should not compile successfully with default settings.
For these small numbers, use putUChar instead, as suggested. The range is 0 to 255.
Thank you all for your replies, this was very helpful. I had tried numerous types (Byte, Char, etc) and the ESP32 kept rebooting. I had also tried sending bytes instead of the binary command which no longer operated the device. Knowing UChar was the best option, making sure I was consistent throughout the code and using bytes instead the binary command worked perfectly. I found the rebooting was a serial printf command during startup I was using for information, once I had ruled that out everything does what it should.
Thank you.