Hi all. I am trying to make a program in which when i press and hold a push button for 3sec the lights cycle will be set to run for 12hrs starting from the time push button was pressed. e.g, if i press and hold the push button at 10am, the lights cycle will start from 10am to 10pm. The cycle should remain that way until i decide to change it by press + hold the button at my desired time
So far i have developed that program but the problem I have is to save the push button input in eeprom memory for if the Arduino might be plugged off, the Arduino must remember the last push button input.
#include <Wire.h>
#include "RTClib.h"
#include <EEPROM.h>
#define lightSignalPin 6
#define lightsPin 13
#define pumpsPin 9
#define buttonManualWateringPin 12
#define lightButtonSetTimePin 8
#define sensorPin A1
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//boolean isPressed = false;
//////////************************************Manual watering variables**********************************************///////
int lightState = 0;
// variables will change:
int buttonLightsState = 0; // variable for reading the pushbutton status
int lightsProgramState = 0;
int levelSensorState = 1; //reads pushbutton status
int buttonSetTimeProgramState =0;
long buttonSetTimeMillis = 0;
const long buttonSetTimeInterval = 3000;
void setup() {
// put your setup code here, to run once:
while (!Serial); // for Leonardo/Micro/Zero
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2022, 5, 31, 10, 35, 20));
}
// Initializing pins
pinMode(pumpsPin, OUTPUT);
pinMode(buttonManualWateringPin, INPUT_PULLUP);
pinMode(lightSignalPin, OUTPUT);
pinMode(lightsPin, OUTPUT);
pinMode(lightButtonSetTimePin, INPUT_PULLUP);
pinMode(sensorPin, INPUT_PULLUP);
EEPROM.get(0, lightState);
}
void loop() {
// put your main code here, to run repeatedly:
pressingButton();
}
void pressingButton(void){
levelSensorState = digitalRead(lightButtonSetTimePin);
DateTime now = rtc.now();
unsigned long currentlightsCycleMillis = millis();
//waiting for button to be pressed
if (buttonSetTimeProgramState == LOW && lightsProgramState == 0) {
buttonSetTimeMillis = currentlightsCycleMillis;
lightsProgramState = 1;
}
else if (lightsProgramState == 1 && buttonSetTimeProgramState == HIGH) {
lightsProgramState = 0; //reset
}
if(currentlightsCycleMillis - buttonSetTimeMillis > buttonSetTimeInterval && lightsProgramState == 1) {
lightsProgramState = 2;
buttonSetTimeMillis = currentlightsCycleMillis;
lightSettings();
}
else{
if (lightState == 0){
if(now.hour() >= 6 && now.hour() <= 18){
Serial.println("Lights ON by default");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() > 18){
Serial.println("Lights OFF default");
digitalWrite(lightsPin, LOW);
}
}
}
}
void lightSettings(){
DateTime now = rtc.now();
// Once per minute...
int selector = now.hour();
// ...Display the time...
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.println("");
switch (selector){
case 6:
if(now.hour() >= 6 && now.hour() <= 18){
Serial.println("Lights ON state 1");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() > 18){
Serial.println("Lights OFF 1");
digitalWrite(lightsPin, LOW);
}
lightState = 1;
lightsProgramState = 0;
break;
case 7:
if(now.hour() >=7 && now.hour() <= 19 ){
Serial.println("Lights ON state 2");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() > 19){
Serial.println("Lights OFF 2");
digitalWrite(lightsPin, LOW);
}
lightState = 2;
lightsProgramState = 0;
break;
case 8:
if(now.hour() >= 8 && now.hour() <= 20){
Serial.println("Lights ON state 3");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() > 20){
Serial.println("Lights OFF 3");
digitalWrite(lightsPin, LOW);
}
lightState = 3;
lightsProgramState = 0;
break;
case 9:
if(now.hour() >=9 && now.hour() <= 21 ){
Serial.println("Lights ON state 4");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() > 21){
Serial.println("Lights OFF 4");
digitalWrite(lightsPin, LOW);
}
lightState = 4;
lightsProgramState = 0;
break;
case 10:
if(now.hour() >= 10 && now.hour() <= 22){
Serial.println("Lights ON state 5");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() > 22){
Serial.println("Lights OFF 5");
digitalWrite(lightsPin, LOW);
}
lightState = 5;
lightsProgramState = 0;
break;
case 11:
if(now.hour() >=11 && now.hour() <= 23 ){
Serial.println("Lights ON state 6");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() > 23){
Serial.println("Lights OFF 6");
digitalWrite(lightsPin, LOW);
}
lightState = 6;
lightsProgramState = 0;
break;
case 12:
if(now.hour() >= 12 ){
Serial.println("Lights ON state 7");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() == 00){
Serial.println("Lights OFF 7");
digitalWrite(lightsPin, LOW);
}
lightState = 7;
lightsProgramState = 0;
break;
case 13:
if(now.hour() >=13){
Serial.println("Lights ON state 8");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() == 1){
Serial.println("Lights OFF 8");
digitalWrite(lightsPin, LOW);
}
lightState = 8;
lightsProgramState = 0;
break;
case 14:
if(now.hour() >=14 && now.hour() <= 2 ){
Serial.println("Lights ON state 9");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() == 2){
Serial.println("Lights OFF 9");
digitalWrite(lightsPin, LOW);
}
lightState = 9;
lightsProgramState = 0;
break;
case 15:
if(now.hour() >=15 && now.hour() <= 3 ){
Serial.println("Lights ON from 00 to 12");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() == 3){
Serial.println("Lights OFF 10");
digitalWrite(lightsPin, LOW);
}
lightState = 10;
lightsProgramState = 0;
break;
case 5:
if(now.hour() >=5 && now.hour() <= 17 ){
Serial.println("Lights ON from 5 to 17");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() == 17){
Serial.println("Lights OFF 11");
digitalWrite(lightsPin, LOW);
}
lightState = 11;
lightsProgramState = 0;
break;
default:
Serial.println("Lights ON by default");
lightState = 0;
if(now.hour() >= 6 && now.hour() <= 18){
Serial.println("Lights ON");
digitalWrite(lightsPin, HIGH);
}
if(now.hour() > 18){
Serial.println("Lights OFF");
digitalWrite(lightsPin, LOW);
}
}
EEPROM.put(0, lightState);
}
I'm not sure how program the eeprom. I did read some documents about it and tried to find some examples online but I don't know how to implement it in my code. Please assist.
TIA

