0

I am building a system where I am controlling 2 LED strips using an Arduino Uno, time chip and relay. I have managed to get the lights to turn on and off on 2 separate days at different times. Now I want to add a switch to override the program, turning the lights on, when they are not programmed to. I would also like to add an LED to show when the switch is active.

Here is my code.

#include <DS3231.h>

int Relay = 4;
String today ;                           

DS3231  rtc(SDA, SCL);
Time t;

void setup() {
  Serial.begin(9600);
  rtc.begin();
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);
  delay (2000);
}

void loop() {

  today = rtc.getDOWStr();

  if (today == "Thursday") {

    if ( 15 > t.hour && t.hour < 22) {
      digitalWrite (Relay, HIGH);
    }

    else{                 
      digitalWrite (Relay, LOW);
    }
  }

  if (today == "Friday") {        

    if ( 16 > t.hour && t.hour < 23) {

      digitalWrite (Relay, HIGH);
    }
     else{

      digitalWrite (Relay, LOW);
    }
}

}

How am I best proceeding?

2 Answers2

1

Add a boolean that indicates whether the leds are on because of the time. replace digitalWrite with setting that boolean.

Then you add an if:

bool switchOn = digitalRead(switch) == HIGH; //when using pulldown configuration
bool timedOn =false;

if (today == "Thursday") {
    if ( 15 > t.hour && t.hour < 22) {
      timedOn = true;
    } else {
      timedOn = false;
    }
} else if (today == "Friday") {        
    if ( 16 > t.hour && t.hour < 23) {
      timedOn = true;
    } else {
      timedOn = false;
    }
} else {
    timedOn = false;
}

if(switchOn || timedOn){
    digitalWrite (Relay, HIGH);
} else {
    digitalWrite (Relay, LOW);
}

if(switchOn){
    digitalWrite (indicator, HIGH);
} else {
    digitalWrite (indicator, LOW);
}
ratchet freak
  • 2,823
  • 15
  • 12
1

To literally override the Arduino program, you just have to connect a toggle switch across the contacts of the relay that controls the LED strips.

To sense that the switch is closed, you could use a two-pole switch, with the second pole connected to an Arduino digital input.

Peter Bennett
  • 59,212
  • 1
  • 49
  • 132