Switching to auto and manual button mode to turn on relay

Hello, i have a project with 4 relay and 4 buttons , 3 relays and buttons can turn on and off and 1 will be using RTC Module to automatic turn on and off relay with a schedule time every day.

i want to use the 1 button to switch to manual and auto mode. HIGH or LOW value

for example:

initial state of the auto/manual button will be HIGH

if i push the auto/manual button the code for schedule time to turn and off the relay

if i push the again auto/manual button it will go back to the manual button to turn on and off the relays

I already have the code from the internet but when I'm pressing the auto/button the code execute the schedule time to turn on and off but when I release the button it will execute the manual button control.

is there a way that when i press it will only execute the first code and when press again execute the second code.

thanks

#include <virtuabotixRTC.h>

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;

const int waitTime = 60000;

const int BUTTON_PIN = 12; // Auto/Manual Button
const int RELAY_PIN  = 2; // relay when press auto/manual button light indicator will turn on
// 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

const int pushButton[] ={9,10,11};// define push button inputs
const int relayPin[]={3,4,5};// output pins where 4 relays will be connected
String relayNames[] ={"CH1","CH2","CH3"};// Just put name for 4 relays
int pushed[] ={0,0,0};// status of each buttons
int relayStatus[] ={HIGH,HIGH,HIGH};// initial status of relay


void setup() {
  // Robojax.com 4-Relay-4-Push button 20181211
  Serial.begin(9600);// initialize serial monitor 
   // SET TIME AND DATE
 myRTC.setDS1302Time(55, 44, 10, 3, 30, 11, 2022);

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

  for(int i=0; i<3; i++)
  {
    pinMode(pushButton[i], INPUT_PULLUP); 
    pinMode(relayPin[i], OUTPUT);   
    digitalWrite(relayPin[i], HIGH);// initial relay status to be OFF
  }

}

void loop() {

 myRTC.updateTime();

  // 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(50);

  lastButtonState    = currentButtonState;      // save the last state
  currentButtonState = digitalRead(BUTTON_PIN); // read new state

 if(currentButtonState == HIGH){  // manual button control
  
  for(int i=0; i<3; i++)
  {
    int  val = digitalRead(pushButton[i]);   
    if(val == HIGH && relayStatus[i] == LOW){
      pushed[i] = 1-pushed[i];
      delay(100);
  }// if   

  relayStatus[i] = val;

      if(pushed[i] == HIGH){
        Serial.print(relayNames[i]);
        Serial.println(" ON");
        //Serial.println(pushed[i]);
        digitalWrite(relayPin[i], LOW); 
      }
      else{
        Serial.print(relayNames[i]);
        Serial.println(" OFF");
        //Serial.println(pushed[i]);
        digitalWrite(relayPin[i], HIGH);
      } 
      
  }//FOR
  }// if
  if(currentButtonState == LOW) { //auto control
     
      // SCHEDULE TIME TURN ON AND OFF
      if (myRTC.hours == OnHour && myRTC.minutes == OnMin && relayStatus[0] == HIGH){
      digitalWrite(relayPin[0], LOW);
      Serial.println("LIGHT ON");
      }
      else if(myRTC.hours == OffHour && myRTC.minutes == OffMin && relayStatus[0] == LOW){
      digitalWrite(relayPin[0], HIGH);
      Serial.println("LIGHT OFF");
      }
  } 
  
  Serial.println("=========================================="); 
  delay(100);
    

}// loop end

Go to the IDE and look at the examples in the very basic section at the top, and find some code that shows how to

detect the change of state of a button

Or

count button presses

Count button presses. When it tries to,go to two, reset it to zero.

Branch in you code depending on whether that count is 0 or 1 or 0 or 1 or 0 or 1 as you press the button and constrain the counter.

a7

Thanks for the reply,

But sorry I don't understand the logic and I'm new in programming Arduino.

if you can provide a code so i can test it much better thank you again a7

Did you have trouble finding the code in the examples from the IDE?

IDE examples

The one that is directly relevant is called

State Change Detection (Edge Detection) for pushbuttons

If that is above your current abilities, just start working through the earlier examples.

a7

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