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);
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.
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
}
}
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)
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