how to write this code with use of eeprom

please help I am a beginner

this code not working for that
blinking light for 1 second and delay 10000 milliseconds but
when i press reset button the begins from first
please help

i just want to led on 1 second and delay for 10000 millisecond from where the stop start from there when the led stopped for 10000 millisec the led off when i reset the button

please show me the right way

#include<EEPROM.h>
int led = 12;
byte ee_val0;
byte ee_val1;
void setup() {
// put your setup code here, to run once:
ee_val0 = EEPROM.read (0);
ee_val1 = EEPROM.read (1);

pinMode (led, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite (led, HIGH);
delay(1000);
EEPROM.update(0,1);

digitalWrite(led,LOW);
EEPROM.write(1,1);
delay(10000);
EEPROM.update(1,0);
}

what are you trying to do with the EEPROM?

One obvious problem is that you read 2 values from EEPROM in setup() but never use them in the program

Another potential problem is that you are writing to EEPROM every 11 seconds in loop() and each EEPROM location is only guaranteed for 100,000 writes. Work out how long it would be before you do 100,000 writes to an EEPROM location

It is difficult to understand your requirements. Do you want the on/off sequence to continue from the same state it was in when reset or powered down ? What if it were half way through the 10 second delay() when powered down ? What should happen when it is powered up ? Another 10 second delay or perhaps a 5 second delay ?

And you’re okay that after about a month of operation it is very likely your Arduino will have destroyed one of the EEPROM locations making that location unusable?
See post #2.

Hi sumayyah,

I think this should help you out!

Kind regards,

Zeb

Duplicate threads merged

Why did you post this question again ?
Did you not like the replies to the original post ?

consider following tested with 3 buttons and 3 leds and active period of only 5 sec

#include <EEPROM.h>

#define ON  LOW
#define OFF HIGH

byte sensors [] = { A1, A2, A3 };
byte outputs [] = { 10, 11, 12 };

#define EE_ADDR     0
#define MAX_STATE   3
byte state;

#define ON_PERIOD   5000
byte idx;
unsigned long msec;
unsigned long msecLst = 0;

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin (115200);

    for (unsigned i = 0; i < sizeof(sensors); i++)
        pinMode (sensors [i], INPUT_PULLUP);

    for (unsigned i = 0; i < sizeof(outputs); i++)  {
        digitalWrite (outputs [i], OFF);
        pinMode (outputs [i], OUTPUT);
    }

    // read last state.  limit if not previously written
    state = EEPROM.read (EE_ADDR);
    if (MAX_STATE < state)
        state = 0;

    // set msecLst if relay previously on
    msecLst = millis();
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    msec = millis();
    idx  = state / 2;

    // even values of state represent waiting for a sensor and
    // odd values that a relay is active

    if (! (state % 2))  {  // check sensor on even states
        // check if sensor active low
        if (LOW == digitalRead (sensors [idx]))  {
            msecLst = msec;                     // set start time
            EEPROM.write (EE_ADDR, ++state);    // advance and save state
            Serial.println (state);
        }
    }

    else {                  // output active
        if (msec - msecLst > ON_PERIOD)  {      // check if period expired
            digitalWrite (outputs [idx], OFF);  // turn output off
            EEPROM.write (EE_ADDR, ++state);    // advance and save state
            Serial.println (outputs [idx]);
        }
        else
            digitalWrite (outputs [idx], ON);   // always turn on to handle
                                                // starting in active state
    }
}

Start by reading Read this before posting a programming question and post the code in code tags unless some of it really should be in italics

sumayyah:
but I didn't understand this please explain I am just a beginner
please show me circuit diagram

these arrays define what are the input and output pins. I specified pins for a Multifunction Board. sensors are buttons and outputs are LEDs

byte sensors [] = { A1, A2, A3 };
byte outputs [] = { 10, 11, 12 };

the inputs are active low. I used #defines to describe the on/off output states

#define ON  LOW
#define OFF HIGH

i interpreted what you said you wanted to mean that you wait for the first input (sensor) to be triggered and activate the relay for 30 seconds before checking the next sensor. not sure what your goal is.

study the code. Comments describe "what" the code is doing. The code is "how" is does it. It may not be doing what you want and the use of the odd/even state values is unusual but keeps things simple for what I think your want

you need to learn to post code inside the code tags that appear when you select the "</>" icon (top left)

Have you decided yet whether it is OK to wear out your EEPROM so quickly ?

UKHeliBob:
Have you decided yet whether it is OK to wear out your EEPROM so quickly ?

how quickly? (once / 30 sec)

gcjr:
how quickly? (once / 30 sec)

Writing once every 30 seconds with a guaranteed number of writes of 100,000 means that the EEPROM might fail after :
30 * 100,000 seconds, ie 3,000,000 seconds
3,000,000 seconds is 3,000,000 / 60, ie 50,000 minutes
50,000 minutes is 50,000 / 60, ie 833 hours
833 hours is 833 / 24, ie 34 days

As predicted in post #4