I was expecting variables chr and test to have the same results? I see that EEPROM.read results a uint, however from web searching it appears that they're supposed to be interchangeable or can be cast by typing the variable, however the results below indicate that is incorrect.
How can I test a single char against isAlphaNumeric/isAlpha/isDigit after reading it from the eeprom?
#include <EEPROM.h>
void setup()
{
Serial.begin(9600);
while (!Serial)
{
; // Wait for serial to connect
}
delay(1000);
Serial.println("");
Serial.println("");
int valToWrite = 3;
int valAddress = 700;
EEPROM.write(valAddress, valToWrite);
char chr = EEPROM.read(valAddress);
Serial.println(isAlphaNumeric(chr)); // false
Serial.println(isAlpha(chr)); // false
Serial.println(isDigit(chr)); // false
Serial.println(isalnum(chr)); // false
Serial.println("");
char test = '3';
Serial.println(isAlphaNumeric(test)); // true
Serial.println(isAlpha(test)); // false
Serial.println(isDigit(test)); // true
Serial.println(isalnum(test)); // some weird numeric response
}
void loop()
{
// put your main code here, to run repeatedly:
}
Would you also say that if I have to write an int to the EEProm, I should convert it to a char or string first before writing it? This way when I read it back at a later time and test it with isAlphanumeric etc, it will interface properly with the functions?
Do you want to store ASCII characters or an actual integer (number) to EEPROM?
The isAlphaNumeric(), isAlpha(), isDigit(), and isalnum() functions are only used for ASCII characters, not numeric values such as an int. The ASCII value for (chr)3 is the control character ETX (end of text), which is correctly not an ASCII letter (Alpha) and not an ASCII number (Numeric or Digit).
Also note that EEPROM.write() writes a single byte of data to EEPROM, an int is at least two bytes, or more, depending on which board you are using, so you are only going to be storing the lower eight bits of the int. The easiest way to write an int to EEPROM is to use EEPROM.put(), along with EEPROM.get() to read it back.
I'd like to store both dependent on the situation, I can try eeprom put/get instead after I look at some examples and read the documentation. I was using eeprom read/write for characters, strings, floats, dates, times in a previous project, so I'm not sure how it wasn't truncating the bits there?
I have a read and write function that was used successfully on a previous project. It wrote pretty much all data types except for ints and when I ported the functions to this project, nothing was working. The way the functions work is that they read a memory address and each consecutive address after that until it finds '\0' and then returns the read value. Simplified, the alphanumeric part would evaluate to determine if the loop should continue reading.
At this point it looks like I'm going to have to remove those functions and start over from scratch. I'd like to have a read function that I can pass an address to and read a value without knowing the total number of bytes and I'd like to have a write function that I can pass the name of the setting I want to write to and a char pointer for the value and have it write the value, no matter the data type.
Are there example functions that do this already? (similar to Robin2's serial read) or is there a design flaw in trying to read/write all data types from a single read and a single write function?
Ok, can I have a single function or do I need to have an overload for each data type that I want to use with EEPROM.put? I just tried using a char* for different data types and I could not get that to work.
For validating my own understanding, 31456 would be two bytes encompassing 111101011100000, with the first byte being 01111010 and the second being 11100000, is that correct?
Reading about templates and having no experience with using them, is the top code set (untested) something that could replace the bottom code set(untested)? If so, I'll start working on and trying to form an EEPROM.get template example.
I think OP should start from the very beginning and learn about different data types - bit, bytes, chars, int8/16/32, string and String and arrays. Without knowing the difference he has a little chances to use an EEPROM properly.
If I understood your previous post, you said that in order to achieve passing different parameter types to a function/functions that wrote to an eeprom, I could use overloads or a template and that the template would make the function type agnostic and be easier. I'm not sure what indicates that I don't understand a decimal value (0 to 255 for 2^8 bit architecture) and it's corresponding ascii representation. The only ones I have psuedo memorized are \n and \r which are 10 and 13 I believe. I did not know that isDigit and isAlphaNumeric only evaluated ascii, that's something I did learn.
As I want to have EEPROM put/read inside of functions, I have to pass parameters to those functions, which is why I asked about overloads. I need to pass parameters that I understand to functions, how they are stored in memory doesn't really matter to me as it's all a series of ones and zeros in the end and the eeprom code will convert that back and forth for me.
I gave code examples of what I think overload functions would look like in C and what I think a replacement template function would look like. If they're wrong, a simplistic example would be appreciated.
At this point I am trying to write what I thought I had previously, an eeprom function I can port from project to project and then modify from there if needed. I'd like to be able to save the basic data types, int, float, string, char and arrays and for the function to be flexible enough to do other tasks such as checking the format of the data and ensure (as an example) that a float contains a decimal point etc. That's the big picture in a nut shell. In this project I believe I will only be saving integer values, however the flexibility of the function to save the basic data types is what I'm after to start. Previously, my loop function was getting large and disorganized.
I think building off of this simplistic example will be the best place to start at. I've read of people saving all of the data at once as a struct, but I'm not there yet. That may be the next step after I get this functional. Currently I have the data organized as the second section of code in this post and I iterate through it with a for loop so I can pass a friendly name and have a function lookup the ee address from that. I'll acknowledge this probably is not the ideal way to handle this, however coming from a managed code language this is the way that makes the most sense to me.
I'm using an app that sends user input data over blue tooth to the Arduino, using text boxes. I don't have the ability to validate the entries on the front end, so my only option was to validate it (if at all) in the Arduino's code.
I did not mention this in my previous reply because I did not want it to come across as argumentative/defensive, however that was why I had to ensure that if I was expecting a float variable as input, then the ascii data being sent over blue tooth contained a decimal point for a float, or slashes for a date etc. I may not need that for this project and if I do not, it'll be removed.