4 relay controller with schedule and manual switch button

Hello, i have a project with relays that can switch manually using switch button and can also switch automatically with schedule, i already have a DS1302 and its working with relay but my problem is when i combine the code for relay using switch button and the schedule using DS1302, and the DHT11 for sensor > 20 will turn on relay, there is a delay and sometimes the buttons are not working.

this is my current code

#include <virtuabotixRTC.h>
#include <DHT.h>

#define DHTPIN 13 //Connect Out pin to D2 in NODE MCU
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

// constants won't change
const int BUTTON_PIN = 12; // Arduino pin connected to button's pin
const int BUTTON_PIN3 = 11; // Arduino pin connected to button's pin
const int BUTTON_PIN2 = 10; // Arduino pin connected to button's pin
const int BUTTON_PIN1 = 9; // Arduino pin connected to button's pin
const int RELAY_PIN = 4; // Arduino pin connected to relay's pin
const int RELAY_PIN3 = 2; // Arduino pin connected to relay's pin
const int RELAY_PIN2 = 3; // Arduino pin connected to relay's pin
const int RELAY_PIN1 = 5; // Arduino pin connected to relay's pin

virtuabotixRTC myRTC(6, 7, 8);

const int OnHour = 10; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 45;
const int OffHour = 10; //SET TIME TO OFF RELAY
const int OffMin = 47;

// variables will change:
int relayState = LOW; // the current state of relay
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
int relayState1 = LOW; // the current state of relay
int lastButtonState1; // the previous state of button
int currentButtonState1; // the current state of button
int relayState2 = LOW; // the current state of relay
int lastButtonState2; // the previous state of button
int currentButtonState2; // the current state of button
int relayState3 = LOW; // the current state of relay
int lastButtonState3; // the previous state of button
int currentButtonState3; // the current state of button

