Relay control with two controls

Hi everyone.

I would like to make a light wake me up in the morning at a set time and be able to switch it off manually. So basically an RTC will be used to switch the relay on and then when I want it off I can press a button and it will switch off, or it will turn off after a set time.

I found this project with code but don't know how to add the manual button part...
http://www.lucadentella.it/en/2013/04/30/macchina-a-stati-finiti-e-arduino/

(Just need help with the code please)

Any help will be appreciated.
Thanks

int relayPin=5;
int offButton=6;

void setup(){
  pinMode(relayPin, OUTPUT);
  piMode(offButton, INPUT);
}
void loop(){
  if(digitalRead(offButton)==HIGH){
     digitalWrite(relayPin, LOW);
  }
}

Might work, give it a try.

Do you have a pullup or pulldown resistor to hold offButton HIGH or LOW when it is not pressed?

Sarouje:

Thanks for the reply. It compiled alright but can't test it coz it won't upload, It seems very random, I've tried different sketches and two other boards, plugged into another USB port and nothing, sometimes sketches will upload and most of the time they won't.

CrossRoads:

Yes I do, a 10k

Sarouje:

OK, great it works.

Thank you for that. now I can play with it to advance it a bit. At least i can sleep tonight.

The problem with the uploads seems to be that I have to restart the Arduino IDE.

I'll add the code

#include <Wire.h>
#include "RTClib.h"

// PIN definitions
#define RELAY_PIN 2
#define offButton 3

// FSM states
#define STATE_OFF  0
#define STATE_ON   1

// Timer settings
#define START_TIME  2113
#define END_TIME    2115

// variables
RTC_DS1307 RTC;
int fsm_state;

void setup() {
  
  Serial.begin(57600);
  Serial.println("SimpleTimer running...");
  Serial.println();

  Wire.begin();
  RTC.begin();  

  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
  fsm_state = STATE_OFF;
  pinMode(offButton, INPUT);
}

void loop() {

  DateTime now = RTC.now();
  int nowHourMinute = now.hour() * 100 + now.minute();

  // FSM states
  switch(fsm_state) {
    
    case STATE_OFF:
      if(nowHourMinute > START_TIME && nowHourMinute < END_TIME) {
        Serial.print(now.hour(), DEC);
        Serial.print(':');
        Serial.print(now.minute(), DEC);
        Serial.println(", it's time to wake up!");
        digitalWrite(RELAY_PIN, HIGH);
        fsm_state = STATE_ON;
      }
      break;
    
    case STATE_ON:
      if(nowHourMinute > END_TIME) {
        Serial.print(now.hour(), DEC);
        Serial.print(':');
        Serial.print(now.minute(), DEC);        
        Serial.println(", it's time to go to sleep!");
        digitalWrite(RELAY_PIN, LOW);
        fsm_state = STATE_OFF;
      }    
      break;
  }

  if(digitalRead(offButton) == HIGH){
    digitalWrite(RELAY_PIN, LOW);
    Serial.print("Relay Off");
  }
}