-1

I am reading a sensor and obtaining values for change in temperature every 10 minutes. At the moment I am saving the change in temperature as an integer.

The range in temperature change should be between -120 and 120, I want to save the temperature change to EEPROM, but I only have 512 bytes spare and as an integer the values take up 2 bytes. Therefore I thought I could assign the value to the corresponding char value and save the char to EEPROM (since this will only take one byte) e.g. (e.g. '4' 's' '$' etc.), however I can't see the easy way to do this.

I am using the arduino IDE which is C++ I believe and asking here because it's really a software question

I thought I should be able to use something like

int tempAsInt = -50;
char tempAsChar;

tempAsChar = char(tempAsInt);

or

int tempAsInt = -50;
signed char tempAsChar;
tempAsChar = tempAsInt;

but the first one printed the same characters (upsidedown question mark or null value) for varied tempAsInt values

and the second one just printed out the same value as the integer, i.e. if the change was -50, it printed -50, so I'm not sure if it is really a char, though perhaps I'm just printing it wrong.

My printing code is

mySerial.print("\tTempAsInt: "); 
mySerial.print(tempAsInt); 
mySerial.print("\tTempDiffAsInt: "); 
mySerial.print(tempDiffAsInt); 
mySerial.print("\tTempDiffAsChar: "); 
mySerial.print(tempDiffAsChar);
Shara
  • 121
  • 2
  • 10
  • p.s. I couldn't find an exact duplicate question yet, but if you find one please feel free to direct me to it. – Shara Dec 11 '17 at 04:24
  • what did you expect to see when you printed the char `-50` ? – M.M Dec 11 '17 at 04:41
  • Printing `char` has "special behaviour". Rather than printing out the numeric value, you get the character mapped to that value because that's what virtually everyone wants to see when they print a `char`. To see the value stored, you're going to have to cast it back to an integer. – user4581301 Dec 11 '17 at 05:08
  • @ M.M, whatever the corresponding unsigned char value would be for the integer value of -50, I was just trying things out, so didn't really know. I found tables which assign the decimal values of 1 to 255 to ASCII values, e.g. 38 -> '&', 50->'2' so I thought perhaps there was a similar one for signed char, but obviously my fundamental understanding of how it works is wrong. – Shara Dec 11 '17 at 05:14
  • @user4581301, but for some reason my results did print the numeric value, wheras I actually wanted to see the char value, just to make sure it was a char. Perhaps this is because it was a signed char? I used print(tempAsChar); and it printed '-50'...... oh, just realised I need to edit my question, as in the second code example, I tried using a signed char however my code just says char. – Shara Dec 11 '17 at 05:17
  • Afraid I misread your question then. Print a `char` and you shouled get the character representation. Could you add the code you use to print? – user4581301 Dec 11 '17 at 05:21
  • @user4581301 ok, thanks! my exact arduino code was: mySerial.print("\tTempAsInt: "); mySerial.print(tempAsInt); mySerial.print("\tTempDiffAsInt: "); mySerial.print(tempDiffAsInt); mySerial.print("\tTempDiffAsChar: "); mySerial.print(tempDiffAsChar); Sorry, should all be on separate lines but not sure how to do this. – Shara Dec 11 '17 at 05:25
  • 1
    I'm not familiar enough with the mechanics of arduino's `print` routine, but the `unsigned char` is probably going to be the culprit. Here is a question where the asker is looking for the exact opposite behaviour, they want the value, and to get it they had to print an unsigned character type: [How to print in Arduino a char variable as a number to the Serial Port?](https://stackoverflow.com/questions/34133509/how-to-print-in-arduino-a-char-variable-as-a-number-to-the-serial-port) – user4581301 Dec 11 '17 at 05:34
  • thanks!! I was using the characters just to make sure it was saving as a char, (and not somehow converting back to an int, or doing something else strange). However, (so long as they only take one byte) in the end I do want numbers, so I think, if this is the case, in the end I will probably use the unsigned char type. Very useful to know. – Shara Dec 11 '17 at 06:20
  • I'm not sure why people are downvoting my question, I guess it wasn't a tough problem but I have done this now and it works great for saving space in the EEPROM. – Shara Dec 27 '17 at 08:43

1 Answers1

1

In C and C++, there are several ways to cast an object to a different type. Refer http://www.cplusplus.com/doc/tutorial/typecasting/ for a summary.

Without the complete code, it is tough to say for sure. However, your problem seems to be caused by using cout to check the values of the variables like:

cout<<tempAsInt<<endl<<tempAsChar;

cout interprets the tempAsChar variable as a character type and prints the value as per the encoding.

Since -50 is outside the printable range of the ASCII code (refer http://www.asciitable.com/) on your system, you will see some value which is not really a representation of tempAsChar, but a filler such as a question mark for an unprintable.

You can confirm the above behavior by setting tempAsChar to a value of , say, 50 to see the character '2'.

To verify that tempAsChar indeed has the correct value, use printf instead:

printf("tempAsChar (int)= %d and tempAsChar (char)= %c",tempAsChar,tempAsChar);

You should see the output: tempAsChar (int)= 50 and tempAsChar (char)= 2

Karthick
  • 2,031
  • 1
  • 13
  • 18
  • Thanks very much, I've added 128 to all my values to get a range between 0 and 255 and now using char only (rather than unsigned char). I cast the values from integer to char using tempDiffAsChar = (char)tempDiffAsInt . Now when I print the char values they match the ASCII table for value up to 128, for values above 128 I get a back to front question mark, I suspect this is just because the arduino IDE isn't set up to print these values, though I'm not sure, anyway so long as they are being saved it doesn't really matter. Will try with the EEPROM and see how it goes. – Shara Dec 11 '17 at 06:14
  • To ensure they are being saved, use the printf line mentioned above. Check the tempAsChar printed as int. – Karthick Dec 11 '17 at 06:24