I will be grateful for help to anyone who has code problem I am using ds3231 moudle and Arduino mega. I will explain the details of the code AND what i want:
1- I have three input and tow output
This my code:
#include <DS3232RTC.h>
#include <Streaming.h>
#include <Time.h>
#include <TimeLib.h>
#include <Wire.h>
#include <DS3231.h>
#include <RTClib.h>
RTC_DS3231 rtc;
const int button = 2;
const int button1 = 3;
int G = 6 ;
const int led = 7;
const int led1 = 8;
int buttonState = 0;
int GSTATE=0;
int buttonState1 = 0;
int long Gen_Shutdown_Time = 0;
int long Now_Stop_Time = 0;
int long Stop_Time = 0;
int long Start_Time=0;
int long Now_Start_Time=0;
void setup() {
Serial.begin(9600);
pinMode(button1, INPUT_PULLUP);//INPUT1
pinMode(G, INPUT_PULLUP);//INPUT2
pinMode(button, INPUT_PULLUP);//INPUT3
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
while (!Serial) ; // wait until Arduino Serial Monitor opens
setSyncProvider(RTC.get); // the function to get the time from the RTC
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
void CAH () {
DateTime now = rtc.now();
> if ((buttonState == LOW && buttonState1 == HIGH) || (buttonState == HIGH && buttonState1 == LOW)&&GSTATE==LOW) {
Now_Stop_Time = now.unixtime();
Stop_Time=Now_Stop_Time + 300L; //Set stop time now.unixtime + 5 minutes
Serial.println("START HEATING");
Serial.print( Stop_Time);
Serial.print("\t");
}
if ((buttonState == HIGH && buttonState1 == HIGH) && GSTATE==LOW) {
Now_Start_Time = now.unixtime();
Start_Time=Now_Start_Time + 300L; //Set start time now.unixtime + 5 minutes
Serial.print( Start_Time);
Serial.print("\t");
}
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
CAH ();
buttonState = digitalRead(button);
buttonState1 = digitalRead(button1);
GSTATE = digitalRead(G);
if (Stop_Time <= now.unixtime() ) { //5 minute count down for cool down before begin shutdown off
digitalWrite(led, LOW);
//Serial.print(now.unixtime());
Serial.print("\t");
delay(1);
}
if (Start_Time <= now.unixtime() ) {
digitalWrite(led1, HIGH);
}
delay(1000);
}
The problem is that when the condition is true( if ((buttonState == LOW && buttonState1 == HIGH) || (buttonState == HIGH && buttonState1 == LOW)&&GSTATE==LOW) {
) and if ((buttonState == HIGH && buttonState1 == HIGH) && GSTATE==LOW) {
for this function CAH ();, the output state of the led and led1 does not change ,Not executing commands in main void loop
if (Stop_Time <= now.unixtime() ) { //5 minute count down for cool down before begin shutdown off
digitalWrite(led, LOW);
//Serial.print(now.unixtime());
Serial.print("\t");
delay(1);
}
if (Start_Time <= now.unixtime() ) {
digitalWrite(led1, HIGH);
}