I have the following situation: I will turn on my Arduino once everyday. Nonetheless, I want to keep track of how many days have gone by by updating a variable "var" (code below). If one week as gone by (var == 7), I perform an action and reset var to 1. Otherwise, var = var + 1;
What I wanted to ask you is, since I am going to shut down Arduino everyday, how to adapt the code so that Arduino reads it and updates this variable by using EEPROM (I guess that's what I need to store the variable after shutdown).
// set water relays
int relay1 = 3;
int relay2 = 4;
int relay3 = 5;
int relay4 = 6;
//init valve nr (x) and pumping time (y)
int x = 0;
int y = 3;
// set water pump
int pump = 2;
//Water correct plant function
void waterPlant(int x, int y) {
Serial.print("Opening: ");
Serial.print(x);
//Open valve x ==> x will contain value of correct relay[1-4]
digitalWrite(x, HIGH);
//We wait 0.5 seconds before opening pump so we are sure valve is open. This will prevent the pump from pushing water to a closed valve and risking the tubes to leak or wose, break loose.
delay(500);
//Open pump for y seconds
digitalWrite(pump, HIGH);
delay(1000*y);
//close pump
digitalWrite(pump, LOW);
delay(500);
digitalWrite(x, LOW);
//Make sure all valves are closed again.
closeAll();
}
void setup() {
// declare relay as output
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
// declare pump as output
pinMode(pump, OUTPUT);
// declare the ledPin as an OUTPUT:
Serial.begin(9600);
}
void loop() {
waterPlant(relay2);
waterPlant(relay3);
waterPlant(relay4);
if (var == 7) {
waterPlant(relay1);
var == 1;
}
else {
var == var + 1;
}
closeAll();
}
// turn pump & valves off just to be sure.
void closeAll() {
Serial.println("closing pump + valves!");
digitalWrite(pump, LOW);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
}
#include <EEPROM.h>
// set water relays
int relay1 = 3;
int relay2 = 4;
int relay3 = 5;
int relay4 = 6;
//init valve nr (x) and pumping time (y)
int x = 0;
int y = 3;
//init var
int var = 0;
// set water pump
int pump = 2;
//Water correct plant function
void waterPlant(int x, int y) {
Serial.print("Opening: ");
Serial.print(x);
//Open valve x ==> x will contain value of correct relay[1-4]
digitalWrite(x, HIGH);
//We wait 0.5 seconds before opening pump so we are sure valve is open. This will prevent the pump from pushing water to a closed valve and risking the tubes to leak or wose, break loose.
delay(500);
//Open pump for y seconds
digitalWrite(pump, HIGH);
delay(1000*y);
//close pump
digitalWrite(pump, LOW);
delay(500);
digitalWrite(x, LOW);
//Make sure all valves are closed again.
closeAll();
}
void setup() {
// declare relay as output
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
// declare pump as output
pinMode(pump, OUTPUT);
// declare the ledPin as an OUTPUT:
Serial.begin(9600);
}
void loop() {
waterPlant(relay2, 3);
waterPlant(relay3, 3);
waterPlant(relay4, 3);
var = EEPROM.read(0);
if (var == 7) {
waterPlant(relay1,3);
EEPROM.write(0, 1);
}
else {
EEPROM.update(0, var+1);
}
closeAll();
delay(999999999);
}
// turn pump & valves off just to be sure.
void closeAll() {
Serial.println("closing pump + valves!");
digitalWrite(pump, LOW);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
}
The big delay is just to ensure that it doesn’t loop through the write function more than once (the Arduino will only be on for 30 min).
You need to do your counting on startup, not on shutdown:
unsigned int dayCounter;
bool daySeven = false;
void setup()
{
EEPROM.get(0, dayCounter);
dayCounter++;
if (dayCounter == 7)
{
daySeven = true;
dayCounter = 0;
}
EEPROM.put(0, dayCounter);
}
void loop()
{
if (daySeven)
{
//If you only need to do your work once, you set daySeven to false here.
//Do your stuff here.
} else delay(1000);
}
This will increment dayCounter for each time the arduino is switched on. You just need to make sure that the EEPROM memory used is reset to 0 before starting to use it.