Hi everyone, apologies if this has already been covered but i have searched and have only found the oposite problem.
my code is using the DS3231 rtc to keep time, obviously :), and i am using this to trigger relays at specific times; this portion of the code is functioning very well.
I decided i would like a manual way of triggering a relay sequence with a button and i opted for the hardware interrupt approach described here:
it does function as intended in all ways except, my "void ISR" contains delay() commands which are strangely no longer behaving in the units of milliseconds!
for example, i have delay(3000000) leading to a real life delay of ~18 seconds, where it clearly ought to be 3000 seconds or 50 minutes, so 166.666 faster.
I can fudge my way around this, by simply correcting by a factor of 166.7, but i would prefer to understand what i have done to cause this.
This is my first post, i have been tinkering with arduino in my late night free-time when the kids are sleeping, sorry if it feels like a waste of bits reading it but i'd love someone to explain what is happening here to me.
Many thanks!
apologies, it wont let me upload the .ino as i am a new user so here is the code:
//based on viral science code//
#include <DS3231.h>
int Pump_Relay = 8;
int Relay2 = 3;
int Relay3 = 4;
int Relay4 = 5;
int Relay5 = 6;
int Relay6 = 7;
DS3231 rtc(SDA, SCL);
Time t;
//need to make Pump_OnHour, relay 1 OnHour etc etc.
const int PumpOnHour = 19; //SET TIME TO ON Pump (24 HOUR FORMAT)
const int PumpOnMin = 37;
const int PumpOffHour = 22; //SET TIME TO OFF Pump
const int PumpOffMin = 28;
const int Relay1_OnHour = 19; //SET TIME TO ON Relay1 (24 HOUR FORMAT)
const int Relay1_OnMin = 37;
const int Relay1_OffHour = 22; //SET TIME TO OFF Relay1
const int Relay1_OffMin = 28;
const int Relay2_OnHour = 19; //SET TIME TO ON Relay2 (24 HOUR FORMAT)
const int Relay2_OnMin = 38;
const int Relay2_OffHour = 22; //SET TIME TO OFF Relay2
const int Relay2_OffMin = 28;
const int Relay3_OnHour = 19; //SET TIME TO ON Relay3 (24 HOUR FORMAT)
const int Relay3_OnMin = 39;
const int Relay3_OffHour = 22; //SET TIME TO OFF Relay3
const int Relay3_OffMin = 28;
const int Relay4_OnHour = 19; //SET TIME TO ON Relay4 (24 HOUR FORMAT)
const int Relay4_OnMin = 40;
const int Relay4_OffHour = 22; //SET TIME TO OFF Relay4
const int Relay4_OffMin = 28;
const int Relay5_OnHour = 20; //SET TIME TO ON Relay5 (24 HOUR FORMAT)
const int Relay5_OnMin = 19;
const int Relay5_OffHour = 22; //SET TIME TO OFF Relay5
const int Relay5_OffMin = 28;
const int Relay6_OnHour = 20; //SET TIME TO ON Relay6 (24 HOUR FORMAT)
const int Relay6_OnMin = 19;
const int Relay6_OffHour = 22; //SET TIME TO OFF Relay6
const int Relay6_OffMin = 28;
int buttonPin = 2;
volatile int switchState;
void buttonInterrupt() {
switchState = digitalRead(buttonPin);
if (switchState == HIGH) {
digitalWrite(Pump_Relay,HIGH);
delay(3000000); // this results in 18 s of delay in reality!
digitalWrite(Relay2,HIGH);
delay(3000000); // this results in 18 s of delay in reality!
digitalWrite(Relay2,LOW);
digitalWrite(Relay3,HIGH);
delay(3000000); // this results in 18 s of delay in reality!
digitalWrite(Relay3,LOW);
digitalWrite(Relay4,HIGH);
delay(3000000); // this results in 18 s of delay in reality!
digitalWrite(Relay4,LOW);
digitalWrite(Relay5,HIGH);
delay(3000000); // this results in 18 s of delay in reality!
digitalWrite(Relay5,LOW);
digitalWrite(Relay6,HIGH);
delay(3000000); // this results in 18 s of delay in reality!
digitalWrite(Relay5,LOW);
digitalWrite(Pump_Relay,LOW);
delay(3000000); // this results in 18 s of delay in reality!
}
}
void setup() {
Serial.begin(9600);
rtc.begin();
pinMode(buttonPin, INPUT_PULLUP);
pinMode(Pump_Relay, OUTPUT);
digitalWrite(Pump_Relay, LOW);
pinMode(Relay2, OUTPUT);
digitalWrite(Relay2, LOW);
pinMode(Relay3, OUTPUT);
digitalWrite(Relay3, LOW);
pinMode(Relay4, OUTPUT);
digitalWrite(Relay4, LOW);
pinMode(Relay5, OUTPUT);
digitalWrite(Relay5, LOW);
pinMode(Relay6, OUTPUT);
digitalWrite(Relay6, LOW);
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, CHANGE);
}
void loop() {
//switchState = digitalRead(2);
t = rtc.getTime();
Serial.print(t.hour);
Serial.print(" hour(s), ");
Serial.print(t.min);
Serial.print(" minute(s)");
Serial.println(" ");
delay (1000);
if(t.hour == PumpOnHour && t.min == PumpOnMin){
digitalWrite(Pump_Relay,HIGH);
Serial.println("PUMP ON");
}
else if(t.hour == PumpOffHour && t.min == PumpOffMin){
digitalWrite(Pump_Relay,LOW);
Serial.println("PUMP OFF");
}
if(t.hour == Relay2_OnHour && t.min == Relay2_OnMin){
digitalWrite(Relay2,HIGH);
Serial.println("Relay2 ON");
}
else if(t.hour == Relay2_OnHour && t.min == Relay2_OnMin){
digitalWrite(Relay2,LOW);
//Serial.println("Relay1 OFF");
}
if(t.hour == Relay3_OnHour && t.min == Relay3_OnMin){
digitalWrite(Relay3,HIGH);
Serial.println("Relay3 ON");
}
else if(t.hour == Relay3_OnHour && t.min == Relay3_OnMin){
digitalWrite(Relay3,LOW);
//Serial.println("Relay3 OFF");
}
if(t.hour == Relay4_OnHour && t.min == Relay4_OnMin){
digitalWrite(Relay4,HIGH);
Serial.println("Relay4 ON");
}
else if(t.hour == Relay4_OnHour && t.min == Relay4_OnMin){
digitalWrite(Relay4,LOW);
//Serial.println("Relay3 OFF");
if(t.hour == Relay5_OnHour && t.min == Relay5_OnMin){
digitalWrite(Relay5,LOW);
Serial.println("Relay5 ON");
}
else if(t.hour == Relay5_OnHour && t.min == Relay5_OnMin){
digitalWrite(Relay5,HIGH);
//Serial.println("Relay3 OFF");
if(t.hour == Relay6_OnHour && t.min == Relay6_OnMin){
digitalWrite(Relay6,LOW);
Serial.println("Relay6 ON");
}
else if(t.hour == Relay6_OnHour && t.min == Relay6_OnMin){
digitalWrite(Relay6,HIGH);
//Serial.println("Relay3 OFF");
}
}
}
}