Storing a Char on internal EEPROM

Currently I am using an Arduino Uno rev3 to control an LED strip (WS2812B) from a bluetooth module. The commands sent to the bluetooth module are sent in Char format as I found these more stable than using int format.

When the Char is received (W, X, Y, Z etc), the led strip changes to a new display sequence. When I turn the Arduino on the code obviously starts for the first time - I want to be able to store the last selected sequence at power up, so need to store the Char in an EEPROM so I can read it to get the last Char that was written to it. I was reading this post here (http://www.gammon.com.au/forum/?id=10896) and it has an example on how to do this by using an 24LC256 chip. As the Arduino has a built in EEPROM this should also be possible without needing to use that extra chip, but I can't see an example of Storing a Char in the example - only an int.

Is this possible, or will I need to use an 24LC256 to do this? Also is there any example code available on how to do this? Thanks.

Have a read through the reference material: https://www.arduino.cc/en/Reference/EEPROM

A char is only one byte, you can see it's value by looking at an ASCII table.

mrdude: but I can't see an example of Storing a Char in the example - only an int.

The EEPROM library only handles bytes. A char is a byte. Look at the link Sam gave you.

Thanks, so does that mean that the letter w would be: 119 also if the letter was Uppercase would it still be the same? Thanks

mrdude: Thanks, so does that mean that the letter w would be: 119 also if the letter was Uppercase would it still be the same? Thanks

There's no difference between char and byte. Both store numbers. In the case of a lowercase w, they would both store 119.

OK thank you for the fast replies, I will have a bash at doing this in a little while, I was reading the links posted (examples), and it looks fairly straight forward.

I read that EEPROM can only be written to 100,000 times - so is this for each address? for example say you write to address 0x0 and then after 100,000 times it becomes corrupted, can you then write to address 0x1 instead - or do you need an entire new atmega?

[quote author=James C4S date=1488681093 link=msg=3160653] The EEPROM library only handles bytes. A char is a byte. Look at the link Sam gave you. [/quote] EEPROM library can handle any type via EEPROM.get() and EEPROM.put().

mrdude: Thanks, so does that mean that the letter w would be: 119 also if the letter was Uppercase would it still be the same? Thanks

If you're asking whether the ASCII value of W is also 119, no it's 87. But you don't need to think about that:

#include "EEPROM.h"
void setup() {
  Serial.begin(9600);
  EEPROM.update(0, 'W');
  Serial.print("char stored at EEPROM address 0: ");
  Serial.write(EEPROM.read(0));
}

void loop() {}

The above code stores and reads the char with no mention of the number 87.

mrdude: I read that EEPROM can only be written to 100,000 times - so is this for each address? for example say you write to address 0x0 and then after 100,000 times it becomes corrupted, can you then write to address 0x1 instead

Correct, each EEPROM address is rated for 100K writes. It's possible to exceed that very quickly if you have a bug in your code so you have to be a bit careful. You can avoid unnecessary writes by using EEPROM.update() instead of EEPROM.write() as it only writes if the value has changed. EEPROM.put() uses the update behavior. If your device will require more than 100K EEPROM writes over the product lifetime you can use wear leveling code to spread the writes out across multiple EEPROM addresses.

Thanks for all the info guys, I came up with this example to read/write/update the data to the EEPROM if someone else needs an example. I’m not an expert coder - just a novice so I’m sure there must be a better way, but at least it shows how to write to an address on the EEPROM, and then update that if the value changes, finally converts the byte read to a char.

// EEPROM update example by MrDude - more info here:
// https://forum.arduino.cc/index.php?topic=460185.0
// https://www.arduino.cc/en/Reference/EEPROM

#include <EEPROM.h>

int address = 0; // address 0x0 on the EEPROM
byte value;
#define baudrate 115200 //set the baudrate the same as the comport settings on the PC

void setup()
{
Serial.begin(baudrate);
delay(1000); //wait 1 second
}

void loop() {
  value = EEPROM.read(address); //read the EEPROM at the address 0x0
  Serial.println(value); //print the byte obtained from the EEPROM at address 0x0
  delay(3000); // wait 3 seconds
  //updatebyte(); // call our updatebyte function to write a new value if something has changed - or write a new value
  showchar(); // call our showchar function to show the read byte as an ASCII character
  }

void updatebyte() {
  char data = 'w';
  EEPROM.update(address, data); // updates the value at address 0x0 if the byte is not equal to 119 (w)
}

void showchar() {
  char newval = (value); // convert 119 to w
  Serial.println(newval);
  delay(3000); //wait 3 seconds before the main void is run again
}

mrdude: void showchar() {  char newval = (value); // convert 119 to w  Serial.println(newval);

Here's another way to do this:

void showchar() {
  Serial.println((char)value);  // print value as a char

This is called type-casting. Your way works fine, and is perhaps a bit easier to understand.

mrdude: int address = 0; // address 0x0 on the EEPROM

I recommend using the const keyword for anything like this:

const int address = 0; // address 0x0 on the EEPROM

You never want address to change so by making it const you will get a helpful compiler error if you accidentally do anything in your code that would change it.

Thanks for the replies and tips, you have certainly given me some very useful info and something to think about. I couldn't see a thanks button, so I have added karma to you.

Cheers.

if your eeprom project of pixle led is completed send the code of that i have also problem for storing the message

My project is not completed yet

eeprom.write(6, 'w' ); // stores it char c = eeprom.read(6); // reads it back