falexandru:
OK, I said I just started to learn C++. I already learn a lot from your comments here. You will be amazed to know how little attention is paid to crucial knowledge in most internet references, while obvious facts are repeated over and over again.
Actually no, I would not be surprised.
It's been quite a while since I was in the newbie phase for C++, so I don't exactly remember what was most helpful for me. One thing I do know is that as part of my ongoing learning, no tutorial beats a comprehensive reference. For the C++ language itself, try CPP Reference. For the AVR specific libraries that the Arduino core is built on top of, the avr-libc modules page is indispensable.
In the EEPROM delay(5) is said mandatory to allow time for the chip to write. Is there any other way to cope with this?
I'm not sure which EEPROM you have, but for the one I looked at (datasheet attached) there is an "Acknowledge Polling" section that describes how you can check if the EEPROM is finished with it's write. This EEPROM takes 10-20 ms to perform a write, so you can't just trust a blind 5 ms delay. I imagine most EEPROMs will have a comparable way to check if the write it finished. The method for this EEPROM might even be a de facto standard common to most I2C EEPROMs.
Since my projects and needs are very much related to timing, the delay command is by its nature "disgusting" me. As the GoTo in the old school.
You're off to a good start.
Regaring some other thing you asked in a previous post:
Why do we need void functions anyway? Wouldnt be easier to just get a ghost return?
C++ follows the philosophy of strong typing. Everything must be declared with a specific type, and implicit conversions are only allowed according to strict rules. By having void as a return type, it tells the compiler that the function will not return a value and cannot be used as part of an expression. Nonsense like int var = loop();
will give you a compilation error.
An EEPROM write takes 3.3mS to complete. The library may take that into account already.
Write yourself a simple test to confirm:
capture the time, startTime = micros();
perform the EEPROM write.
capture the time again, endTime = micros();
determine how much time passed, elapsedTime = endTime - startTime;
If it's around 3300, then you know the library is waiting out the write.
If its much faster, then you need to do something to let the EEPROM write finish before starting another one.
falexandru did not specify that it was the internal EEPROM. And since a library was referenced, I assume it's an external one.
AT24C32 - EEPROM, I2C 32k.pdf (210 KB)