so ive been playing around with the eeprom for a few days now and i have managed to achieve what i was try to,
but id like to know more about storing a number larger then 256 in the eeprom,
the company i work for have a programmer which we sell, currently at the moment the programmer is unlimited, (it programs doors) however the big boss would like the option to sell these programmers with x amount of uses on them, (a basic count down on the eeprom) ive managed to achieve this but the problem i have is i can currently only write a max of 256 to eeprom.
can anyone point me in the right directon of how this is done.
i love learning this stuff, ive checked the examples but cant find anthing,
Look where sorry ?
ive been threw arduino software looking at examples.
i write this which has compiled but im not sure if it will work, i currently cant test as ive broken my board and waiting on a replacement.
would this below work ?
int eepromMemory = 1; // set the location of the memory were going to use in the eeprom/
int counterValue; // set the var
int subtractCounter = 1;
int testValue;
int i;
#include <EEPROM.h> // include the eerpom libary
int LED = 13;
void setup() {
pinMode(LED, OUTPUT);
// put your setup code here, to run once:
// set the serial monitor
Serial.begin(9600);
counterValue = EEPROM.read(eepromMemory);
Serial.println("Previous eeprom Value");
Serial.println(counterValue);
delay(500);
Serial.println("New eeprom Value");
if (counterValue, testValue > 0) {counterValue = counterValue - subtractCounter; // check to see if counter above zero, if so deduct
} else {
//do nothing
}
//counterValue = counterValue - subtractCounter;
EEPROM.write(eepromMemory, counterValue);
delay(500);
Serial.println(counterValue);
}
void loop() {
// put your main code here, to run repeatedly:
// start test code
//if (EEPROM.read(counterValue == 0)
//for(int i=0; i<=255; i++);
//EEPROM.write(i, 0);
// end test code
if (counterValue > 0) {
digitalWrite(LED, LOW);
// need to find a way to erase the eeprom to prevent it from looping.
} else {digitalWrite(LED, HIGH);}
}
this is the line i have modified.
if (counterValue, testValue > 0) {counterValue = counterValue - subtractCounter; // check to see if counter above zero, if so deduct
so basically im telling the script to check counterValue & testValue if both are above zero to remove the count from one of them ?
ive had alook at splitting up the number but i dont quite understand how it works,
There are two macros, highByte() and lowByte(), that will extract the high and low bytes from an integer. Since you can store bytes, storing two of them means that you can store an int.
There is a macro, word(), that combines two bytes to make an int, so you can recreate the int that you split up to store.
Of course, the macros are doing nothing that you can't do with division and the modulo function or bit-shifting and masking.
Another way is to use a union for the target data type:
#include <EEPROM.h>
union {
byte myArray[2];
int val;
} myUnion;
void setup() {
int i;
int j;
int offset = 0;
int temp;
Serial.begin(9600);
myUnion.val = 1000;
for (j = 0; j < 10; j++) {
for (i = 0; i < sizeof(int); i++) {
EEPROM.write(offset, myUnion.myArray[i]); // Write to EEPROM
offset++;
}
Serial.println(myUnion.val);
myUnion.val++;
}
Serial.println("====== Reading Back ======");
offset = 0;
for (j = 0; j < 10; j++) {
for (i = 0; i < sizeof(int); i++) {
myUnion.myArray[i] = EEPROM.read(offset++); // Read from EEPROM
}
Serial.println(myUnion.val);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
this is where im at so far, it compiles, can someone tell me if im onto the right track, as with eeprom as far as im aware there is no way to tell if im writing to the eeprom correctly.
#include <EEPROM.h>
int eepromMemory1 = 1;
int eepromMemory2 = 2;
int counterLimit = 1000;
byte HighByte1(eepromMemory1);
byte LowByte1(eepromMemory1);
byte HighByte2(eepromMemory2);
byte LowByte2(eepromMemory2);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
EEPROM.write(eepromMemory1, HighByte1);
EEPROM.write(eepromMemory2, LowByte1);
}
void loop() {
// put your main code here, to run repeatedly:
}
so from what ive worked out (weather it be right or wrong) is this code should write the number 1000 to the eeprom in 2 segments ?
im now trying to master value above 1000 and then ill intergrate it into my current project.,
int counter = 1000;
void setup()
{
// Read the stored values
byte hi = EEPROM.read(1);
byte lo = EEPROM.read(2);
// Reassemble the integer
counter = word(hi, lo);
// Decrement to 0 but not lower
if(counter > 0)
counter--;
// Store the new value
EEPROM.write(1, highByte(counter));
EEPROM.write(2, lowByte(counter));
}
void loop()
{
if(counter > 0)
{
// do it up to 1000 times
}
}
mathewbuer:
this is where im at so far, it compiles, can someone tell me if im onto the right track, as with eeprom as far as im aware there is no way to tell if im writing to the eeprom correctly.
If you are using the Arduino IDE version 1.6.2 or greater, then your task becomes far easier. The method EEPROM.put() can write values larger than 255 (data types greater than 1 byte). You can use EEPROM.get() to read the data.
Here is an example of reading and writing an int!
#include <EEPROM.h>
void setup(){
int data = 1000;
int address = 0; //data will be stored in cells 0 & 1
Serial.begin(9600);
Serial.print( "data: " );
Serial.println( data );
// Write data to EEPROM
EEPROM.put( address, data );
int result = 0; //So we can test the read.
// Read the data.
EEPROM.get( address, result );
Serial.print( "result: " );
Serial.println( result );
}
void loop(){}