Heyy,
I'm currently working on a project where the importance of stored values. Here I have some questions about, and I hope someone can help me with this.
The idea is that something happened which dates to be stored. If the power has been off, these values collected in the void setup (). With the values, there is then on again counted.
- EEPROM:
This code I made:
/*
door de knop in te duwen wordt, wordt er geteld.
het tellen wordt opgeslagen in het memory (eeprom).
PAS OP!!! er kan maar 100.000 keer over deze data heen geschreven.
*/
# define button 2
int buttunState;
int currentState = 0;
int previousState = 0;
long counter = 0;
EEMEM uint16_t addr; //adres waar de waarde naar toe wordt gestuurd
int val = 0;
void setup () {
pinMode(button, INPUT);
Serial.begin(9600);
int val = eeprom_read_word(& addr); //EEPROM wordt uitgelezen in val
Serial.print("EEPROM counter: "); //het wordt "EEPROM counter: " wordt gestuurd
Serial.println(val); //val wordt geprint met de data van de EEPROM
}
void loop () {
buttunState = digitalRead(button);
if (buttunState == HIGH) {
currentState = 1;
}
else {
currentState = 0;
}
if (currentState != previousState) {
if (currentState == 1) {
counter = eeprom_read_word(& addr) + 1;
Serial.print("Echte counter: \t");
Serial.println(counter);
eeprom_write_word(& addr, counter); //de counter wordt weg geschreven in de EEPROM
}
}
previousState = currentState;
delay(50);
}
This works, but i doesn't feel right. Feedback is welcome.
- SD
I tried to save the data on a SD. Saving was not a problem, but it does not manage to read this value and use. Does anyone have a small example program manages?
EDIT:
the part of the project what i was working on was mor focust on the EEPROM. the SD card is for a
another time. 
EEMEM uint16_t addr; //adres waar de waarde naar toe wordt gestuurd
?
I copied parts of this program:
#include <avr/eeprom.h>
int myValuePos = 12345;//int in sram
int myValueNeg = -12345;
EEMEM uint16_t saveMyValuePos; //integer in eeprom; avr will control address
EEMEM uint16_t saveMyValueNeg;
void setup()
{
Serial.begin(9600);
eeprom_write_word( & saveMyValuePos , myValuePos);
eeprom_write_word( & saveMyValueNeg , myValueNeg);
int valPos =eeprom_read_word(& saveMyValuePos);
int valNeg =eeprom_read_word(& saveMyValueNeg);
Serial.println(valPos);
Serial.println(valNeg);
}
void loop()
{
}
my source is this site/forum: EEPROM anything working example? - Programming Questions - Arduino Forum
UKHeliBob:
But not this part
#include <avr/eeprom.h>
that's true. previously i had it in, but did not see any difference.
Am still new in this world, so try and look what happens...
Hey,
I think I just use the EEPROM like i do now, feedback is still welcome.
I have still a SD card problem. Below I have posted a program that I have so far. I am not able to use a number.
For example:
I have 12 times pushed a button and then the power goes off. Every time when pressing the button changes the value on the SD card. If the power is going up again and the new value button is pushed again is 13.
The code that doesn't do that:
#include <SPI.h> //bieb voor SD
#include <SD.h> //bieb voor SD
File myFile; //
#define button 2
const int chipSelect = 53; //waar de chip heen schrijft. bij een arduino uno is dit pin 10, MEGA is dit pin53
int val = 0;
int currentState = 0;
int previousState = 0;
long counter = 0;
long countertest = 0;
void setup () {
pinMode(button, INPUT);
Serial.begin(9600);
pinMode(53, OUTPUT);
if (!SD.begin(53)) {
Serial.println("initialization failed!");
return;
}
myFile = SD.open("datalog.txt");
if (myFile) {
//Serial.println("datalog.txt:");
while (myFile.available()) {
Serial.write(myFile.read());
countertest = myFile;
}
// close the file:
myFile.close();
} else {
Serial.println("error opening test.txt");
}
Serial.print("\n");
Serial.println(countertest);
}
void loop () {
counter = countertest;
val = digitalRead(button);
if (val == HIGH) {
currentState = 1;
}
else {
currentState = 0;
}
if (currentState != previousState) {
if (currentState == 1) {
counter = counter + 1;
Serial.print("real counter: ");
Serial.println(counter);
myFile = SD.open("datalog.txt", FILE_WRITE);
if (myFile) {
myFile.println(counter);
myFile.close();
// print to the serial port too:
}
else {
Serial.println("error opening datalog.txt");
}
}
previousState = currentState;
delay(50);
}
}
In response to Post5:
You've declared -
long countertest = 0;
While reading the SD, you have -
countertest = myFile;
You'll need to convert the characters to an integer; do this by calling the atoi() function.
Thanks for your feedback JMeller. I will try this.
Unfortunately i need first to fix the EEPROM. I thought that this would work. It also does well, but I have to store multiple values .....
maybe someone can give me advice.
The idea is as follows. You have two buttons. If you push a button a counter starts counting. For example, you press 3 times buttonGrey and buttonGreen 5 times. This should be updated on the EEPROM every time there is a button push. If the power was off and starts again, the counter buttonGrey starts with 3 and buttonGreen at 5.
This is the code that I have so far. There is only one key that is stored now ...... my question is how I can put the best second button.
Please help me, I am still learning this.
# define buttonGreen 2
# define buttonGrey 3
long counterGrey;
long counterGreen;
int currentStateGrey = 0;
int previousStateGrey = 0;
int currentStateGreen = 0;
int previousStateGreen = 0;
int fase = 0;
EEMEM uint16_t addr;
int val = 0;
void setup () {
Serial.begin(9600);
pinMode(buttonGreen, INPUT);
pinMode(buttonGrey, INPUT);
int val = eeprom_read_word(& addr);
Serial.print("EEPROM counter: ");
Serial.println(val);
}
void loop () {
if (digitalRead(buttonGrey) == HIGH) {
currentStateGrey = 1;
}
else {
currentStateGrey = 0;
}
if (currentStateGrey != previousStateGrey) {
if (currentStateGrey == 1) {
counterGrey = eeprom_read_word(& addr) + 1;
eeprom_write_word(& addr, counterGrey);
fase = 1;
}
}
previousStateGrey = currentStateGrey;
if (digitalRead(buttonGreen) == HIGH) {
currentStateGreen = 1;
}
else {
currentStateGreen = 0;
}
if (currentStateGreen != previousStateGreen) {
if (currentStateGreen == 1) {
counterGreen = counterGreen + 1;
fase = 1;
}
}
previousStateGreen = currentStateGreen;
if (fase == 1) {
Serial.print("\ncounter grey: \t");
Serial.println(counterGrey);
Serial.print("counter green: \t");
Serial.println(counterGreen);
fase = 0;
}
}
If you look at EEPROM.h from Arduino, you will notice it includes, avr/eeprom.h from Atmel, makers of the microcontroller in the Arduino products.
the term EEMEM comes from the avr/eeprom.h and is defined in that header file.
Arduino has taken terms like EEMEM and other cryptic jargon, such as uint16_t, and translated them into terms that are easier to understand by the average novice programmer.
If you use the cryptic jargon, be sure to include avr/eeprom.h, but if you want to consider using the Arduino EEPROM library, be sure to include EEPROM.h.
You can do this in 2 ways:
1)
#include <EEPROM.h>
- in the Arduino IDE, click on Sketch > Import Library > EEPROM
Perehama:
If you look at EEPROM.h from Arduino, you will notice it includes, avr/eeprom.h from Atmel, makers of the microcontroller in the Arduino products.
the term EEMEM comes from the avr/eeprom.h and is defined in that header file.
Arduino has taken terms like EEMEM and other cryptic jargon, such as uint16_t, and translated them into terms that are easier to understand by the average novice programmer.
If you use the cryptic jargon, be sure to include avr/eeprom.h, but if you want to consider using the Arduino EEPROM library, be sure to include EEPROM.h.
You can do this in 2 ways:
1)
#include <EEPROM.h>
- in the Arduino IDE, click on Sketch > Import Library > EEPROM
Thank you for your comment.
First I also used this library. However, there is a big downside to it. This is that there can not be stored large numbers. This is a small program which is implemented in a diffrent program and there the numbers that needs to be stored is bigger then 256.
FeelGoodGirl:
Thank you for your comment.
First I also used this library. However, there is a big downside to it. This is that there can not be stored large numbers. This is a small program which is implemented in a diffrent program and there the numbers that needs to be stored is bigger then 256.
If you need numbers larger than 256, how large?
If you:
#include <avr/eeprom.h>
you can:
eeprom_write_byte(address,value); // 0-255
or
eeprom_write_word(address,value); // 0-65535
or
eeprom_write_dword(address,value); // 0-4294967295
You can also:
eeprom_update_byte(address,value);
eeprom_update_word(address,value);
eeprom_update_dword(address,value);
eeprom_read_byte(address,value);
eeprom_read_word(address,value);
eeprom_read_dword(address,value);
You could just use the official EEPROM library?
you can easily store any object of size greater than 1 byte using its put() function...
Perehama:
If you need numbers larger than 256, how large?
i think that 4294967295 is big enoug!!
i will try to put it in the program tomorrow.
BulldogLowell:
You could just use the official EEPROM library?
I looked in this library, but didn't figured out how to do bigger numbers......
But i still have the problem that i need to store more numbers... does someone have a idea how i can do that?? I will have around 5 different numbers that need to be stored.
i need to store more numbers
Store each of them at a different address in the EEPROM but don't forget that numbers larger than a byte take more than one space in the EEPROM, so choose storage addresses that are far enough apart to prevent overlap of the stored data.
Does it meen when you use 2bytes your nuber can be 65536 (256x256)??
Again, the EEPROM library will take care of that if you choose to store an object... Example:
#include <EEPROM.h>
struct MyBigNumber{
uint32_t value[4];
};
MyBigNumber myBigNumber;
void setup()
{
Serial.begin(9600);
myBigNumber.value[0] = 4294967295UL;
myBigNumber.value[1] = 4294967294UL;
myBigNumber.value[2] = 4294967293UL;
myBigNumber.value[3] = 4294967292UL;
EEPROM.put(0, myBigNumber); // save to EEPROM location zero
// ...
//
//...
EEPROM.get(0, myBigNumber); // retrieve from EEPROM location zero
for(int i = 0; i < 4; i++)
{
Serial.println(someThing.value[i]);
}
}
void loop()
{
}
the struct has a size, so you can increment the EEPROM storage locations by using sizeof() if the case where you have multiple objects to store.
Thanks for all the help. It was not totely clear to me what exactly was meant, but I solved it as follows. These are two buttons that can be pushed. The number of presses is added and saved.
Feedback is always welcome.

