ive been playing with the arduino for a little under a month and im now onto my second project.
basically what i want to do is store a number in the eeprom, and every time i power up the arduino i want the number in the eeprom to deduct by 1, then perform rest of my code.
when the eeprom reaches 0 i need to code to stop running. (sort of like a trial, when trial reaches 0 stop working)
ive been looking into reading the eeprom and writing to it and updating it from the arduino examples but im a little confused on how i store the number,
do i need to break the number down into bytes before storing it ?
would anyone be able to give me an example of how to store the number 25 into the eeprom ?
int eepromMemory = 1;
byte counterValue = 250;
int subtractCounter = 1;
#include <EEPROM.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//EEPROM.write(eepromMemory, counterValue);
Serial.println(counterValue);
counterValue = counterValue - subtractCounter;
Serial.println(counterValue);
EEPROM.update(eepromMemory, counterValue);
}
void loop() {
// put your main code here, to run repeatedly:
}
[code]
so basically what ive written, i believe should set an inital value into the eeprom of 250, then it dose a simple subtraction, then rewrite the value to the eeprom,
however a reboot of my arduino just resets the counter to 250 again.
i though after i did the calculation and used
[code]
EEPROM.update(eepromMemory, counterValue);
this should then set the value to its new value for next time the eeprom is read ?
so i assume once ive run the 1st sketch i then upload the 2nd sketch which reads the eeprom, subtracts x from current number then re-writes the eeprom.
that's fine but how would i go about defining my counter variable ?
basically im trying to set a value in the eeprom. (which ive managed to do in sketch 1)
sketch 2
is ment to read the value from eeprom, then subtract 1 then re-write the eeprom so eventually the eeprom value will read 0.
however when i try to compile my read and subtract sketch i get the following
WRITE EEPROM WITH 250 VALUE SKETCH
int eepromMemory = 1; // set the location of the memory were going to use in the eeprom/
int counterValue = 250; // set the inital value for our countdown
#include <EEPROM.h> // include the eerpom libary
void setup() {
// put your setup code here, to run once:
// set the serial monitor
Serial.begin(9600);
EEPROM.write(eepromMemory, counterValue);
delay(500);
Serial.println(counterValue);
}
void loop() {
// put your main code here, to run repeatedly:
}
READ / SUBTRACT / WRITE SKETCH
int eepromMemory = 1; // set the location of the memory were going to use in the eeprom/
int counterValue; // set the var
int subtractCounter = 1;
#include <EEPROM.h> // include the eerpom libary
void setup() {
// put your setup code here, to run once:
// set the serial monitor
Serial.begin(9600);
EEPROM.read(eepromMemory, counterValue);
Serial.println(counterValue);
delay(500);
counterValue = counterValue - subtractCounter;
EEPROM.write(eepromMemory, counterValue);
delay(500);
Serial.println(counterValue);
}
void loop() {
// put your main code here, to run repeatedly:
}
ok so ive set the following code to turn on a LED when the counter hits 0
but when i reset the board at 0 it loops back to 254 ?
this is what the code looks like
int eepromMemory = 1; // set the location of the memory were going to use in the eeprom/
int counterValue; // set the var
int subtractCounter = 1;
#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");
counterValue = counterValue - subtractCounter;
EEPROM.write(eepromMemory, counterValue);
delay(500);
Serial.println(counterValue);
}
void loop() {
// put your main code here, to run repeatedly:
if (counterValue > 0) {
digitalWrite(LED, LOW);
// need to find a way to erase the eeprom to prevent it from looping.
} else {digitalWrite(LED, HIGH);}
}