void setup() {
Serial.begin(9600);

pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode

pinMode(BUTTON_PIN1, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(RELAY_PIN1, OUTPUT); // set arduino pin to output mode

pinMode(BUTTON_PIN2, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(RELAY_PIN2, OUTPUT); // set arduino pin to output mode

pinMode(BUTTON_PIN3, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(RELAY_PIN3, OUTPUT); // set arduino pin to output mode

digitalWrite(RELAY_PIN,LOW);
digitalWrite(RELAY_PIN1,LOW);
digitalWrite(RELAY_PIN2,LOW);
digitalWrite(RELAY_PIN3,LOW);

currentButtonState = digitalRead(BUTTON_PIN);
currentButtonState1 = digitalRead(BUTTON_PIN1);
currentButtonState2 = digitalRead(BUTTON_PIN2);
currentButtonState3 = digitalRead(BUTTON_PIN3);

// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year
myRTC.setDS1302Time(00, 44, 10, 3, 30, 11, 2022);
dht.begin();
}

void loop() {

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}

// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print(F(" Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("C "));
Serial.print(f);
Serial.print(F("F Heat index: "));
Serial.print(hic);
Serial.print(F("C "));
Serial.print(hif);
Serial.println(F("F"));

// This allows for the update of variables for time or accessing the individual elements.
myRTC.updateTime();
int buttonState = digitalRead(BUTTON_PIN); // read new
int buttonState1 = digitalRead(BUTTON_PIN1); // read new state
int buttonState2 = digitalRead(BUTTON_PIN2); // read new state
int buttonState3 = digitalRead(BUTTON_PIN3); // read new state

// Start printing elements as individuals
Serial.print("Current Date / Time: ");
Serial.print(myRTC.dayofmonth);
Serial.print("/");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.println(myRTC.seconds);
// Delay so the program doesn't print non-stop
delay(500);

lastButtonState = currentButtonState; // save the last state
lastButtonState1 = currentButtonState1; // save the last state
lastButtonState2 = currentButtonState2; // save the last state
lastButtonState3 = currentButtonState3; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
currentButtonState1 = digitalRead(BUTTON_PIN1); // read new state
currentButtonState2 = digitalRead(BUTTON_PIN2); // read new state
currentButtonState3 = digitalRead(BUTTON_PIN3); // read new state

if (t > 20){ digitalWrite(RELAY_PIN2, LOW);}

if(lastButtonState == LOW && currentButtonState == HIGH) {
Serial.println("The button is pressed");
// toggle state of relay
relayState = !relayState;
// control relay arccoding to the toggled state
digitalWrite(RELAY_PIN, relayState);
}
if(lastButtonState1 == HIGH && currentButtonState1 == LOW) {
Serial.println("Relay 1");

// toggle state of relay
relayState1 = !relayState1;

// control relay arccoding to the toggled state
digitalWrite(RELAY_PIN1, relayState1); 

}
if(lastButtonState2 == HIGH && currentButtonState2 == LOW) {
Serial.println("Relay 2");

// toggle state of relay
relayState2 = !relayState2;

// control relay arccoding to the toggled state
digitalWrite(RELAY_PIN2, relayState2); 

}
if(lastButtonState3 == HIGH && currentButtonState3 == LOW) {
Serial.println("Relay 3");

// toggle state of relay
relayState3 = !relayState3;

// control relay arccoding to the toggled state
digitalWrite(RELAY_PIN3, relayState3); 

}
// control using schedule
if(myRTC.hours == OnHour && myRTC.minutes == OnMin){
digitalWrite(RELAY_PIN, LOW);
Serial.println("LIGHT ON");
}

else if(myRTC.hours == OffHour && myRTC.minutes == OffMin){
digitalWrite(RELAY_PIN,HIGH);
Serial.println("LIGHT OFF");
}

}

Schematics are valuable as relays do need special care.
Please post code, not text like this. Read 'how to' here: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum
A wild guess... Does any device use SPI? You use some I/O for buttons that are also used for SPI.

Sorry i cant upload , because I'm a new user

But You can download the linked info, follow it and post code according to the advice.

Hello markii_2430

Welcome to the forum.

This is a free forum service to get you started.

#include <virtuabotixRTC.h>
#include <DHT.h>
#define DHTPIN 13 //Connect Out pin to D2 in NODE MCU
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// constants won't change
const int BUTTON_PIN = 12; // Arduino pin connected to button's pin
const int BUTTON_PIN3 = 11; // Arduino pin connected to button's pin
const int BUTTON_PIN2 = 10; // Arduino pin connected to button's pin
const int BUTTON_PIN1 = 9; // Arduino pin connected to button's pin
const int RELAY_PIN = 4; // Arduino pin connected to relay's pin
const int RELAY_PIN3 = 2; // Arduino pin connected to relay's pin
const int RELAY_PIN2 = 3; // Arduino pin connected to relay's pin
const int RELAY_PIN1 = 5; // Arduino pin connected to relay's pin
virtuabotixRTC myRTC(6, 7, 8);
const int OnHour = 10; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 45;
const int OffHour = 10; //SET TIME TO OFF RELAY
const int OffMin = 47;
// variables will change:
int relayState = LOW; // the current state of relay
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
int relayState1 = LOW; // the current state of relay
int lastButtonState1; // the previous state of button
int currentButtonState1; // the current state of button
int relayState2 = LOW; // the current state of relay
int lastButtonState2; // the previous state of button
int currentButtonState2; // the current state of button
int relayState3 = LOW; // the current state of relay
int lastButtonState3; // the previous state of button
int currentButtonState3; // the current state of button
void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
  pinMode(BUTTON_PIN1, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN1, OUTPUT); // set arduino pin to output mode
  pinMode(BUTTON_PIN2, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN2, OUTPUT); // set arduino pin to output mode
  pinMode(BUTTON_PIN3, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN3, OUTPUT); // set arduino pin to output mode
  digitalWrite(RELAY_PIN, LOW);
  digitalWrite(RELAY_PIN1, LOW);
  digitalWrite(RELAY_PIN2, LOW);
  digitalWrite(RELAY_PIN3, LOW);
  currentButtonState = digitalRead(BUTTON_PIN);
  currentButtonState1 = digitalRead(BUTTON_PIN1);
  currentButtonState2 = digitalRead(BUTTON_PIN2);
  currentButtonState3 = digitalRead(BUTTON_PIN3);
  // Set the current date, and time in the following format:
  // seconds, minutes, hours, day of the week, day of the month, month, year
  myRTC.setDS1302Time(00, 44, 10, 3, 30, 11, 2022);
  dht.begin();
}
void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);
  Serial.print(F(" Humidity: "));
  Serial.print(h);
  Serial.print(F("% Temperature: "));
  Serial.print(t);
  Serial.print(F("C "));
  Serial.print(f);
  Serial.print(F("F Heat index: "));
  Serial.print(hic);
  Serial.print(F("C "));
  Serial.print(hif);
  Serial.println(F("F"));
  // This allows for the update of variables for time or accessing the individual elements.
  myRTC.updateTime();
  int buttonState = digitalRead(BUTTON_PIN); // read new
  int buttonState1 = digitalRead(BUTTON_PIN1); // read new state
  int buttonState2 = digitalRead(BUTTON_PIN2); // read new state
  int buttonState3 = digitalRead(BUTTON_PIN3); // read new state
  // Start printing elements as individuals
  Serial.print("Current Date / Time: ");
  Serial.print(myRTC.dayofmonth);
  Serial.print("/");
  Serial.print(myRTC.month);
  Serial.print("/");
  Serial.print(myRTC.year);
  Serial.print(" ");
  Serial.print(myRTC.hours);
  Serial.print(":");
  Serial.print(myRTC.minutes);
  Serial.print(":");
  Serial.println(myRTC.seconds);
  // Delay so the program doesn't print non-stop
  delay(500);
  lastButtonState = currentButtonState; // save the last state
  lastButtonState1 = currentButtonState1; // save the last state
  lastButtonState2 = currentButtonState2; // save the last state
  lastButtonState3 = currentButtonState3; // save the last state
  currentButtonState = digitalRead(BUTTON_PIN); // read new state
  currentButtonState1 = digitalRead(BUTTON_PIN1); // read new state
  currentButtonState2 = digitalRead(BUTTON_PIN2); // read new state
  currentButtonState3 = digitalRead(BUTTON_PIN3); // read new state
  if (t > 20) {
    digitalWrite(RELAY_PIN2, LOW);
  }
  if (lastButtonState == LOW && currentButtonState == HIGH) {
    Serial.println("The button is pressed");
    // toggle state of relay
    relayState = !relayState;
    // control relay arccoding to the toggled state
    digitalWrite(RELAY_PIN, relayState);
  }
  if (lastButtonState1 == HIGH && currentButtonState1 == LOW) {
    Serial.println("Relay 1");
    // toggle state of relay
    relayState1 = !relayState1;
    // control relay arccoding to the toggled state
    digitalWrite(RELAY_PIN1, relayState1);
  }
  if (lastButtonState2 == HIGH && currentButtonState2 == LOW) {
    Serial.println("Relay 2");
    // toggle state of relay
    relayState2 = !relayState2;
    // control relay arccoding to the toggled state
    digitalWrite(RELAY_PIN2, relayState2);
  }
  if (lastButtonState3 == HIGH && currentButtonState3 == LOW) {
    Serial.println("Relay 3");
    // toggle state of relay
    relayState3 = !relayState3;
    // control relay arccoding to the toggled state
    digitalWrite(RELAY_PIN3, relayState3);
  }
  // control using schedule
  if (myRTC.hours == OnHour && myRTC.minutes == OnMin) {
    digitalWrite(RELAY_PIN, LOW);
    Serial.println("LIGHT ON");
  }
  else if (myRTC.hours == OffHour && myRTC.minutes == OffMin) {
    digitalWrite(RELAY_PIN, HIGH);
    Serial.println("LIGHT OFF");
  }
}

Enjoy the day and have fun.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.