#include <EEPROM.h>
# define buttonGreen 2
# define buttonGrey 3
long counterGrey;
long counterGreen;
int currentStateGrey = 0;
int previousStateGrey = 0;
int currentStateGreen = 0;
int previousStateGreen = 0;
int fase = 0;
long address = 0; //EEPROM:
//****************READ****************
long EEPROMReadlong(long address)
{
//Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
//****************WRITE****************
void EEPROMWritelong(int address, long value)
{
//Decomposition from a long to 4 bytes by using bitshift.
//One = Most significant -> Four = Least significant byte
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
//Write the 4 bytes into the eeprom memory.
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
void setup () {
Serial.begin(9600);
pinMode(buttonGreen, INPUT);
pinMode(buttonGrey, INPUT);
Serial.print("EEPROM counterGrey: ");
Serial.println(EEPROMReadlong(0));
Serial.print("EEPROM counterGreen: ");
Serial.println(EEPROMReadlong(4));
}
void loop () {
if (digitalRead(buttonGrey) == HIGH) {
currentStateGrey = 1;
}
else {
currentStateGrey = 0;
}
if (currentStateGrey != previousStateGrey) {
if (currentStateGrey == 1) {
counterGrey = EEPROMReadlong(0) + 1;
EEPROMWritelong(0, counterGrey);
fase = 1;
}
}
previousStateGrey = currentStateGrey;
if (digitalRead(buttonGreen) == HIGH) {
currentStateGreen = 1;
}
else {
currentStateGreen = 0;
}
if (currentStateGreen != previousStateGreen) {
if (currentStateGreen == 1) {
counterGreen = EEPROMReadlong(4) + 1;
EEPROMWritelong(4, counterGreen);
fase = 1;
}
}
previousStateGreen = currentStateGreen;
if (fase == 1) {
Serial.print("\ncounter grey: \t");
Serial.println(counterGrey);
Serial.print("counter green: \t");
Serial.println(counterGreen);
fase = 0;
}
}
if (digitalRead(buttonGrey) == HIGH)
{
currentStateGrey = 1;
}
else
{
currentStateGrey = 0;
}
Why not just
currentStateGrey = digitalRead(buttonGrey);
It would make more sense to use HIGH and LOW instead of 0 and 1 throughout the program when they refer to button states as it makes more sense.
Your read and write long functions are totally unnecessary as you could use the EEPROM put and get methods,
A small example
#include <EEPROM.h>
float outFloat = 123.456;
int outInt = 31234;
void setup()
{
Serial.begin(115200);
EEPROM.put(0, outFloat);
float inFloat;
EEPROM.get(0, inFloat);
Serial.println(inFloat);
EEPROM.put(0, outInt);
int inInt;
EEPROM.get(0, inInt);
Serial.println(inInt);
}
void loop()
{
}
UKHeliBob:
A small example
#include <EEPROM.h>
float outFloat = 123.456;
int outInt = 31234;
void setup()
{
Serial.begin(115200);
EEPROM.put(0, outFloat);
float inFloat;
EEPROM.get(0, inFloat);
Serial.println(inFloat);
EEPROM.put(0, outInt);
int inInt;
EEPROM.get(0, inInt);
Serial.println(inInt);
}
void loop()
{
}
Thanks for your feedback. I tryed to understand the up en get function. The part that i didn't unstood was that the get part also a value needs..... But when i look at the example is needs een varible. I will try this tomorrow